Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a loop to plot n charts Python

I have a set of data that I load into python using a pandas dataframe. What I would like to do is create a loop that will print a plot for all the elements in their own frame, not all on one. My data is in an excel file structured in this fashion:

Index | DATE  | AMB CO 1 | AMB CO 2 |...|AMB CO_n | TOTAL 1     | 1/1/12|  14      | 33       |...|  236    | 1600 .     | ...   | ...      | ...      |...|  ...    | ... .     | ...   | ...      | ...      |...|  ...    | ... .     | ...   | ...      | ...      |...|  ...    | ... n 

This is what I have for code so far:

import pandas as pd import matplotlib.pyplot as plt ambdf = pd.read_excel('Ambulance.xlsx',                        sheetname='Sheet2', index_col=0, na_values=['NA']) print type(ambdf) print ambdf print ambdf['EAS']  amb_plot = plt.plot(ambdf['EAS'], linewidth=2) plt.title('EAS Ambulance Numbers') plt.xlabel('Month') plt.ylabel('Count of Deliveries') print amb_plot  for i in ambdf:     print plt.plot(ambdf[i], linewidth = 2) 

I am thinking of doing something like this:

for i in ambdf:     ambdf_plot = plt.plot(ambdf, linewidth = 2) 

The above was not remotely what i wanted and it stems from my unfamiliarity with Pandas, MatplotLib etc, looking at some documentation though to me it looks like matplotlib is not even needed (question 2)

So A) How can I produce a plot of data for every column in my df and B) do I need to use matplotlib or should I just use pandas to do it all?

Thank you,

like image 548
MCP_infiltrator Avatar asked Oct 04 '13 19:10

MCP_infiltrator


People also ask

Can you plot in a for loop Python?

We can create a for loop and pass all the numeric columns into it. The loop will plot the graphs one by one in separate pane as we are including plt. figure() into it.

How do you plot a while loop in Python?

To plot in real-time in a while loop using Python matplotlib, we can create a loop to plot the data and then call pause . to call scatter to plot a scatterplot. Then we call pause to draw the new data and run the GUI's `event loop. And then we call show to show the GUI.

How do you make a subplot for a for loop in Python?

Create a figure and a set of subplots with number of rows = 3 and number of columns = 2. Make a function to iterate the columns of each row and plot the x data points using plot() method at each column index. Iterate rows (Step 2) and create random x data points and call iterate_columns() function (Step 3).


1 Answers

Ok, so the easiest method to create several plots is this:

import matplotlib.pyplot as plt x=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] y=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] for i in range(len(x)):     plt.figure()     plt.plot(x[i],y[i])     # Show/save figure as desired.     plt.show() # Can show all four figures at once by calling plt.show() here, outside the loop. #plt.show() 

Note that you need to create a figure every time or pyplot will plot in the first one created.

If you want to create several data series all you need to do is:

import matplotlib.pyplot as plt plt.figure() x=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] y=[[1,2,3,4],[2,3,4,5],[3,4,5,6],[7,8,9,10]] plt.plot(x[0],y[0],'r',x[1],y[1],'g',x[2],y[2],'b',x[3],y[3],'k') 

You could automate it by having a list of colours like ['r','g','b','k'] and then just calling both entries in this list and corresponding data to be plotted in a loop if you wanted to. If you just want to programmatically add data series to one plot something like this will do it (no new figure is created each time so everything is plotted in the same figure):

import matplotlib.pyplot as plt x=[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] y=[[1,2,3,4],[2,3,4,5],[3,4,5,6],[7,8,9,10]] colours=['r','g','b','k'] plt.figure() # In this example, all the plots will be in one figure.     for i in range(len(x)):     plt.plot(x[i],y[i],colours[i]) plt.show() 

Hope this helps. If anything matplotlib has a very good documentation page with plenty of examples.

17 Dec 2019: added plt.show() and plt.figure() calls to clarify this part of the story.

like image 143
Aleksander Lidtke Avatar answered Sep 19 '22 05:09

Aleksander Lidtke