Link Search Menu Expand Document (external link)

How to create basic plots (in Python, using Matplotlib)

See all solutions.

Task

Plotting is a huge topic with many options and variations, but the most foundational types of plots are a line plot and a scatterplot. How can we create those?

Related topics:

Solution

We will create some fake data using Python lists, for simplicity. But everything we show below works also if your data is in columns of a DataFrame, such as df['age'].

1
2
3
patient_id     = [   0,   1,   2,   3,   4,   5,   6,   7,   8,   9 ]
patient_height = [  60,  64,  64,  65,  66,  66,  70,  72,  72,  76 ]
patient_weight = [ 141, 182, 169, 204, 138, 198, 180, 175, 244, 196 ]

The conventional way to import matplotlib in Python is as follows.

1
import matplotlib.pyplot as plt

Make a line plot by giving the $x$ and $y$‚Äč data values in separate lists (or in this case pandas Series). This line plot is very jagged just because the data was random.

1
2
plt.plot( patient_id, patient_height )         # create plot
plt.show()                                     # display plot

png

You can make a scatterplot as follows.

1
2
plt.scatter( patient_height, patient_weight )  # create plot
plt.show()                                     # display plot

png

If your data is already in a pandas DataFrame, there are shortcuts:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Plot all columns:
df.plot()
plt.show()

# Plot all columns in separate subplots:
df.plot( subplots = True )
plt.show()

# Plot one column:
df['column'].plot()
plt.show()

# Plot specific columns:
df.plot( x='col name', y='other col name' )
plt.show()

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Contributed by Nathan Carter (ncarter@bentley.edu)