数据可视化---共享子图

文章详细介绍了如何使用Python的matplotlib库创建共享非相邻和相邻子图的坐标轴图,以及如何在同一个画布上展示多个子图,包括不同类型的图表如柱状图、折线图和饼图,用于数据可视化和分析。
摘要由CSDN通过智能技术生成

题目一:

编写程序。根据5.3.2,绘制共享非相邻子图的坐标轴图。

运行代码:

#绘制共享非相邻子图的坐标轴图
import numpy as np
import matplotlib.pyplot as plt

#数据准备
x1 = np.linspace(0.2*np.pi, 400)
y1 = np.cos(x1**2)
x2 = np.linspace(0.01, 10, 100)
y2 = np.sin(x2)
ax_one = plt.subplot(221)
ax_one.plot(x1, y1)
#共享子图ax_one和ax_two的x轴
ax_two = plt.subplot(224, sharex = ax_one)
ax_two.plot(x2, y2)
plt.show()

运行结果:

题目

编写程序。根据5.3.2,绘制共享同一子图的坐标轴图

运行代码:

#绘制共享同一子图的坐标轴图
import numpy as np
import matplotlib.pyplot as plt

fig, ax1 = plt.subplots(1, 1)
ax2 = ax1.twinx()
#作y=sin(x)函数
x1 = np.linspace(-4*np.pi, 4*np.pi, 256)
y1 = np.sin(x1)
ax1.plot(x1, y1, color='red')
#作y=sin(x)函数
x2 = np.linspace(-4*np.pi, 4*np.pi, 256)
y2 = np.cos(x2)
ax1.plot(x2, y2, color='green')
plt.show()

运行结果:

题目

编写程序。根据实例3的要求,绘制展示2017与2018年抖音用户分析的多个子图,实现过程如下:

  1. 导入matplotlib.pyplot模块;
  2. 准备数据;
  3. 将画布规划为3*2的矩阵区域;
  4. 在横跨2个、纵跨2个的区域中绘制第1个子图;
  5. 在位于第2行第0列的区域中绘制第2个子图;
  6. 在位于第2行第1列的区域中绘制第3个子图;;
  7. 调整子图之间的距离。

运行代码:

#绘制展示2017与2018年抖音用户分析的多个子图
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
data_2017 = np.array([21, 35, 22, 19, 3])
data_2018 = np.array([13, 32, 27, 27, 1])

x = np.arange(5)
y = np.array([51, 73, 99, 132, 45])
labels = np.array(['一线城市', '二线城市', '三线城市', '四线及以外', '其他国家及地区'])

average = 75
bar_width = 0.5

#添加无指向型注释文本
def autolabel(ax, rects):
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + bar_width/2, height+3, s='{}'.format(height), ha='center', va='bottom')

#第一个子图
ax_one = plt.subplot2grid((3, 2), (0, 0), rowspan=2, colspan=2)
bar_rects = ax_one.bar(x, y, tick_label=labels, color='#20B2AA', width=bar_width)
ax_one.set_title('抖音2018vs2017人群增长倍数')
ax_one.set_ylabel('增长倍数')
autolabel(ax_one, bar_rects)
ax_one.set_ylim(0, y.max() + 20)
ax_one.axhline(y=75, linestyle='--', linewidth=1, color='gray')
# 第2个子图
ax_two = plt.subplot2grid((3, 2), (2, 0))
ax_two.pie(data_2017, radius=1.5, labels=labels, autopct='%3.1f %%',
           colors=['#2F4F4F', '#00ff00', '#A9A9A9', '#FFD700', '#B0C4DE'])
ax_two.set_title('2017年抖音用户地区分布的比例')
# 第3个子图
ax_thr = plt.subplot2grid((3, 2), (2, 1))
ax_thr.pie(data_2018, radius=1.5, labels=labels, autopct='%3.1f %%',
           colors=['#2F4F4F', '#FF0000', '#A9A9A9', '#FFD700', '#B0C4DE'])
