JiangKui
JiangKui

Reputation: 1347

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

Upvotes: 19

Views: 50755

Answers (1)

JiangKui
JiangKui

Reputation: 1347

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.

Upvotes: 25

Related Questions