- 阿里云月度账单
- 根据月份、消费金额(可开票)生成一个折线图表
import pandas as pd
import matplotlib.pyplot as plt
import os
from calendar import month_abbr
plt.rcParams['font.sans-serif'] = ['SimHei']
csv_files = [file for file in os.listdir() if file.endswith('.csv')]
fig, ax = plt.subplots(figsize=(10, 6))
for file in csv_files:
df = pd.read_csv(file)
df['账期'] = pd.to_datetime(df['账期'].str.strip(), format='%Y-%m')
df['月份'] = df['账期'].dt.month.map(lambda x: month_abbr[x])
df['消费(可开票)'] = df['消费(可开票)'].fillna(0)
if not (df['消费(可开票)'] == 0).all():
line = ax.plot(df['账期'], df['消费(可开票)'], label=file[:-4], marker='o')[0]
ax.set_title('年度账单消费情况(阿里云)')
ax.set_xlabel('月份')
ax.set_ylabel('消费(可开票)')
ax.set_ylim(bottom=0)
ax.set_xticks(df['账期'])
ax.set_xticklabels(df['月份'])
for y_tick in ax.get_yticks():
ax.hlines(y=y_tick, xmin=df['账期'].min(), xmax=df['账期'].max(), colors='gray', linestyles='dashed', alpha=0.5, linewidth=0.5)
ax.legend()
plt.savefig('line_chart.png')
plt.show()