python连数据库给每个商家按月的交易量变化作折线图 怎么做多个图啊
给你写了一个例子,我根据交易数据,按商家ID分组计算了每个商家每个月的交易量,在各自为商家绘制图:
import matplotlib.pyplot as plt
import pandas as pd
data = pd.DataFrame({
'商家ID': [1, 1, 2, 2, 3, 3, 3],
'交易日期': ['2023-01-01', '2023-02-01', '2023-01-01', '2023-02-01', '2023-01-01', '2023-02-01', '2023-03-01'],
'交易量': [100, 200, 150, 250, 120, 180, 300]
})
data['交易日期'] = pd.to_datetime(data['交易日期'])
monthly_data = data.groupby(['商家ID', pd.Grouper(key='交易日期', freq='M')]).sum().reset_index()
merchant_ids = monthly_data['商家ID'].unique()
fig, ax = plt.subplots()
for merchant_id in merchant_ids:
merchant_data = monthly_data[monthly_data['商家ID'] == merchant_id]
ax.plot(merchant_data['交易日期'], merchant_data['交易量'], label=f'商家{merchant_id}')
ax.set_title('商家交易量变化')
ax.set_xlabel('日期')
ax.set_ylabel('交易量')
ax.legend()
plt.show()
参考 https://blog.csdn.net/weixin_43719743/article/details/87359623
resources_file_path = ‘/resources/movie/cinemaNameList.ini’
scratch_url = ‘http://theater.mtime.com/China_Beijing/’