gpt4 book ai didi

python - 如何使用 matplotlib 从字典中绘制水平条形图?

转载 作者:太空宇宙 更新时间:2023-11-03 16:02:17 25 4
gpt4 key购买 nike

我正在使用:

plt.bar(range(len(d)), d.values(), align='center')
plt.yticks(range(len(d)), d.keys())
plt.xlabel('frequency', fontsize=18)
plt.ylabel('keywords', fontsize=18)

plt.show()

我得到以下输出: enter image description here

但我想在 y 轴而不是 x 轴上显示每个关键字对应的栏。我怎样才能实现这一目标?

最佳答案

使用 matplotlib 的 barh()函数将为您创建一个水平条形图:

plt.barh(range(len(d)), d.values(), align='center')

来自文档:

matplotlib.pyplot.barh(bottom, width, height=0.8, left=None, hold=None, **kwargs)

Make a horizontal bar plot with rectangles bounded by:

left, left + width, bottom, bottom + height

(left, right, bottom and top edges)

bottom, width, height, and left can be either scalars or sequences

另请参阅 matplotlib 图库页面 here 上的 barh 演示

关于python - 如何使用 matplotlib 从字典中绘制水平条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40223833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com