用 Python 的 numpy 和 matplotlib 库来绘制图表。

1、导入 numpy 和 matplotlib.pyplot 模块

import numpy as np
import matplotlib.pyplot as plt

2、设置字体为 SimHei,以支持中文显示。

plt.rcParams['font.sans-serif'] = ["SimHei"]

3、定义 x 轴和 y 轴的数据,分别表示月份和销售额。y 轴的数据有两组,分别对应产品 A 和产品 B和定义标签列表,用于显示月份的中文名称。

x = [x for x in range(1, 13)]
y1 = [20, 28, 23, 16, 29, 36, 39, 33, 31, 19, 21, 25]
y2 = [17, 22, 39, 26, 35, 23, 25, 27, 29, 38, 28, 20]
labels = ['1 月', '2 月', '3 月', '4 月', '5 月', '6 月', '7月', '8 月', '9 月', '10 月', '11 月', '12 月']

4、创建一个两行四列的子图布局

plt.figure(figsize=(12, 6))

5、绘制上方的大图,使用 subplot2grid 函数指定位置为第 0 行第 1 列,跨越两列。

ax1 = plt.subplot2grid((2, 4), (0, 1), colspan=2)
ax1.plot(x, y1, 'm--o', lw=2, ms=5, label='产品A')
ax1.plot(x, y2, 'g--o', lw=2, ms=5, label='产品B')
ax1.set_title("产品A 与产品B的销售额", fontsize=11)
ax1.set_ylim(10, 45)
ax1.set_ylabel('销售额(亿元)')
ax1.set_xlabel('月份')
for xy1 in zip(x, y1):
    ax1.annotate("%s" % xy1[1], xy=xy1, xytext=(-5, 5), textcoords='offset points')
for xy2 in zip(x, y2):
    ax1.annotate("%s" % xy2[1], xy=xy2, xytext=(-5, 5), textcoords='offset points')
ax1.legend()

6、绘制下方的圆形子图,使用 subplot2grid 函数指定位置为第 1 行第 0 列和第 1 行第 3 列。使用 pie 函数绘制两个饼图,分别用不同的颜色表示产品 A 和产品 B 的销售额占比。设置标题、标签、百分比显示和饼图的宽度。

# 绘制下方的圆形子图
ax2 = plt.subplot2grid((2, 4), (1, 0))
ax2.pie(y1, radius=1, wedgeprops={'width': 0.5}, labels=labels, autopct='%3.1f%%', pctdistance=0.75)
ax2.set_title('产品A的销售额 ')

# 绘制下方的圆形子图
ax3 = plt.subplot2grid((2, 4), (1, 3))
ax3.pie(y2, radius=1, wedgeprops={'width': 0.5}, labels=labels, autopct='%3.1f%%', pctdistance=0.75)
ax3.set_title('产品B的销售额 ')

7、调用 tight_layout 函数调整子图之间的距离,使得图表更加美观。

plt.tight_layout()

8、调用 show 函数显示图表

plt.show()

以下是完整代码示例:、

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ["SimHei"]

x = [x for x in range(1, 13)]
y1 = [20, 28, 23, 16, 29, 36, 39, 33, 31, 19, 21, 25]
y2 = [17, 22, 39, 26, 35, 23, 25, 27, 29, 38, 28, 20]
labels = ['1 月', '2 月', '3 月', '4 月', '5 月', '6 月', '7月', '8 月', '9 月', '10 月', '11 月', '12 月']

# 创建一个两行四列的子图布局
plt.figure(figsize=(12, 6))

# 绘制上方的大图
ax1 = plt.subplot2grid((2, 4), (0, 1), colspan=2)
ax1.plot(x, y1, 'm--o', lw=2, ms=5, label='产品A')
ax1.plot(x, y2, 'g--o', lw=2, ms=5, label='产品B')
ax1.set_title("产品A 与产品B的销售额", fontsize=11)
ax1.set_ylim(10, 45)
ax1.set_ylabel('销售额(亿元)')
ax1.set_xlabel('月份')
for xy1 in zip(x, y1):
    ax1.annotate("%s" % xy1[1], xy=xy1, xytext=(-5, 5), textcoords='offset points')
for xy2 in zip(x, y2):
    ax1.annotate("%s" % xy2[1], xy=xy2, xytext=(-5, 5), textcoords='offset points')
ax1.legend()

# 绘制下方的圆形子图
ax2 = plt.subplot2grid((2, 4), (1, 0))
ax2.pie(y1, radius=1, wedgeprops={'width': 0.5}, labels=labels, autopct='%3.1f%%', pctdistance=0.75)
ax2.set_title('产品A的销售额 ')

# 绘制下方的圆形子图
ax3 = plt.subplot2grid((2, 4), (1, 3))
ax3.pie(y2, radius=1, wedgeprops={'width': 0.5}, labels=labels, autopct='%3.1f%%', pctdistance=0.75)
ax3.set_title('产品B的销售额 ')

# 调整子图之间的距离
plt.tight_layout()
plt.show()

一下是运行图示例、

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值