Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; it's simply too big. How would I be able to do that?

Do I use the kwds argument?

The plotting site above states that I can use kwds in the following way:

"kwds : keywords Options to pass to matplotlib plotting method"

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.

like image 936
alvarezcl Avatar asked Apr 08 '15 01:04

alvarezcl


2 Answers

DataFrame.plot() returns the Axes object, you can then call ax.legend() to modifiy the settings:

ax = df.plot()
ax.legend(prop={'size':10})
like image 130
HYRY Avatar answered Oct 10 '22 21:10

HYRY


You do not have to do anything special to pass **kwds (see this SO question to understand the ** notation better).

All arguments that are not positional arguments of the DataFrame.plot method will be passed to the pyplot.plt method automatically.

Note: kwds stands for keyword arguments, so you have to use 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.

You can see it when trying aalpha: the error stack points to matplotlib, not pandas.

--

Note: the label argument does not work as is.

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. It makes sense, as y can only be a single column, where DataFrame.plot allows you to plot all the columns at once. But label does not accept a list of values, so there is no way to know which label to update.

It means that you have three options. Either pass a single column name as y: in that case label will work as intended. Or update the legend afterward (see the legend guide for reference). Or use the single-column DataFrame as a Series.

--

Edit about the original question: the **kwds arguments are passed to pyplot.plt, that has no direct link to the legend. So it is not possible to update the legend properties with DataFrame.plot.

like image 39
paulwasit Avatar answered Oct 10 '22 19:10

paulwasit