代码:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['Times New Roman'] # 正常显示中文
plt.rcParams['axes.unicode_minus'] = False # 用来显示负号
plt.rcParams['xtick.direction'] = 'in' # 将x周的刻度线方向设置向内
plt.rcParams['ytick.direction'] = 'in' # 将y轴的刻度方向设置向内`
font={'family':'STSong','size': 10.5,} # 中文宋体,五号字体
labels = ['运行状态', '发电机转速', '机组有功', '机舱外温度',
'风轮转速', '变桨轴1位置', '变桨轴2位置',
'变桨轴3位置', '风速', '风向', '限电标志位']
corr = rho.iloc[:-1]
x = np.arange(len(labels)) # the label locations
width = 0.6 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x, np.abs(corr.round(3)), width)
#rects2 = ax.bar(x + width/2, women_means, width, label='Women')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('相关系数',fontdict=font)
# ax.set_title('Scores by group and gender')
ax.set_yticks([x for x in np.arange(0,1.1,0.2)])
ax.set_xticks(x,fontsize=10.5)
ax.set_xticklabels(labels,rotation=45,fontdict=font)
def autolabel(rects):
"""Attach a text label above each bar in *rects*, displaying its height."""
for rect in rects:
height = rect.get_height()
ax.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
autolabel(rects1)
#autolabel(rects2)
fig.tight_layout()
plt.savefig('./Figures/关键影响因素.svg', bbox_inches='tight')
plt.show()
运行结果: