How to plot multiple pandas columns

Quick answer

Pass a list of column names to DataFrame.plot(): df.plot(x='date', y=['col1','col2'], kind='line'). pandas draws one series per column on shared axes. For separate panels use subplots=True, and call plt.show() to render.

To plot multiple columns from a Pandas DataFrame, you can use the DataFrame.plot() method and specify the column names you want to plot as a list in the x and y parameters. You can also specify the kind parameter to specify the type of plot you want to create (e.g., line, bar, scatter).

Here is an example of how to plot multiple columns from a Pandas DataFrame using the plot() method:

import matplotlib.pyplot as plt
 
# Load data into a Pandas DataFrame
df = pd.read_csv("data.csv")
 
# Select the columns you want to plot
x = ['col1', 'col2']
y = ['col3', 'col4']
 
# Create the plot
df.plot(x=x, y=y, kind='line')
 
# Show the plot
plt.show()

This will create a line plot with col1 and col2 on the x-axis and col3 and col4 on the y-axis.

You can also use the plot() method directly on a single column of a DataFrame if you only want to plot a single column. For example:

df['col1'].plot()
plt.show()
 

This will create a line plot of the col1 column.


Related: this is one of a handful of recurring pandas pitfalls. See the common pandas errors and gotchas guide for the view-vs-copy, index-type, and alignment concepts behind most of them.


Related Posts