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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22070263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:10:37  来源:igfitidea点击:

Create a legend with pandas and matplotlib.pyplot

pythonmatplotlibpandas

提问by paco_uk

This is my first attempt at plotting with python and I'm having problems creating a legend.

这是我第一次尝试用 python 绘图,但我在创建图例时遇到了问题。

These are my imports:

这些是我的进口:

import matplotlib.pyplot as plt
import pandas

I load my data like this:

我像这样加载我的数据:

data = pandas.read_csv( 'data/output/limits.dat', sep=r"\s+", encoding = 'utf-8' )

and plot it like this:

并像这样绘制它:

axdata = data.plot( label = '$|U|^{2}$' , x = 'mass', y = 'U2',
                    style = '-s', markeredgecolor = 'none' )

Apparently axdata is now an AxesSubplot.

显然 axdata 现在是一个 AxesSubplot.

Now I want to create a legend as described herelike this:

现在我想像这里描述的那样创建一个图例:

plt.legend( (line1), ('label1') )

but I don't know how to extract a lineobject from an AxesSubplot

但我不知道如何line从一个对象中提取一个对象AxesSubplot

plt.legend()on its own works, but I only want some of my lines to feature in the legend. Is this the right approach? Is there another command I can use here?

plt.legend()在它自己的作品中,但我只希望我的一些台词出现在图例中。这是正确的方法吗?我可以在这里使用另一个命令吗?

EDIT:

编辑

For example, if I try:

例如,如果我尝试:

plt.legend( [axdata], ['U2'])

I get the error:

我收到错误:

~/.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),))

I haven't worked out what a proxy artist is yet but I think it is a tool for when you are using a non-default graphical object, which I thought probably was not the case here because I am trying to produce a normal matlibplot plot. The words 'non-default' and 'normal' are mine - I'm not sure what they mean yet.

我还没有弄清楚代理艺术家是什么,但我认为它是当您使用非默认图形对象时的一种工具,我认为这里可能不是这种情况,因为我正在尝试生成一个正常的 matlibplot 图. “非默认”和“正常”这两个词是我的意思——我还不确定它们是什么意思。

ANOTHER EDIT:(because I misread the comment )

另一个编辑:(因为我误读了评论)

plt.legend()on it's own doesn't output anything to the console but the resulting plot now has a legend auto-generated from the plotted data.

plt.legend()它本身不会向控制台输出任何内容,但结果图现在具有从绘制数据自动生成的图例。

采纳答案by jmz

I think what you want to do is be able to display a legend for a subset of the lines on your plot. This should do it:

我认为您想要做的是能够为您的情节中的一部分线显示图例。这应该这样做:

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

Giving

给予

enter image description here

在此处输入图片说明