In [1]:
import pandas as pd
# 读取文件
excel_file = pd.ExcelFile('./中国汽车分厂商每月销售表.xlsx')
df = excel_file.parse('Sheet1')
In [2]:
import matplotlib.pyplot as plt
# 按照年份和月份对销量进行分组统计
grouped_data = df.groupby(['年份', '月份'])['销量'].sum().unstack(fill_value=0)
# 设置图片清晰度
plt.rcParams['figure.dpi'] = 300
# 设置中文字体为黑体
plt.rcParams['font.sans-serif'] = ['SimHei']
# 创建一个画布,包含三个子图
fig, axes = plt.subplots(3, 1, figsize=(10, 15))
# 绘制折线图
grouped_data.plot(ax=axes[0])
axes[0].set_title('各年份和月份的销量折线图')
axes[0].set_xlabel('年份')
axes[0].set_ylabel('销量')
axes[0].tick_params(axis='x', rotation=45)
# 绘制柱状图
grouped_data.plot(kind='bar', ax=axes[1])
axes[1].set_title('各年份和月份的销量柱状图')
axes[1].set_xlabel('年份和月份')
axes[1].set_ylabel('销量')
# 绘制饼图(统计所有数据的总销量按年份和月份分布)
total_sales_by_year_month = grouped_data.sum().sum()
grouped_data_sum = grouped_data.sum()
axes[2].pie(grouped_data_sum, labels=grouped_data_sum.index, autopct='%1.1f%%')
axes[2].set_title('各年份和月份销量占总销量的比例')
plt.tight_layout()
plt.show()
In [ ]:
In [3]:
# 读取文件
excel_file = pd.ExcelFile('./中国汽车分车型每月销售量.xlsx')
# 获取指定工作表中的数据
df = excel_file.parse()
In [4]:
from wordcloud import WordCloud
import matplotlib.pyplot as plt
font_path = 'C:/Windows/Fonts/simhei.ttf'
# 创建词云对象并生成词云
wordcloud = WordCloud(width=800, height=400, background_color='white', font_path=font_path).generate(' '.join(df['车型'].astype(str)))
# 设置图片清晰度
plt.rcParams['figure.dpi'] = 300
# 显示词云图
plt.figure(figsize=(10, 5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.title('车型词云图')
plt.show()
In [5]:
# 按照年份和月份对销量进行分组统计
grouped_data = df.groupby(['年份', '月份'])['销量'].sum().unstack(fill_value=0)
# 创建一个画布,包含三个子图
fig, axes = plt.subplots(3, 1, figsize=(10, 15))
# 绘制箱线图
grouped_data.plot.box(ax=axes[0])
axes[0].set_title('各年份和月份销量的箱线图')
axes[0].set_xlabel('年份和月份')
axes[0].set_ylabel('销量')
# 绘制散点图
for year in grouped_data.index:
axes[1].scatter([year] * len(grouped_data.columns), grouped_data.loc[year], label=str(year))
axes[1].set_title('各年份和月份销量的散点图')
axes[1].set_xlabel('年份')
axes[1].set_ylabel('销量')
axes[1].legend()
# 绘制面积图
grouped_data.plot.area(ax=axes[2])
axes[2].set_title('各年份和月份销量的面积图')
axes[2].set_xlabel('年份')
axes[2].set_ylabel('销量')
plt.tight_layout()
plt.show()