parent
f247d00560
commit
173369141e
@ -0,0 +1,83 @@
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib.font_manager as font_manager
|
||||
|
||||
def set_chinese_font():
|
||||
# 设置中文字体
|
||||
font_path = "C:\\Windows\\Fonts\\simsun.ttc" # 宋体
|
||||
prop = font_manager.FontProperties(fname=font_path)
|
||||
return prop
|
||||
|
||||
def convert_time(df):
|
||||
df['时长'] = pd.to_datetime(df_hk['时长'], format='%M:%S').dt.minute * 60 + pd.to_datetime(df_hk['时长'], format='%M:%S').dt.second
|
||||
return df
|
||||
|
||||
def barh_chart(df, title):
|
||||
prop = set_chinese_font()
|
||||
convert_time(df)
|
||||
# 绘制水平条形图
|
||||
plt.figure(figsize=(10, 8))
|
||||
plt.barh(range(1, len(df) + 1), df['时长'], color='b')
|
||||
plt.title(title, fontproperties=prop)
|
||||
plt.xlabel('歌曲时长(秒)', fontproperties=prop)
|
||||
plt.ylabel('歌曲', fontproperties=prop)
|
||||
# 设置横坐标刻度和标签
|
||||
max_duration = df['时长'].max()
|
||||
xticks = range(0, max_duration + 1, 10)
|
||||
plt.xticks(xticks, ['{:02d}'.format(x) for x in xticks], fontproperties=prop, rotation=45)
|
||||
# 设置纵坐标标签
|
||||
plt.yticks(range(1, len(df) + 1), df['歌曲'], fontproperties=prop)
|
||||
plt.tight_layout()
|
||||
plt.show()
|
||||
|
||||
def pie_chart(df, title):
|
||||
prop = set_chinese_font()
|
||||
convert_time(df)
|
||||
# 绘制饼图
|
||||
plt.figure(figsize=(10, 8))
|
||||
plt.pie(df['时长'], labels=df['歌曲'], autopct='%1.1f%%', startangle=140)
|
||||
plt.title(title, fontproperties=prop)
|
||||
plt.axis('equal') # 保证饼图是圆形的
|
||||
plt.tight_layout()
|
||||
plt.show()
|
||||
|
||||
def line_chart(df, title):
|
||||
prop = set_chinese_font()
|
||||
convert_time(df)
|
||||
# 绘制折线图
|
||||
plt.figure(figsize=(10, 8))
|
||||
plt.plot(range(1, len(df) + 1), df['时长'], marker='o', color='y', linestyle='-')
|
||||
plt.title(title, fontproperties=prop)
|
||||
plt.xlabel('歌曲', fontproperties=prop)
|
||||
plt.ylabel('歌曲时长(秒)', fontproperties=prop)
|
||||
plt.xticks(range(1, len(df) + 1), df['歌曲'], rotation=90, fontproperties=prop)
|
||||
plt.grid(True)
|
||||
plt.tight_layout()
|
||||
plt.show()
|
||||
|
||||
def scatter_chart(df, title):
|
||||
prop = set_chinese_font()
|
||||
convert_time(df)
|
||||
# 绘制散点图
|
||||
plt.figure(figsize=(10, 8))
|
||||
plt.scatter(range(1, len(df) + 1), df['时长'], color='pink')
|
||||
plt.title(title, fontproperties=prop)
|
||||
plt.xlabel('歌曲', fontproperties=prop)
|
||||
plt.ylabel('歌曲时长(秒)', fontproperties=prop)
|
||||
plt.xticks(range(1, len(df) + 1), df['歌曲'], rotation=90, fontproperties=prop)
|
||||
plt.grid(True)
|
||||
plt.tight_layout()
|
||||
plt.show()
|
||||
|
||||
|
||||
# 读取 Excel 文件
|
||||
df_hk = pd.read_excel("C:\\Users\\lenovo\\pythonProject1\\香港2.0.xls")
|
||||
df_western = pd.read_excel('C:\\Users\\lenovo\\pythonProject1\\欧美2.0.xls')
|
||||
df_korea = pd.read_excel('C:\\Users\\lenovo\\pythonProject1\\韩国2.0.xls')
|
||||
df_tw = pd.read_excel('C:\\Users\\lenovo\\pythonProject1\\台湾2.0.xls')
|
||||
|
||||
# 绘制
|
||||
pie_chart(df_western, '欧美歌曲时长变化')
|
||||
line_chart(df_tw, '台湾歌曲时长变化')
|
||||
scatter_chart(df_korea, '韩国歌曲时长变化')
|
||||
barh_chart(df_hk, '香港歌曲时长变化')
|
Loading…
Reference in new issue