Make more than one chart in same IPython Notebook cell

PythonPandasIpythonIpython Notebook

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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

   plt.plot(a)
   plt.show()
   plt.plot(b)
   plt.show()

Solution 2 - Python

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.

Solution 3 - Python

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

Solution 4 - Python

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']].

Solution 5 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWebOrCodeView Question on Stackoverflow
Solution 1 - PythonTooblippeView Answer on Stackoverflow
Solution 2 - PythonRutger KassiesView Answer on Stackoverflow
Solution 3 - PythonmgoldwasserView Answer on Stackoverflow
Solution 4 - PythonLucianoView Answer on Stackoverflow
Solution 5 - PythonstevenView Answer on Stackoverflow