116

I have started my IPython Notebook with

ipython notebook --pylab inline

This is my code in one cell

df['korisnika'].plot()
df['osiguranika'].plot()

This is working fine, it will draw two lines, but on the same chart.

I would like to draw each line on a separate chart. And it would be great if the charts would be next to each other, not one after the other.

I know that I can put the second line in the next cell, and then I would get two charts. But I would like the charts close to each other, because they represent the same logical unit.

5 Answers 5

147

You can also call the show() function after each plot. e.g

   plt.plot(a)
   plt.show()
   plt.plot(b)
   plt.show()
3
  • 1
    Great, but how to set the plot picture location, like (2,2) or (1,2)?
    – Pythoner
    Apr 8, 2016 at 15:07
  • 3
    With this option, the plots necessarily appear one after the other. For more control on the overall layout, pyplot.subplots() is the way to go. Oct 1, 2017 at 19:55
  • 1
    I removed the link.
    – Tooblippe
    Apr 23, 2019 at 9:55
103

Make the multiple axes first and pass them to the Pandas plot function, like:

fig, axs = plt.subplots(1,2)

df['korisnika'].plot(ax=axs[0])
df['osiguranika'].plot(ax=axs[1])

It still gives you 1 figure, but with two different plots next to each other.

4
  • 5
    This is working. Now I wont to change size of image. I can do it with fig, axs = plt.subplots(1,2, figsize=(15, 5)). Is there some way to set size of image in pixels ?
    – WebOrCode
    May 6, 2013 at 7:31
  • 1
    Using the figsize is indeed the way, thats the size in inches. How many pixels that will be depends on the dpi, which is fixed for displaying on screen. But for saving it can be set by adding the dpi keyword. May 6, 2013 at 7:44
  • 3
    It is not quite accurate that the dpi is fixed for on-screen display (well it is and it isn't). IPython displays images based purely on their pixel size, and that pixel size is determined in matplotlib by multiplying the figure size in inches by the savefig.dpi config. Inline figures use the same savefig path as writing files.
    – minrk
    May 6, 2013 at 18:36
  • Thanks for pointing that out, i didnt know it worked like that. Wouldnt it be more convenient though if the display dpi was taken into account (as well), since thats easy to set at figure creation. For the savefig.dpi i dont know any other way than mpl.rcParams['savefig.dpi'] = 120. For the display dpi there is a keyword in a lot of places, like plt.figure(dpi=120), but thats ignored now, hence my wrong assumption about it being fixed. May 7, 2013 at 6:32
17

Something like this:

import matplotlib.pyplot as plt
... code for plot 1 ...
plt.show()
... code for plot 2...
plt.show()

Note that this will also work if you are using the seaborn package for plotting:

import matplotlib.pyplot as plt
import seaborn as sns
sns.barplot(... code for plot 1 ...) # plot 1
plt.show()
sns.barplot(... code for plot 2 ...) # plot 2
plt.show()
13

Another way, for variety. Although this is somewhat less flexible than the others. Unfortunately, the graphs appear one above the other, rather than side-by-side, which you did request in your original question. But it is very concise.

df.plot(subplots=True)

If the dataframe has more than the two series, and you only want to plot those two, you'll need to replace df with df[['korisnika','osiguranika']].

1
  • For future travellers, you can now do df.plot(subplots=True, layout=(3, 2)) if you want to define a particular arrangement. You can also pass sharex and sharey to control those settings. May 11, 2022 at 4:50
2

I don't know if this is new functionality, but this will plot on separate figures:

df.plot(y='korisnika')
df.plot(y='osiguranika')

while this will plot on the same figure: (just like the code in the op)

df.plot(y=['korisnika','osiguranika'])

I found this question because I was using the former method and wanted them to plot on the same figure, so your question was actually my answer.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.