在多个图上绘制多列分组

luc*_*usm 2 python matplotlib python-3.x pandas subplot

我有这样的数据

ID    value_y   date_x      end_cutoff
1      75     2020-7-1      2021-01-17
1      73     2020-7-2      2021-01-17
1      74     2020-7-1      2021-06-05
1      71     2020-7-2      2021-06-05
2      111    2020-7-1      2021-01-17
2      112    2020-7-2      2021-01-17
2      113    2020-7-1      2021-06-05
2      115    2020-7-2      2021-06-05
   
Run Code Online (Sandbox Code Playgroud)

我想绘制以下数据以满足以下条件:

  1. 每个ID有1张图
  2. 每个图表都绘制了 n 条线(本例中为 2 条;每个 end_cutoff 为 1 条)

因此,理想情况下,在这个示例中,我将有两个单独的图,每个图都有两条线。

目前,这是我拥有的代码,但它将它们全部绘制在同一个图上,而不是为每个 ID 绘制一个新图。

 grouped = df_fit.groupby(['ID','end_cutoff'])
 fig, ax = plt.subplots()
 for (ID, end_cutoff), df_fit in grouped:
     ax.plot(df_fit['date_x'], df_fit['value_y'], label=ID+' '+str(end_cutoff.date()))
 plt.show()
Run Code Online (Sandbox Code Playgroud)

Tre*_*ney 5

  • 该解决方案将缺失的部分添加到您现有的代码中
  1. 将日期列正确格式化为 a datetime dtype,并仅提取日期部分。
  2. 创建数量等于唯一'ID'值数量的子图
  3. ID获取内部索引uid并使用该值进行索引并绘制到正确的位置ax
  • 该选项使用pandas.DataFrame.plot
  • x 轴的格式是'%m-%d %H'因为点之间的时间很小。X 轴将根据日期范围自动设置格式。
import pandas as pd
import numpy as np

# dataframe
data = {'ID': [1, 1, 1, 1, 2, 2, 2, 2], 'value_y': [75, 73, 74, 71, 111, 112, 113, 115], 'date_x': ['2020-7-1', '2020-7-2', '2020-7-1', '2020-7-2', '2020-7-1', '2020-7-2', '2020-7-1', '2020-7-2'], 'end_cutoff': ['2021-01-17', '2021-01-17', '2021-06-05', '2021-06-05', '2021-01-17', '2021-01-17', '2021-06-05', '2021-06-05']}
df = pd.DataFrame(data)

# set date columns to a datetime dtype and extract only the date component since time isn't relevant
df['end_cutoff'] =  pd.to_datetime(df['end_cutoff']).dt.date
df['date_x'] =  pd.to_datetime(df['date_x']).dt.date

# create grouped
grouped = df.groupby(['ID','end_cutoff'])

# create subplots based on the number of unique ID values
uid = df.ID.unique()
fig, ax = plt.subplots(nrows=len(uid), figsize=(7, 4))

for (ID, end_cutoff), df_fit in grouped:
    
    # get the index of the current ID, and use it to index ax
    axi = np.argwhere(uid==ID)[0][0]

    # plot to the correct ax based on the index of the ID
    df_fit.plot(x='date_x', y='value_y', ax=ax[axi], label=f'{ID} {end_cutoff}',
                xlabel='Date', ylabel='Value', title=f'ID: {ID}', marker='.', rot=30)

    # place the legend outside the plot
    ax[axi].legend(title='Cutoff', bbox_to_anchor=(1.05, 1), loc='upper left')

plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述