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 

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OceanEyes.GZY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值