大家好,我是带我去滑雪!
子图可以更清晰地展示和理解复杂的数据关系,通过将数据分成多个小图,有助于观察数据间的关系和趋势。减少数据之间的重叠和混淆,使得每个子图更易于理解和解释。不同类型的子图可以呈现数据的不同方面。例如,旭日图可以展示层次数据的结构,渐变堆积面积图可以显示时间序列数据的变化,多数据折线图可以比较多个数据集的趋势,比例关系图可以展示数据之间的相对比例关系。下面开始代码实战。
目录
(1)导入相关库与模块
(2)分别绘制子图
(3)保存图像到指定文件夹
(1)导入相关库与模块
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
# 设置中文字体和负号
plt.rcParams['font.sans-serif'] = ['KaiTi']
plt.rcParams['axes.unicode_minus'] = False
(2)分别绘制子图
plt.rcParams['font.sans-serif'] = ['KaiTi']
plt.rcParams['axes.unicode_minus'] = False
data1 = {
'景区名称': ['荔波小七孔', '黄果树瀑布', '镇远古城', '梵净山', '安顺龙宫', '乌蒙大草原', '百里杜鹃', '千户苗寨'],
'评分': [4.7, 4.5, 4.5, 4.4, 4.4, 4.4, 4.3, 4.2]
}
data2 = {
'年份': [2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023],
'旅游人数': [4747.89, 6262.89, 8190.23, 10439.95, 12913.02, 17019.36, 21401.18, 26761.28, 32134.94, 37630.01, 53148.42, 74417.43, 96858.12, 113526.6, 61781.49, 64436.68, 49206.88, 63558.44],
'旅游收入': [387.05, 512.28, 653.13, 805.23, 1061.23, 1429.48, 1860.16, 2370.65, 2895.98, 3512.82, 5027.54, 7116.81, 9471.03, 12318.86, 5785.09, 6642.16, 5245.64, 7404.56],
'人数增长率': [53.21, 31.91, 30.77, 27.47, 23.69, 31.80, 25.75, 25.05, 20.08, 17.10, 41.24, 40.02, 30.16, 17.21, -45.58, 4.30, -23.64, 29.17],
'旅游收入增长率': [59.39, 32.35, 27.49, 23.29, 31.79, 34.70, 30.13, 27.44, 22.16, 21.30, 43.12, 41.56, 33.08, 30.07, -53.04, 14.82, -21.03, 41.16]
}
data3 = {
'Year': [2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023],
'人数增长率': [53.21, 31.91, 30.77, 27.47, 23.69, 31.80, 25.75, 25.05, 20.08, 17.10, 41.24, 40.02, 30.16, 17.21, -45.58, 4.30, -23.64, 29.17],
'旅游收入增长率': [59.39, 32.35, 27.49, 23.29, 31.79, 34.70, 30.13, 27.44, 22.16, 21.30, 43.12, 41.56, 33.08, 30.07, -53.04, 14.82, -21.03, 41.16],
'人均旅游收入增长率': [4.04, 0.34, -2.51, -3.28, 6.55, 2.20, 3.49, 1.92, 1.73, 3.59, 1.33, 1.10, 2.25, 10.97, -13.71, 10.08, 3.42, 9.28]
}
data4 = {
'评分': ['1', '2', '差评', '3', '中评', '4', '5', '好评'],
'安顺龙宫比例': [5, 3, 8, 9, 0, 19, 63, 83],
'大方百里杜鹃比例': [1, 1, 1, 8, 0, 21, 70, 91],
'黄果树瀑布比例': [3, 2, 5, 4, 0, 13, 78, 91],
'江口梵净山比例': [6, 2, 9, 6, 0, 15, 70, 85],
'荔波小七孔比例': [1, 0, 1, 4, 0, 11, 84, 95],
'乌蒙大草原比例': [1, 1, 2, 8, 0, 20, 70, 90],
'西江千户苗寨比例': [7, 4, 11, 15, 0, 22, 52, 73],
'镇远古城比例': [4, 2, 6, 8, 0, 21, 66, 87]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
df3 = pd.DataFrame(data3)
df4 = pd.DataFrame(data4)
fig, axs = plt.subplots(2, 2, figsize=(16, 12),dpi=300)
axs[0, 0].set_title('景区评分旭日图', fontsize=18)
colors = ['#ff4500', '#ff6347', '#ff7f50', '#ff8c00', '#ff4500', '#ff6347', '#ff7f50', '#ff8c00']
wedges, texts, autotexts = axs[0, 0].pie(df1['评分'], autopct='%1.1f%%', startangle=140, colors=colors,
pctdistance=0.85, wedgeprops=dict(width=0.4, edgecolor='w'), labels=df1['景区名称'])
axs[0, 0].axis('equal')
centre_circle = plt.Circle((0,0),0.70,fc='white')
axs[0, 0].add_artist(centre_circle)
for text, color in zip(texts, colors):
text.set_color(color)
text.set_fontsize(12)
for autotext in autotexts:
autotext.set_color('white')
autotext.set_fontsize(18)
for wedge in wedges:
wedge.set_edgecolor('black')
axs[0, 1].set_title('旅游数据渐变堆叠面积图', fontsize=18)
x = df2['年份'].astype(int) # 将年份数据转换为整数
colors = plt.cm.viridis(np.linspace(0, 1, len(df2.columns) - 1)) # 使用viridis颜色映射
for i, col in enumerate(df2.columns[1:]):
y = df2[col]
axs[0, 1].fill_between(x, 0, y, alpha=0.7, color=colors[i], label=col)
axs[0, 1].legend(title='指标', loc='upper left')
axs[0, 1].xaxis.set_major_locator(plt.MaxNLocator(integer=True))
axs[0, 1].set_xticks(x) # 设置x轴刻度为年份
axs[1, 0].set_title('增长率排序折线图', fontsize=18)
x = df3['Year'].astype(int) # 将年份数据转换为整数
axs[1, 0].plot(x, df3['人数增长率'], marker='o', label='人数增长率', linestyle='-', color='blue')
axs[1, 0].plot(x, df3['旅游收入增长率'], marker='o', label='旅游收入增长率', linestyle='-', color='orange')
axs[1, 0].plot(x, df3['人均旅游收入增长率'], marker='o', label='人均旅游收入增长率', linestyle='-', color='green')
axs[1, 0].legend()
axs[1, 0].xaxis.set_major_locator(plt.MaxNLocator(integer=True))
axs[1, 0].set_xticks(x) # 设置x轴刻度为年份
axs[1, 1].set_title('评分与各景点比例关系图', fontsize=18)
for column in df4.columns[1:]:
axs[1, 1].plot(df4['评分'], df4[column], marker='o', label=column)
axs[1, 1].legend()
(3)保存图像到指定文件夹
plt.tight_layout()
plt.savefig('visualization.png', dpi=300, bbox_inches='tight')
plt.savefig(r'E:\工作\硕士\博客\博客86-python绘制子图\子图.png',
bbox_inches ="tight",
pad_inches = 1,
transparent = True,
facecolor ="w",
edgecolor ='w',
dpi=300,
orientation ='landscape')
输出结果展示:
需要数据集的家人们可以去百度网盘(永久有效)获取:
链接:https://pan.baidu.com/s/173deLlgLYUz789M3KHYw-Q?pwd=0ly6
提取码:2138
更多优质内容持续发布中,请移步主页查看。
博主的WeChat:TCB1736732074
点赞+关注,下次不迷路!