Reputation: 6333
I plotted two different subplots using matplotlib.plt
and pandas.DataFrame.plot
.
Both figures are stored in the same pandas dataframe, which I called f
. You can download the sample data here.
One of these plots cannot be described by a function (that is, one x
value can yield two or more y
values. This is what's causing the issue (I'm trying to plot a square).
I tried:
f[f['figure'] == 'fig1'].plot(x='x_axis', y='y_axis', legend=False)
f[f['figure'] == 'fig2'].plot(x='x_axis', y='y_axis', legend=False)
plt.show()
I want both subplots combined into a single one. Is there a way to plot the second subplot in the same subplot as the first? I want to stack both figures in a single subplot.
Upvotes: 2
Views: 6988
Reputation: 30940
df_toplot=df.pivot_table(columns='figure',index='x_axis',values='y_axis').ffill()
print(df_toplot)
Output
figure fig1 fig2
x_axis
0 100.000000 37.033667
1 99.969669 37.033667
2 99.939339 37.033667
3 99.939339 37.033667
4 99.909008 37.033667
... ... ...
365 0.060661 18.516833
366 0.060661 18.516833
367 0.060661 18.516833
368 0.060661 18.516833
369 0.030331 18.516833
df_toplot.plot(legend=False)
Output image:
Upvotes: 1
Reputation: 10890
You can always plot again and again into the same plot if you have stored its axes object, e.g. like
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
f[f['figure'] == 'fig1'].plot(ax=ax, x='x_axis', y='y_axis', legend=False)
f[f['figure'] == 'fig2'].plot(ax=ax, x='x_axis', y='y_axis', legend=False)
plt.show()
Note that this is just one single example how to get the current axes - another one might be
ax = plt.gca()
The main point here is to refer to it in pandas' plot command with the ax
kwarg.
Upvotes: 7