简体   繁体   English

如何将 matplotlib 轴刻度标签字体粗体设置为粗体?

[英]How to set the matplotlib axes tick labels fontweight to bold?

I was trying the following code:我正在尝试以下代码:

fig, ax1 = plt.subplots()
#data is a geopandas DataFrame 
data.plot(ax=ax1, kind='bar', color= barcolor, width= 0.8)
ax1.set_yticklabels(ax1.get_yticklabels(), weight='bold')
ax1.set_xticklabels(ax1.get_xticklabels(), rotation=0, weight='bold', size=12)

Only x-axis gets affected by this code and y-axis labels are getting removed.只有 x 轴会受到此代码的影响,并且 y 轴标签会被移除。

Change get_yticklabels() to get_yticks works for me:get_yticklabels()更改为get_yticks对我有用:

fig, ax1 = plt.subplots()
ax1.bar([0,1],[2,3])
ax1.set_yticklabels(ax1.get_yticks(), weight='bold')
ax1.set_xticklabels(ax1.get_xticks(), rotation=0, weight='bold', size=12)

Output: Output:

在此处输入图像描述

Here is the answer for reasons behind this kind of error. 是此类错误背后原因的答案。 It's because matplotlib avoids static positioning.这是因为 matplotlib 避免了 static 定位。 Adding the following code just after data.plot() helped too:data.plot()之后添加以下代码也有帮助:

fig.canvas.draw()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM