Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting multiple columns in a pandas line graph [duplicate]

I am trying to plot a multiple columns in a line graph with 'Month' as the X axis and each 'Count' as a new line. I want it to have 5 lines, 'Count-18..Count-14'. I tried plotting 1 line as a test but when I run the following code I get the following output with no graph. Any ideas?

ax = plt.gca()

DomReg1418.plot(kind='line',x='Month',y='Count-18',ax=ax)

enter image description here

enter image description here

like image 746
Jgallagher Avatar asked Nov 22 '19 16:11

Jgallagher


People also ask

How do I graph multiple columns in pandas?

Pandas has a tight integration with Matplotlib. You can plot data directly from your DataFrame using the plot() method. To plot multiple data columns in single frame we simply have to pass the list of columns to the y argument of the plot function.

How do you plot multiple lines on a DataFrame graph?

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

Can pandas apply return multiple columns?

Return Multiple Columns from pandas apply() You can return a Series from the apply() function that contains the new data. pass axis=1 to the apply() function which applies the function multiply to each row of the DataFrame, Returns a series of multiple columns from pandas apply() function.


Video Answer


1 Answers

When you have a DataFrame with one column to be used as X axis and other as a source of lines to draw, you should:

  • set the index to the "X" column (in your case Month),
  • run plot,
  • terminate the command with a semicolon, to block a text message concerning the picture object.

The code to do it is:

DomReg1418.set_index('Month').plot();

For the test, I entered X and 3 Count columns, for first 6 months and executed the above command, getting the following result:

enter image description here

like image 103
Valdi_Bo Avatar answered Oct 22 '22 02:10

Valdi_Bo