I have this kind of pandas.DataFrame. "a","b" are conditions when getting "x" and "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
I need to plot line charts of (x,y) colums with respect to the same condition. The expected result plot is:
Of course, this image is manually given by the following code:
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")
My question is how to dynamically make plots like above when getting a dataframe including condition columns, x and y.
Column names are fixed. However, values of condition columns is dynamically changed. So, I can not use the values 10, 20, 100, 200.
If I have the below "filter_with_a_and_b" method, I think the problem solved:
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])
is that what you want?
df.loc[(df.a == 10) & (df.b == 20), ['x','y']].plot(title='a: 10, b: 20')
and now let's do it bit smarter:
cond = {'a': 100, 'b': 200}
df.loc[(df.a == cond['a']) & (df.b == cond['b']), ['x','y']].plot(title='a: {a}, b: {b}'.format(**cond))
or using query()
:
q = 'a == 100 and b == 200'
df.query(q)[['x','y']].plot(title=q)
UPDATE:
a_b_list = df[['a','b']].drop_duplicates()
[df.loc[(df.a == tup[0]) & (df.b == tup[1]), ['x','y']] \
.plot(x='x', y='y', kind='line', style='-',title='a: {0[0]}, b: {0[1]}'.format(tup)) \
.set_ylabel('y')
for tup in a_b_list.itertuples(index=False)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With