简体   繁体   English

Panda DataFrame 传入参数进行绘图

[英]Panda DataFrame Passing in Parameters For Plotting

For any pandas DataFrame, say对于任何熊猫数据帧,说

df

I can plot relevant information using我可以使用绘制相关信息

df.plot()

but on the pandas site for plotting: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html , I am looking for a way to reduce the size of legend;但是在用于绘图的熊猫站点上: http : //pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html ,我正在寻找一种方法来减少图例的大小; it's simply too big.它太大了。 How would I be able to do that?我怎么能做到这一点?

Do I use the kwds argument?我是否使用 kwds 参数?

The plotting site above states that I can use kwds in the following way:上面的绘图站点指出我可以通过以下方式使用 kwds:

"kwds : keywords Options to pass to matplotlib plotting method" “kwds : 传递给 matplotlib 绘图方法的关键字选项”

How do I use that exactly?我如何使用它? Do I use a dictionary?我使用字典吗? How can I make it so that this option refers to the legend, ie, something like我怎样才能让这个选项引用图例,即像

plt.legend(['foo'],prop={'size':13})

for the fontsize of the legend, which makes it smaller.对于图例的字体大小,使其更小。

DataFrame.plot() returns the Axes object, you can then call ax.legend() to modifiy the settings: DataFrame.plot()返回Axes对象,然后您可以调用ax.legend()来修改设置:

ax = df.plot()
ax.legend(prop={'size':10})

You do not have to do anything special to pass **kwds (see this SO question to understand the ** notation better).您无需执行任何特殊操作即可传递**kwds (请参阅此SO 问题以更好地理解**符号)。

All arguments that are not positional arguments of the DataFrame.plot method will be passed to the pyplot.plt method automatically.所有不是DataFrame.plot方法的位置参数的参数将自动传递给pyplot.plt方法。

Note: kwds stands for keyword arguments, so you have to use arg_name = arg_value .注意: kwds代表关键字参数,因此您必须使用arg_name = arg_value

You might have already used it without knowing, for example in df.plot(alpha=0.5) : alpha is not a positional argument of DataFrame.plot , so it is passed to pyplot.plt .您可能已经在不知道的情况下使用了它,例如在df.plot(alpha=0.5) : alpha 不是DataFrame.plot的位置参数,因此它被传递给pyplot.plt

You can see it when trying aalpha : the error stack points to matplotlib, not pandas.您可以在尝试aalpha时看到它:错误堆栈指向 matplotlib,而不是 pandas。

-- ——

Note: the label argument does not work as is.注意: label参数不能按原样工作。

In the pandas code , you can see that the legend labels are automatically generated from the column names, except when the y argument is explicitly passed.pandas 代码中,您可以看到图例标签是根据列名自动生成的,除非显式传递y参数。 It makes sense, as y can only be a single column, where DataFrame.plot allows you to plot all the columns at once.这是有道理的,因为y只能是单列,而DataFrame.plot允许您一次绘制所有列。 But label does not accept a list of values, so there is no way to know which label to update.但是label不接受值列表,因此无法知道要更新哪个标签。

It means that you have three options.这意味着您有三个选择。 Either pass a single column name as y : in that case label will work as intended.将单个列名作为y传递:在这种情况下, label将按预期工作。 Or update the legend afterward (see the legend guide for reference).或者之后更新图例(请参阅图例指南以供参考)。 Or use the single-column DataFrame as a Series.或者使用单列 DataFrame 作为一个系列。

-- ——

Edit about the original question : the **kwds arguments are passed to pyplot.plt , that has no direct link to the legend.编辑原始问题**kwds参数传递给pyplot.plt ,它与图例没有直接链接。 So it is not possible to update the legend properties with DataFrame.plot .因此无法使用DataFrame.plot更新图例属性。

You can do this:你可以这样做:

df.plot().legend(prop={'size':10})

You can also pass more parameters (this will place legend outside of the plot):您还可以传递更多参数(这会将图例放置在图之外):

df.plot().\
    legend(loc='center left', bbox_to_anchor=(1.0, 0.5))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM