Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pandas, Plotting options for multiple lines

I want to plot multiple lines from a pandas dataframe and setting different options for each line. I would like to do something like

testdataframe=pd.DataFrame(np.arange(12).reshape(4,3)) testdataframe.plot(style=['s-','o-','^-'],color=['b','r','y'],linewidth=[2,1,1]) 

This will raise some error messages:

  • linewidth is not callable with a list

  • In style I can't use 's' and 'o' or any other alphabetical symbol, when defining colors in a list

Also there is some more stuff which seems weird to me

  • when I add another plot command to the above code testdataframe[0].plot() it will plot this line in the same plot, if I add the command testdataframe[[0,1]].plot() it will create a new plot

  • If i would call testdataframe[0].plot(style=['s-','o-','^-'],color=['b','r','y']) it is fine with a list in style, but not with a list in color

Hope somebody can help, thanks.

like image 897
Joerg Avatar asked Jan 06 '13 00:01

Joerg


People also ask

How do you make a multiple line graph on pandas?

Set the figure size and adjust the padding between and around the subplots. Make a 2D potentially heterogeneous tabular data using Pandas DataFrame class, where the column are x, y and equation. Get the reshaped dataframe organized by the given index such as x, equation, and y. Use the plot() method to plot the lines.

How do you plot multiple lines on a graph in Python?

You can plot multiple lines from the data provided by an array in python using matplotlib. You can do it by specifying different columns of the array as the x and y-axis parameters in the matplotlib. pyplot. plot() function.

Can we plot multiple lines using Python?

you can use: fig,ax = plt. subplots(2) then use: ax[0]. plot(x,y1) ax[1]. plot(x,y2) or if you want you can separate your code into two blocks of code.


2 Answers

You're so close!

You can specify the colors in the styles list:

import numpy as np import matplotlib.pyplot as plt import pandas as pd  testdataframe = pd.DataFrame(np.arange(12).reshape(4,3), columns=['A', 'B', 'C']) styles = ['bs-','ro-','y^-'] linewidths = [2, 1, 4] fig, ax = plt.subplots() for col, style, lw in zip(testdataframe.columns, styles, linewidths):     testdataframe[col].plot(style=style, lw=lw, ax=ax) 

Also note that the plot method can take a matplotlib.axes object, so you can make multiple calls like this (if you want to):

import numpy as np import matplotlib.pyplot as plt import pandas as pd  testdataframe1 = pd.DataFrame(np.arange(12).reshape(4,3), columns=['A', 'B', 'C']) testdataframe2 = pd.DataFrame(np.random.normal(size=(4,3)), columns=['D', 'E', 'F']) styles1 = ['bs-','ro-','y^-'] styles2 = ['rs-','go-','b^-'] fig, ax = plt.subplots() testdataframe1.plot(style=styles1, ax=ax) testdataframe2.plot(style=styles2, ax=ax) 

Not really practical in this case, but the concept might come in handy later.

like image 167
Paul H Avatar answered Sep 19 '22 02:09

Paul H


So I think the answer lies in passing the color and style in the same argument. The following example works with pandas 0.19.2:

testdataframe=pd.DataFrame(np.arange(12).reshape(4,3)) testdataframe.plot(style=['r*-','bo-','y^-'], linewidth=2.0) 

Unfortunately, it seems that passing multiple line widths as an input to matplotlib is not possible.

like image 38
RexFuzzle Avatar answered Sep 19 '22 02:09

RexFuzzle