Pyplot - 标签图
我在尝试用 pyplot 标记数据时遇到了一场噩梦。
我目前正在绘制所有数据,如下所示:plt.plot(data)。
数据是一个数组,其中有一列是总成本,然后是其他列的子成本
我想添加一个图例并适当地标记每个数据列表。但我似乎无法让传奇发挥作用。 plt.legend(('Column 1','Column 2','etc.) , loc =1) 和其他类似的东西不起作用。如果我单独绘制每一列,它会由于某种原因停止正常工作。
我期待并感谢您的任何建议。
编辑:
for i in range(1,days):
data.append(totalCost(i)) #cost returns retVal, construction, gas, and wage
plt.ylabel('Cost in US Dollars')
plt.title('Economic Cost over Time')
plt.plot(data)
plt.legend(('Total','Construction','Gas','Wage'),loc=1) # Legend is blank
编辑2: 我没有将代码放在程序中的不同位置,而是重新组织了代码并将所有内容集中起来。随着这些变化,传奇开始发挥作用,一切似乎都得到了解决。我不知道问题是什么。
plt.plot(data)
plt.legend(('Total Cost', 'Construction Cost', 'Gas Cost','Wage Cost'),loc=0)
plt.grid(False)
plt.xlabel('Time (Days)')
plt.ylabel('Cost in US Dollars')
plt.title('Economic Cost over Time')
print (' Close the Graph to Continue Using this Model')
plt.show()
I am having a nightmare of a time trying to label data with pyplot.
I am currently plotting all of my data like this: plt.plot(data).
data is an array that has a column of total costs, and then other columns for sub costs
I would like to add a legend and label each of the data lists appropriately. I can't seem to get the legend to work though. plt.legend(('Column 1','Column 2','etc.) , loc =1) and other things like that did not work. If I plot each column individually, it stops working correctly for some reason.
I look forward and thank you for any advice.
EDIT:
for i in range(1,days):
data.append(totalCost(i)) #cost returns retVal, construction, gas, and wage
plt.ylabel('Cost in US Dollars')
plt.title('Economic Cost over Time')
plt.plot(data)
plt.legend(('Total','Construction','Gas','Wage'),loc=1) # Legend is blank
EDIT 2:
Instead of having the code in different locations in my program, I reorganized it and centralized everything. With those changes, the legend started working, and everything seems to be resolved. I have no idea what the issue was though.
plt.plot(data)
plt.legend(('Total Cost', 'Construction Cost', 'Gas Cost','Wage Cost'),loc=0)
plt.grid(False)
plt.xlabel('Time (Days)')
plt.ylabel('Cost in US Dollars')
plt.title('Economic Cost over Time')
print (' Close the Graph to Continue Using this Model')
plt.show()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码在 Mac OS X 上使用 Matplotlib 1.1 可以正确显示图例,前提是:
plt.ion()
plt.show 结尾()
,例如。升级 Matplotlib 也可能有帮助。
最后一种可能的解决方案是确实使用自己的标签单独绘制每条线: plt.plot(, label='Construction') 等,并绘制图例与 plt.legend(loc='best')。
Your code properly displays a legend, with Matplotlib 1.1 on Mac OS X, provided that:
plt.ion()
plt.show()
, for instance.Upgrading Matplotlib might also help.
A last possible solution would be to indeed plot each line separately with its own label:
plt.plot(<single line data>, label='Construction')
, etc., and the plot the legend withplt.legend(loc='best')
.在 plt.legend() 中传递标题,您可以创建一个图例列表并将它们传递到您的绘图,即,
legends = ['总成本', '建筑成本', '天然气成本','工资成本']
plt.plot(数据,标签=图例)
plt.legend()
Insted of passing headings inside plt.legend() u can create an list of legends and pass them to your plot ie,
legends = ['Total Cost', 'Construction Cost', 'Gas Cost','Wage Cost']
plt.plot(data , labels = legends)
plt.legend()