python绘制条形图 中文横坐标_python – 如何并排绘制具有相同X坐标的条形图(‘dodged’)...

python绘制条形图 中文横坐标_python – 如何并排绘制具有相同X坐标的条形图(‘dodged’)...import matplotlib pyplot as plt gridnumber range 1 4 b1 plt bar gridnumber 0 2 0 3 0 1 width 0 4 label Bar 1 align center b2

大家好,我是讯享网,很高兴认识大家。

import matplotlib.pyplot as plt

gridnumber = range(1,4)

b1 = plt.bar(gridnumber, [0.2, 0.3, 0.1], width=0.4,

label="Bar 1", align="center")

b2 = plt.bar(gridnumber, [0.3, 0.2, 0.2], color="red", width=0.4,

label="Bar 2", align="center")

plt.ylim([0,0.5])

plt.xlim([0,4])

plt.xticks(gridnumber)

plt.legend()

plt.show()

目前b1和b2相互重叠.我如何单独绘制它们如下:

解决方法:

matplotlib网站中有一个example.基本上,您只需将x值移动宽度即可.这是相关的一点:

import numpy as np

import matplotlib.pyplot as plt

N = 5

menMeans = (20, 35, 30, 35, 27)

menStd = (2, 3, 4, 1, 2)

ind = np.arange(N) # the x locations for the groups

width = 0.35 # the width of the bars

fig = plt.figure()

ax = fig.add_subplot(111)

rects1 = ax.bar(ind, menMeans, width, color='royalblue', yerr=menStd)

womenMeans = (25, 32, 34, 20, 25)

womenStd = (3, 5, 2, 3, 3)

rects2 = ax.bar(ind+width, womenMeans, width, color='seagreen', yerr=womenStd)

# add some

ax.set_ylabel('Scores')

ax.set_title('Scores by group and gender')

ax.set_xticks(ind + width / 2)

ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5') )

ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )

plt.show()

标签:python,matplotlib,bar-chart

来源: https://codeday.me/bug/20190926/1822263.html

小讯
上一篇 2025-01-19 11:07
下一篇 2025-03-18 19:49

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/53701.html