美文网首页大数据 爬虫Python AI Sql
Matplotlib->柱状图(bar)度量值

Matplotlib->柱状图(bar)度量值

作者: 默直 | 来源:发表于2020-01-03 12:10 被阅读0次

我们在绘制图表时,不可避免的在x轴中做出不同的类型的柱状图,那么怎么让多个类型恰如其分的位于x轴,这时我们不妨如下代码描述的样子,进行绘制。

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1026)#保证每次随机数都是一样的数值
# 解决中文乱码问题
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
#解决坐标轴刻度符号乱码
plt.rcParams['axes.unicode_minus'] = False

size=5
a=np.random.rand(size)
b=np.random.rand(size)
c=np.random.rand(size)

#x是横坐标,[0,1,2,3]
x=np.arange(size)
print(x)
#有多少类型,只需要更改n即可
#横坐标总长度为0.9,有3个数据类型,每个类型所占的长度为width=total_width-width
total_width=0.8
n=3
width=total_width/n
print(width)
x=x-(total_width-width)/2
print(x)

plt.bar(x,a,width=width,label='a')
plt.bar(x+width,b,width=width,label='b')
plt.bar(x+2*width,c,width=width,label='c')
plt.legend()
plt.show()

绘制结果:


图片.png

如果有多个类型,我们只需要更改n值即可。

相关文章

网友评论

    本文标题:Matplotlib->柱状图(bar)度量值

    本文链接:https://www.haomeiwen.com/subject/octunctx.html