alvarezcl
alvarezcl

Reputation: 579

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.

Upvotes: 5

Views: 7103

Answers (3)

Iopheam
Iopheam

Reputation: 1228

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))

Upvotes: 1

paulwasit
paulwasit

Reputation: 426

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.

Upvotes: 1

HYRY
HYRY

Reputation: 97331

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

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

Upvotes: 3

Related Questions