mrapacz
mrapacz

Reputation: 1029

How to set the font size for labels in pd.DataFrame.plot()? (pandas)

I'm wondering if it is possible to override the label sizes for a plot generated with pd.DataFrame.plot() method. Following the docs I can easily do that for the xticks and yticks using the fontsize kwarg:

fontsize int, default None Font size for xticks and yticks.

Unfortunately, I don't see a similar option that would change the size of the xlabel and ylabel. Here's a snippet visualizing the issue:

import pandas as pd
df = pd.DataFrame(
    [
        {'date': '2020-09-10', 'value': 10},
        {'date': '2020-09-10', 'value': 12},
        {'date': '2020-09-10', 'value': 13},
    ]
)
df.plot(x='date', y='value', xlabel='This is the date.', ylabel='This is the value.', fontsize=10)

Plot with fontsize=10

df.plot(x='date', y='value', xlabel="This is the date.", ylabel="This is the value.", fontsize=20)

enter image description here

Can I change the size of xlabel and ylabel in a similar manner?

Upvotes: 2

Views: 3947

Answers (1)

Roim
Roim

Reputation: 3066

As the documentation states, the result is a matplotlib object (by default, unless you changed it). Therefore you can change whatever you like in the same way you would change a matplolib object:

from matplolib import pyplot as plt
plt.xlabel('This is the date.', fontsize=18)
plt.ylabel('This is the value.', fontsize=16)

You can keep changing the object as you wish using matplolib options.

Upvotes: 0

Related Questions