如何将垂直和水平轴标签设置为粗体大小或字体

时间:2016-10-26 13:34:05

标签: python matplotlib

如何为x和y标签获取粗体字体。我使用weight='bold'作为plt但不适用于主机。

from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA

if 2:
    host = host_subplot(111, axes_class=AA.Axes)
    plt.subplots_adjust(right=0.75)
    par1 = host.twinx()
    par2 = host.twinx()
    offset = 60
    new_fixed_axis = par2.get_grid_helper().new_fixed_axis
    par2.axis["right"] = new_fixed_axis(loc="right",
                                        axes=par2,
                                        offset=(offset, 0))
    par2.axis["right"].toggle(all=True)
    host.set_xlim(1, 9)
    host.set_ylim(200,1100)
    host.set_xlabel('Station Number [-]', weight='bold')
    host.set_ylabel('Temperature [K]', weight='bold')
    par1.set_ylabel('Pressure [kPa]', weight='bold')
    par2.set_ylabel("Mass flow rate [kg/s]", weight='bold')
    p1, = host.plot(Station, Total_temperature,'k-*',label="Total Temperature",ms=8,mew=2,mfc='w',linewidth=2)
    p1, = host.plot(Station, Static_temperature, 'k--o',label="Static Temperature",ms=8,mew=2,linewidth=2)
    p2, = par1.plot(Station, Total_pressure, 'k-v',label="Total Pressure",ms=8,mew=2,mfc='w',linewidth=2)
    p2, = par1.plot(Station, Static_pressure,'k--d',label="Static Pressure",ms=8,mew=2,linewidth=2)
    p3, = par2.plot(Station, Mass_flow,'k-x',label="Mass Flow Rate",ms=8,mew=2,mfc='w',linewidth=2)
    plt.grid()
    par1.set_ylim(40,400)
    par2.set_ylim(0.287,0.294)
    host.legend(prop={'size':12}, loc='center right')
    #legend1 = host.legend(('Total Temperature', 'Static Temperature', 'Mass Flow Rate'),'upper right', prop={'size':13})
    #plt.legend(('Total Pressure','Static Pressure'),'lower right',prop={'size':13})
    #plt.gca().add_artist(legend1)
    host.axis["left"].label.set_color(p1.get_color())
    par1.axis["right"].label.set_color(p2.get_color())
    par2.axis["right"].label.set_color(p3.get_color())
    plt.savefig('Stations.svg')
    plt.draw()
    plt.show()

2 个答案:

答案 0 :(得分:1)

要更改axes.set_xlabel / axes.set_ylabel的字体属性,weight='bold'需要在fontdict关键字字典中传递,而不是作为独立的关键字参数传递。例如:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.set_xlabel('xlabel', fontdict=dict(weight='bold'))

plt.show()

enter image description here

答案 1 :(得分:-2)

您可以使用 fontweight="Bold"。它应该可以正常工作。