Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change pandas DataFrame.plot fontsize of xlabel?

I'm trying to plot a figure by pandas.

The font of other text could be set.

But xlabel couldn't be set. ax3 couldn't use axes.set_xlabel() parameter.

I also try ax.set_fontsize() or plt.rcParams.update({'font.size': 22}). It doesn't work.

    plt.figure()
    ax1 = df_plot.plot(x='wind_direct',y=plot_columns,figsize=(30,18),linewidth=5,kind='line',legend=True, fontsize=16)
    ax1.legend(loc=2,fontsize=20)
    ax1.set_ylabel('kw',fontdict={'fontsize':24})

    ax2 = ax1.twinx()
    ax3 = df_plot.plot(x='wind_direct',y=counts_columns,figsize=(30,18),kind='bar',legend=True, ax=ax2, fontsize=16)
    ax3.set_title(title,pad=20, fontdict={'fontsize':24})
    ax3.set_ylabel('counts',fontdict={'fontsize':24})
    #ax3.set_fontsize(24)
    plt.rcParams.update({'font.size': 22})
    ax3.legend(loc=1,fontsize=20);

enter image description here

like image 817
JiangKui Avatar asked Jan 02 '20 07:01

JiangKui


People also ask

How do I change the size of my pandas plot?

The size of a plot can be modified by passing required dimensions as a tuple to the figsize parameter of the plot() method. it is used to determine the size of a figure object.

How do I change the axis label size in Matplotlib?

If we want to change the font size of the axis labels, we can use the parameter “fontsize” and set it your desired number.


1 Answers

plt.figure() The parameter 'plt.rcParams.update({'font.size': 36})' must set in the top.

like this:

plt.figure()
plt.rcParams.update({'font.size': 22}) # must set in top

ax1 = df_plot.plot(x='wind_direct',y=plot_columns,figsize=(30,18),linewidth=5,kind='line',legend=True, fontsize=16)
ax1.legend(loc=2,fontsize=20)
ax1.set_ylabel('kw',fontdict={'fontsize':24})
ax2 = ax1.twinx()
ax3 = df_plot.plot(x='wind_direct',y=counts_columns,figsize=(30,18),kind='bar',legend=True, ax=ax2, fontsize=16)
ax3.set_title(title,pad=20, fontdict={'fontsize':24})
ax3.set_ylabel('counts',fontdict={'fontsize':24})
ax3.legend(loc=1,fontsize=20);

enter image description here

Because the 'plt.rcParams' is the default value.

The xlabel font maybe use 'plt.rcParams' when pandas.DataFrame.axes is bulit.

So 'plt.rcParams' should set in front of pandas.DataFrame.axes.

like image 164
JiangKui Avatar answered Oct 11 '22 04:10

JiangKui