如何更改xlabel的pandas DataFrame.plot fontsize?

Jia*_*Kui 7 python dataframe pandas

我正在尝试用熊猫绘制图形。

可以设置其他文本的字体。

但是无法设置 xlabel。ax3 无法使用 axes.set_xlabel() 参数。

我也尝试 ax.set_fontsize() 或 plt.rcParams.update({'font.size': 22})。它不起作用。

    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);
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

Jia*_*Kui 10

plt.figure() 参数 'plt.rcParams.update({'font.size': 36})' 必须设置在顶部。

像这样:

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);
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

因为“plt.rcParams”是默认值。

当 pandas.DataFrame.axes 建立时,xlabel 字体可能使用“plt.rcParams”。

所以 'plt.rcParams' 应该设置在 pandas.DataFrame.axes 的前面。