ax_thr.set_title('2018年抖音用户地区分布的比例')
# 调整子图之间的距离
plt.tight_layout()
plt.show()

运行结果:

题目

编写程序。根据实例4的要求,绘制一个展示某地区全年气温和水量的关系的图表,实现过程如下:

  1. 导入matplotlib.pyplot模块;
  2. 准备数据;
  3. 在整个画布区域中绘制一个说明降水量与蒸发量的堆积柱形图;
  4. 在同一区域中绘制一个说明全年平均气温的折线图。

运行代码:

# 实例9某地区全年平均气温降水量、蒸发量的关系
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
month_x = np.arange(1, 13, 1)
# 平均气温
data_tem = np.array([2.0, 2.2, 3.3, 4.5, 6.3, 10.2,
                     20.3, 33.4, 23.0, 16.5, 12.0, 6.2])
# 降水量
data_precipitation = np.array([2.6, 5.9, 9.0, 26.4, 28.7, 70.7,
                               175.6, 182.2, 48.7, 18.8, 6.0, 2.3])
# 蒸发量
data_evaporation = np.array([2.0, 4.9, 7.0, 23.2, 25.6, 76.7,
                             135.6, 162.2, 32.6, 20.0, 6.4, 3.3])
fig, ax = plt.subplots()
bar_ev = ax.bar(month_x, data_evaporation, color='orange', tick_label=['1月', '2月', '3月', '4月', '5月', '6月',
                                                                       '7月', '8月', '9月', '10月', '11月', '12月'])
bar_pre = ax.bar(month_x, data_precipitation, bottom=data_evaporation, color='green')
ax.set_ylabel('水量 (ml)')
ax.set_title('平均气温与降水量、蒸发量的关系')
ax_right = ax.twinx()
line = ax_right.plot(month_x, data_tem, 'o-m')
ax_right.set_ylabel('气温($^\circ$C)')
# 添加图例
plt.legend([bar_ev, bar_pre, line[0]], ['蒸发量', '降水量', '平均气温'],
           shadow=True, fancybox=True)
plt.show()

运行结果:

题目

编写程序。根据实例5的要求,绘制一个展示2018上半年某品牌汽车销售情况的多个子图,实现过程如下:

  1. 导入matplotlib.pyplot模块;
  2. 准备数据;
  3. 在第0行第0~1列的区域中,绘制反映2018上半年汽车销售额的柱形图;
  4. 在第1行第0列和第1行第1列的区域中,绘制反映2018上半年各分公司汽车销量的折线图和堆积面积图。

运行代码:

#实例12:2018年上半年某汽车品牌汽车销售情况
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
plt.rcParams["font.sans-serif"] = ["SimHei"]
x_month = np.array(['1月', '2月', '3月', '4月', '5月', '6月'])
y_sales = np.array([2150, 1050, 1560, 1480, 1530, 1490])
x_citys = np.array(['北京', '上海', '广州', '深圳', '浙江', '山东'])
y_sale_count = np.array([83775, 62860, 59176, 64205, 48671, 39968])
# 创建画布和布局
fig = plt.figure(constrained_layout=True)
gs = fig.add_gridspec(2, 2)
ax_one = fig.add_subplot(gs[0, :])
ax_two = fig.add_subplot(gs[1, 0])
ax_thr = fig.add_subplot(gs[1, 1])
# 第1个子图\n",
ax_one.bar(x_month, y_sales, width=0.5, color='#3299CC')
ax_one.set_title('2018年上半年某品牌汽车的销售额')
ax_one.set_ylabel('销售额(亿元)')
# 第2个子图
ax_two.plot(x_citys, y_sale_count, 'm--o', ms=8)
ax_two.set_title('分公司某品牌汽车的销量')
ax_two.set_ylabel('销量(辆)')
# 第3个子图
ax_thr.stackplot(x_citys, y_sale_count, color='#9999FF')
ax_thr.set_title('分公司某品牌汽车的销量')
ax_thr.set_ylabel('销量(辆)')
plt.show()

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值