attributeError:' list'对象没有属性' get_label'
我正在尝试使用两个Y轴和一个X轴的基本情节。为了获得不同曲线的传说信息,我会得到属性。 这是我的代码:
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])
如果有人知道为什么会发生这种情况。
I'm trying basic plot with two y-axis and one x-axis. To obtain the legend information for different curve I'm getting AttributeError.
Here is my code:
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()
I'm getting Error on below line:
ax1.legend(curves, [curve.get_label() for curve in curves])
Please let me know if anyone know why its happening.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将解决您的问题,请尝试以下操作:
This will solve your problem, try this:
正如LC05所暗示的那样,如果您不想进行每个曲线进行硬码,则可以:
从第二轴上获取标签和手柄:
循环穿过每个曲线以附加:
仅创建一个传奇:
As LC05 alluded to above, If you don't want to hardcode each curve, you can:
Get the labels and handles from second axis:
Loop through each to append:
Create only one legend:
如果您阅读 pyplot文档绘图函数返回一个列表,该列表显然没有方法
get_label()
。您想要的可能是 matplotlib的Legend文档 ,这是对情节标签的自动检测。
这意味着您不必存储您的行结果,而您的传奇电话
从我看来
,阅读文档不仅在大多数情况下解决了您的问题,而且还为您提供了在编程世界中非常重要的能力,是能够自己解决问题(以及阅读文档)。
干杯
if you read the pyplot documentation, you can see that the plot function returns a list, which clearly does not have a method
get_label()
.What you want is probably what is described in matplotlib's legend documentation, which is automatic detection of your plot's labels.
This means that you do not have to store your line results, and your legend calls goes from
to simply
In my opinion reading the documentation not only solves your problem in most cases, but gives you a very important ability in the world of programming, which is to be able to solve the problems on your own (as well as reading documentations).
Cheers