matplotlib 入门之条形图(一)

官方文档

英文文档:matplotlib.pyplot — Matplotlib 3.7.1 documentation

中文文档:API Reference_Matplotlib 中文网

先绘制个条形图

import matplotlib.pyplot as plt

# 数据
x = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
height = [289, 293, 381, 128, 376, 298, 239]

# 设置风格
plt.style.use('ggplot')
# 设置标题
plt.title('本周商品销售情况')
# 设置轴标签名
plt.xlabel(xlabel='日期')
plt.ylabel(ylabel='销售量(个)')
# 设置图形数据
plt.bar(x=x, height=height, width=0.5)
# 解决中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
# 显示图形
plt.show()

运行一下看下结果

绘制个横向条形图

import matplotlib.pyplot as plt

# 数据
y = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
width = [289, 293, 381, 128, 376, 298, 239]

# 设置风格
plt.style.use('ggplot')
# 设置标题
plt.title('本周商品销售情况')
# 设置轴标签名
plt.xlabel(xlabel='销售量')
plt.ylabel(ylabel='日期')
# 设置图形数据
plt.barh(y=y, width=width, height=0.5)
# 解决中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
# 显示图形
plt.show()

运行一下看下结果

绘制个堆叠条形图

import matplotlib.pyplot as plt

# 数据
x = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
height1 = [289, 293, 381, 128, 376, 298, 239]
height2 = [90, 27, 79, 48, 59, 38, 28]

# 设置风格
plt.style.use('ggplot')
# 设置标题
plt.title('本周商品销售情况')
# 设置轴标签名
plt.xlabel(xlabel='日期')
plt.ylabel(ylabel='销售量(个)')
# 设置图形数据
plt.bar(x=x, height=height1, width=0.5)
plt.bar(x=x, height=height2,bottom=height1, width=0.5)
# 解决中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
# 显示图形
plt.show()

运行一下看下结果

以上都是条形图的绘制,更多配置请移至官方文档。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值