Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pandas和matplotlib.pyplot创建图例_Python_Matplotlib_Pandas - Fatal编程技术网

Python 使用pandas和matplotlib.pyplot创建图例

Python 使用pandas和matplotlib.pyplot创建图例,python,matplotlib,pandas,Python,Matplotlib,Pandas,这是我第一次尝试用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',

这是我第一次尝试用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' )
plt.legend( (line1), ('label1') )
把它画成这样:

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' )
plt.legend( (line1), ('label1') )
显然,axdata现在是一个
AxesSubplot

现在我想创建一个图例,如下所述:

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' )
plt.legend( (line1), ('label1') )
但是我不知道如何从
AxesSubplot

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()
本身不会向控制台输出任何内容,但生成的绘图现在有一个根据绘图数据自动生成的图例。

我认为您要做的是能够显示绘图上某一行子集的图例。这应该做到:

df = pd.DataFrame(np.random.randn(400, 4), columns=['one', 'two', 'three', 'four'])
ax1 = df.cumsum().plot()
lines, labels = ax1.get_legend_handles_labels()
ax1.legend(lines[:2], labels[:2], loc='best')  # legend for first two lines only
给予


无论如何,您可以提供更多的信息吗?例如,plt.legend()输出什么以及您希望图例输出什么?这将极大地帮助我们了解如何帮助您:)如果您执行
ax.legend()
plt.draw()
,会发生什么?我得到一个图例,但它包含所有行的条目,而不仅仅是返回原始axdata对象的行。这正是我想要做的。不幸的是,当我调用
data.plot()
(根据pandas文档,它应该是
plt.plot()
的包装器)时,返回的对象不支持
get\u legend\u handles\u labels()
。我不知道如何从中提取行或将其转换为任何类型
ax1
是您的示例
ax1
是调用
pandas.DataFrame.plot()
返回的
matplotlib.axes.AxesSubplot
。这正是
data.plot()
应该返回的内容。如果不运行代码进行调试,我不确定问题可能是什么,抱歉。这是一个打字错误:(。结果我拼写标签错了。感谢您的演示。