如何在每个 id 的面板数据上生成 2-yaxis 图?

How to generate 2-yaxis graphs on a panel data per id?

我有一个数据集,df 看起来像这样:

Date Code City State Quantity x Quantity y Population Cases Deaths
2019-01 10001 Los Angeles CA 445 0 0
2019-01 10002 Sacramento CA 4450 556 0 0
2020-03 12223 Houston TX 440 4440 35000000 23 11
... ... ... ... ... ... ... ... ...
2021-07 10002 Sacramento CA 3220 NA 5444000 211 22

我的开始和结束日期在所有城市都是一样的。我有超过 4000 个不同的城市,并且想为每个城市绘制一个 2-yaxis 图,使用类似于以下代码的内容:

import matplotlib.pyplot as plt

fig, ax1 = plt.subplots(figsize=(9,9))

color = 'tab:red'
ax1.set_xlabel('Date')
ax1.set_ylabel('Quantity X', color=color)
ax1.plot(df['Quantity x'], color=color)
ax1.tick_params(axis='y', labelcolor=color)

ax2 = ax1.twinx()
color2 = 'tab:blue'
ax2.set_ylabel('Deaths', color=color2)
ax2.plot(df['Deaths'], color=color2)
ax2.tick_params(axis='y', labelcolor=color2)
plt.show()

我想创建一个循环,以便上面的代码针对与 City 相关的每个 Code 运行,数量为 x 和死亡,并将每个图表保存为文件夹。我如何创建一个循环来执行此操作,并停止每个不同的 Code?

观察:df['Quantity x]df[Population] 上的某些值留空。

如果我没理解错的话,你正在寻找 filtering functionality:

import matplotlib.pyplot as plt
import pandas as pd


def plot_quantity_and_death(df):
    # your code
    fig, ax1 = plt.subplots(figsize=(9, 9))

    color = 'tab:red'
    ax1.set_xlabel('Date')
    ax1.set_ylabel('Quantity X', color=color)
    ax1.plot(df['Quantity x'], color=color)
    ax1.tick_params(axis='y', labelcolor=color)

    ax2 = ax1.twinx()
    color2 = 'tab:blue'
    ax2.set_ylabel('Deaths', color=color2)
    ax2.plot(df['Deaths'], color=color2)
    ax2.tick_params(axis='y', labelcolor=color2)

    # save & close addon
    plt.savefig(f"Code_{str(df['Code'].iloc[0])}.png")
    plt.close()


df = pd.DataFrame()  # this needs to be replaced by your dataset

# get unique city codes, loop over them, filter data and plot it
unique_codes = pd.unique(df['Code'])
for code in unique_codes:
    filtered_df = df[df['Code'] == code]
    plot_quantity_and_death(filtered_df)