700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > python绘制并列的条形图的方法_如何并排绘制具有相同X坐标的条形图('dodged')...

python绘制并列的条形图的方法_如何并排绘制具有相同X坐标的条形图('dodged')...

时间:2022-09-11 20:23:04

相关推荐

python绘制并列的条形图的方法_如何并排绘制具有相同X坐标的条形图('dodged')...

有时很难找到合适的宽度。我通常使用这个np.diff来找到正确的维度。import numpy as np

import matplotlib.pyplot as plt

#The data

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

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

indices = [5.5,6,7,8.5,8.9]

#Calculate optimal width

width = np.min(np.diff(indices))/3

fig = plt.figure()

ax = fig.add_subplot(111)

ax.bar(indices-width,womenMeans,width,color='b',label='-Ymin')

ax.bar(indices,menMeans,width,color='r',label='Ymax')

ax.set_xlabel('Test histogram')

plt.show()

结果是:

如果我在x轴上的索引是名义值,如名称:#

import numpy as np

import matplotlib.pyplot as plt

# The data

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

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

indices = range(len(womenMeans))

names = ['Asian','European','North Amercian','African','Austrailian','Martian']

# Calculate optimal width

width = np.min(np.diff(indices))/3.

fig = plt.figure()

ax = fig.add_subplot(111)

ax.bar(indices-width/2.,womenMeans,width,color='b',label='-Ymin')

ax.bar(indices+width/2.,menMeans,width,color='r',label='Ymax')

#tiks = ax.get_xticks().tolist()

ax.axes.set_xticklabels(names)

ax.set_xlabel('Test histogram')

plt.show()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。