我有这种 pandas.DataFrame。 “a”、“b”是获取“x”和“y”时的条件。
df = pd.DataFrame([[10,20,0,.1], [10,20,1,.5], [100,200,0,.33], [100,200,1,.11]], columns=["a", "b", "x", "y"])
df
我需要根据相同的条件绘制 (x,y) 列的折线图。预期结果图是:
当然,这个图像是通过以下代码手动给出的:
pd.DataFrame([[0,.1],[1,.5]]).plot(kind="line", x=0, y=1, style="-", legend=None, title="a: 10, b: 20")
plt.xlabel("x")
plt.ylabel("y")
plt.figure()
pd.DataFrame([[0,.33],[1,.11]]).plot(kind="line", x=0, y=1, style="-", legend=None, title="a: 100, b: 200")
plt.xlabel("x")
plt.ylabel("y")
我的问题是,当获取包含条件列、x 和 y 的数据框时,如何动态制作如上所述的图。
Update
列名称是固定的。然而,条件列的值是动态改变的。所以,我不能使用值 10、20、100、200。
Update2
如果我有以下“filter_with_a_and_b”方法,我认为问题已解决:
def filter_with_a_and_b(df, a_b):
# how to implement?
a_b_list = df.drop_duplicates(["a","b"])
new_df_list = filter_with_a_and_b(df, a_b)
for idx, df in enumerate(new_df_list):
df.plot(title=a_b_list[idx])