直方图code
import matplotlib.pyplot as plt
import os # 文件保存所需头文件
# 横坐标
x = [0.1430848603, 0.0882253359, 0.0563979112, 0.0458832019, 0.0442359346,
0.0435552462, 0.0393077842, 0.0387827515, 0.0384597305, 0.0344006384]
# 纵坐标
y = [3, 34, 53, 21, 29, 58, 14, 81, 12, 58]
# 每个柱的颜色设置
color = ['steelblue', 'cornflowerblue', 'royalblue', 'lightsteelblue', 'slateblue', 'mediumslateblue', 'mediumpurple', 'darkslateblue', 'plum', 'thistle']
plt.barh(range(10), importance, height=0.6, color=color, alpha=0.8) # 从下往上画
plt.yticks(range(10), features)
plt.xlim(0, 0.2)
plt.xlabel('x')
plt.ylabel('y')
plt.title("bar")
# 将值显示于图片
for x, y in enumerate(importance):
plt.text(y, x, '%s' % y)
plt.show()
#图片保存
dir = 'F:\picture\\'
plt.savefig(os.path.join(save_dir, '1.png'))
结果

色卡

参考链接:
这篇博客展示了如何使用Python的matplotlib库绘制直方图,并详细解释了代码中的各个参数,包括设置横纵坐标、柱状颜色、图例及显示数值等。通过示例代码,读者可以了解到如何自定义直方图的视觉效果。
2万+

被折叠的 条评论
为什么被折叠?



