这是我第一次尝试使用Python绘图,但我在创建图例方面遇到了问题。
以下是我的导入:
import matplotlib.pyplot as plt
import pandas
我是这样加载我的数据的:
data = pandas.read_csv( 'data/output/limits.dat', sep=r"\s+", encoding = 'utf-8' )
并像这样绘制图表:
axdata = data.plot( label = '$|U|^{2}$' , x = 'mass', y = 'U2',
style = '-s', markeredgecolor = 'none' )
显然,axdata现在是一个AxesSubplot
。
现在我想按照这里的描述来创建图例:
plt.legend( (line1), ('label1') )
但我不知道如何从一个AxesSubplot
中提取出一个line
对象。
plt.legend()
本身是有效的,但我只想让一些线条显示在图例中。这是正确的方法吗?还有其他命令可以在这里使用吗?
编辑:
例如,如果我尝试:
plt.legend( [axdata], ['U2'])
我看到了这个错误:
~/.virtualenvs/science/lib/python3.3/site-packages/matplotlib/legend.py:613:
UserWarning: Legend does not support Axes(0.125,0.1;0.775x0.8)
Use proxy artist instead.
http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist
(str(orig_handle),))
我还没有弄清楚代理艺术家是什么,但我认为它是一个工具,用于当您使用非默认图形对象时,这里可能不是这种情况,因为我尝试生成一个普通的matlibplot图。'非默认'和'正常'是我的话 - 我还不确定它们的意思。
另一个编辑:(因为我误读了评论)
plt.legend()
单独使用不会输出任何内容到控制台,但由所绘制的数据自动生成一个图例。
ax.legend()
和plt.draw()
会发生什么? - TomAugspurger