5

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.

alvarezcl
  • 579
  • 6
  • 22

3 Answers3

3

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

ax = df.plot()
ax.legend(prop={'size':10})
HYRY
  • 94,853
  • 25
  • 187
  • 187
  • I have the same question as alvarezcl but unfortunately your response doesn't really answer it. How do you use the kwds argument to pass a plot setting (say alpha level of points) from pandas.DataFrame.plot to matplotlib.pyplot.plot? The pandas docs indicates you can do this using the kwds keyword but I understand how. – dreme Jul 06 '16 at 05:41
  • 2
    Agreed, this response does not answer the question, although it is accepted. I am still looking for the syntax for passing keywords in a pandas plot, but there is not a single example around that shows how to do it. – marillion Oct 03 '17 at 23:31
1

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.

paulwasit
  • 416
  • 2
  • 12
1

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))
Iopheam
  • 1,065
  • 10
  • 11