04-matplotlib-柱形图

import numpy as np
import matplotlib.pyplot as plt

# 柱形图
# 例一
N =5
y =  [15,28,10,30,25]
index = np.arange(N)
p = plt.bar(index,height=y)
plt.show()

# 例2
p1 = plt.bar(0,bottom=index,width=y,height=0.5,align='edge',color='red',orientation='horizontal')
plt.show()

p2 = plt.barh(index,width= y , align='edge',color ='green',height=0.5)
plt.show()

# 例3
sales_BJ = [52,55,63,53]
sales_SH = [44,66,55,41]

index = np.arange(4)
bar_width = 0.3

# 竖着显示
plt.bar(index,sales_BJ,bar_width)
plt.bar(index+bar_width,sales_SH,bar_width,color='r')
plt.show()

# 横着显示
plt.barh(index,sales_BJ,bar_width)
plt.barh(index+bar_width,sales_SH,bar_width,color='r')
plt.show()

# 层叠图
plt.bar(index,sales_BJ,bar_width)
plt.bar(index,sales_SH,bar_width,color='r',bottom=sales_BJ)
plt.show()

# 练习
'''
    生成两组大小为5的数据;
    画出两组数据 水平的条形图;
    采用并列,层叠两种方式;
    
'''

N =5
n1 = np.random.randint(1,100,N)
n2 = np.random.randint(1,100,N)

index = np.arange(N)
bar_width = 0.3

# 并列显示
plt.bar(index,n1,bar_width)
plt.bar(index+bar_width,n2,bar_width,color='r')
plt.show()

# 层叠显示
plt.bar(index,n1,bar_width)
plt.bar(index,n2,bar_width,color='r',bottom=n1)
plt.show()

 

 

 

 

在未来面前,我们永远都是孩子。不断思考,不断学习,才能让我们走的更远。

个人主页:https://www.oceaneyes.cn/

个人学习博客:http://oceaneyes.top/

CSDN:https://blog.csdn.net/qq_16123129 

 长按二维码关注,一起交流学习~~~

posted @ 2019-03-17 18:47  OCEANEYES.GZY  阅读(96)  评论(0编辑  收藏  举报