我正在嘗試使用兩個 y 軸和一個 x 軸的基本圖。為了獲得不同曲線的圖例資訊,我得到了 AttributeError。這是我的代碼:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2.0*np.pi, 101)
y = np.sin(x)
z = np.sinh(x)
# separate the figure object and axes object from the plotting object
fig, ax1 = plt.subplots()
# Duplicate the axes with a differebt y axis and the same x axis
ax2 = ax1.twinx() # ax2 and ax1 will have common x axis and different y axis
# plot the curves on axes 1, and 2 and get the curve hadles
curve1 = ax1.plot(x, y, label="sin", color='r')
curve2 = ax2.plot(x, z, label="sinh", color='b')
# Make a curves list to access the parameters in the curves
curves = [curve1, curve2]
# Add legend via axes1 or axex 2 object.
# ax1.legend() will not display the legend of ax2
# ax2.legend() will not display the legend of ax1
ax1.legend(curves, [curve.get_label() for curve in curves])
#ax2.legend(curves, [curve.get_label() for curve in curves]) also valid
# Global figure properties
plt.title("Plot of sine and hyperbolic sine")
plt.show()
我在下一行收到錯誤:
ax1.legend(curves, [curve.get_label() for curve in curves])
如果有人知道為什么會這樣,請告訴我。
uj5u.com熱心網友回復:
這將解決你的問題,試試這個:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2.0*np.pi, 101)
y = np.sin(x)
z = np.sinh(x)
# separate the figure object and axes object from the plotting object
fig, ax1 = plt.subplots()
# Duplicate the axes with a differebt y axis and the same x axis
ax2 = ax1.twinx() # ax2 and ax1 will have common x axis and different y axis
# plot the curves on axes 1, and 2 and get the curve hadles
curve1 = ax1.plot(x, y, label="sin", color='r')
curve2 = ax2.plot(x, z, label="sinh", color='b')
# Make a curves list to access the parameters in the curves
curves = curve1 curve2
# Add legend via axes1 or axex 2 object.
# ax1.legend() will not display the legend of ax2
# ax2.legend() will not display the legend of ax1
labs = [curve.get_label() for curve in curves]
ax1.legend(curves, labs, loc=0)
#ax1.legend(curves, [curve.get_label() for curve in curves])
#ax2.legend(curves, [curve.get_label() for curve in curves]) also valid
# Global figure properties
plt.title("Plot of sine and hyperbolic sine")
plt.show()
uj5u.com熱心網友回復:
因為 get_label 不支持串列資料型別,[https://www.geeksforgeeks.org/matplotlib-axes-axes-get_label-in-python/][1] 試試這個。
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2.0*np.pi, 101)
y = np.sin(x)
z = np.sinh(x)
# separate the figure object and axes object from the plotting object
fig, ax1 = plt.subplots()
# Duplicate the axes with a differebt y axis and the same x axis
ax2 = ax1.twinx() # ax2 and ax1 will have common x axis and different y axis
# plot the curves on axes 1, and 2 and get the curve hadles
curve1, = ax1.plot(x, y, label="sin", color='r')
curve2, = ax2.plot(x, z, label="sinh", color='b')
# Make a curves list to access the parameters in the curves
curves = [curve1, curve2]
# Add legend via axes1 or axex 2 object.
# ax1.legend() will not display the legend of ax2
# ax2.legend() will not display the legend of ax1
ax1.legend(curves, curve1.get_label())
#ax2.legend(curves, curve2.get_label())
#ax2.legend(curves, [curve.get_label() for curve in curves]) also valid
# Global figure properties
plt.title("Plot of sine and hyperbolic sine")
plt.show()
uj5u.com熱心網友回復:
如果您閱讀pyplot 檔案,您可以看到 plot 函式回傳一個串列,該串列顯然沒有方法get_label()
。
您想要的可能是matplotlib 的圖例檔案中描述的內容,即自動檢測您的繪圖示簽。這意味著您不必存盤您的線路結果,并且您的圖例呼叫來自
ax1.legend(curves, [curve.get_label() for curve in curves])
簡單地
ax1.legend()
在我看來,閱讀檔案不僅在大多數情況下可以解決您的問題,而且還為您提供了編程領域非常重要的能力,即能夠自己解決問題(以及閱讀檔案)。
干杯
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/491031.html