官方文档
英文文档: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()
运行一下看下结果
以上都是条形图的绘制,更多配置请移至官方文档。