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
长按二维码关注,一起交流学习~~~
本文来自博客园,作者:OCEANEYES.GZY,转载请注明原文链接:https://www.cnblogs.com/oceaneyes-gzy/p/12317401.html
关于作者
👋 读书城南,🤔 在未来面前,我们都是孩子~
- 📙 一个热衷于探索学习新方向、新事物的智能产品经理,闲暇时间喜欢coding💻、画图🎨、音乐🎵、学习ing~
👋 Social Media
🛠️ Blog: http://oceaneyes.top
⚡ PM导航: https://pmhub.oceangzy.top
☘️ CNBLOG: https://www.cnblogs.com/oceaneyes-gzy/
🌱 AI PRJ自己部署的一些算法demo: http://ai.oceangzy.top/
📫 Email: 1450136519@qq.com
💬 WeChat: OCEANGZY
- 💬 公众号: UncleJoker-GZY
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步