Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPython/Jupyter Notebook and Pandas, how to plot multiple graphs in a for loop?

Consider the following code running in iPython/Jupyter Notebook:

from pandas import * %matplotlib inline  ys = [[0,1,2,3,4],[4,3,2,1,0]] x_ax = [0,1,2,3,4]  for y_ax in ys:     ts = Series(y_ax,index=x_ax)     ts.plot(kind='bar', figsize=(15,5)) 

I would expect to have 2 separate plots as output, instead, I get the two series merged in one single plot. Why is that? How can I get two separate plots keeping the for loop?

like image 697
alec_djinn Avatar asked Apr 09 '15 07:04

alec_djinn


People also ask

What does %% capture do Jupyter?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.

What is %% Writefile in Jupyter notebook?

%%writefile lets you output code developed in a Notebook to a Python module. The sys library connects a Python program to the system it is running on.


1 Answers

Just add the call to plt.show() after you plot the graph (you might want to import matplotlib.pyplot to do that), like this:

from pandas import Series import matplotlib.pyplot as plt %matplotlib inline  ys = [[0,1,2,3,4],[4,3,2,1,0]] x_ax = [0,1,2,3,4]  for y_ax in ys:     ts = Series(y_ax,index=x_ax)     ts.plot(kind='bar', figsize=(15,5))     plt.show() 
like image 57
Andrey Sobolev Avatar answered Sep 21 '22 02:09

Andrey Sobolev