2

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()

Figs

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.

3
  • @ansev Haha, almost. I want both figures, but also as separate legends. Commented Nov 6, 2019 at 23:01
  • I don't really get what issue is caused by the fact that one plot has two y values at the same x. As you can see, it can be plot without any issues. And perhaps if you invert the order of the three points it consists of, you might even be able to plot the whole line (the square) with one plot command. Commented Nov 6, 2019 at 23:30
  • @SpghttCd Typically, I would plot using f['y_axis].plot(...), but this wouldn't work with f[f['figure].eq('fig1')] because I also have to specify x_axis. Does this make sense? Commented Nov 7, 2019 at 21:44

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! I had used subplots before, but I always did it in the form of 'f['y_axis'].plot(ax=ax)´. I didn't know you could specify an axis as well as the x and y parameters. I now see I don't understand a fundamental part of matplotlib.
1

Use DataFrame.pivot_table:

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:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.