Data Science Tutorial

How to Create a Graph in Python

Learn the basics of Python plotting with matplotlib.pyplot — from empty figures to polished multi-series line charts with labels, legends, and grids.

By Suhith Illesinghe · 10 Apr 2026 · 8 min read
Ad Advertisement — 728 x 90

Creating graphs in Python is an important skill to learn. Monitoring your health is critical for living a long and healthy life. Similarly, business professionals monitor the health of the business by evaluating graphs. Data professionals use data collected to give the business the necessary understanding through graphs and other visual mediums.

There are many ways to create graphs in Python and if you are new to Python this is one of the most difficult skills to master. To start with, I will show the most basic way to create a graph in Python with a pandas data frame. You will learn the following:

  1. How to create a simple plot with matplotlib.pyplot
  2. How to plot data from a pandas DataFrame with matplotlib.pyplot

1. Import Libraries and Create Data

Let's get started by importing the necessary libraries: numpy, pandas, and matplotlib.pyplot. We'll also create our dataset inline — monthly new car registration data for EV and Diesel vehicles across 12 months.

Python — editable
Figure 1: Car registration data — 24 rows, two vehicle types over 12 months (in thousands).

The data shows the monthly new car registrations (in thousands) for two vehicle types: ev (electric vehicles) and diesel. The business is interested in understanding market trends — in particular, how is EV adoption growing compared to diesel, and when does EV overtake diesel?

2. How to Create a Simple Graph with matplotlib.pyplot

The matplotlib package has been around for more than a decade and has definitely passed the test of time. Let's start by creating a simple figure using plt.figure.

Python — editable
Figure 2: An empty matplotlib figure — 640x480 dots (6.4 x 4.8 inches at 100 dpi).

I specified the figure number as num=1. This is not essential, but I find it more elegant to number them when you are creating many graphs. The figure size shows there are 640 dots in width and 480 dots in height. Typically, matplotlib has 100 dots per inch (dpi).

Add a subplot

Now we will add a subplot using add_subplot method. The add_subplot method takes a number like 111 — the first two digits create the two-dimensional grid of subplots while the last digit is for the position. So 111 means 1 row, 1 column, first subplot.

Python — editable
Figure 3: A figure with a subplot — default axes from 0 to 1.
Ad Advertisement — 728 x 90

Change the figure size with figsize

Let's change the size of the image using figsize. Let's create a 6-inch by 6-inch image:

Python — editable
Figure 4: A 6x6 inch figure — the aspect ratio has changed.

Plot simple data points

The default dimensions have changed to the new dimensions. Let's add some data points to a line plot and show it on the graph:

Python — editable
Figure 5: A line from point (1,4) to point (2,6).

3. How to Plot Data from a Pandas DataFrame

Now you've got the hang of how to plot data. Time to plot data from a pandas data frame. As a first attempt, we will simply plot the data by separating them to arrays for the cars data frame:

Python — editable
Figure 6: First attempt — both vehicle types plotted as one messy line.

Oops, looks like we are plotting both vehicle types as one line. The data presented in the cars data frame is actually in a long form. We want the data to be in a wide form — a column for 'month' and the monthly registrations separated into individual vehicle type columns. Let's convert the data to a wide form using pivot.

Ad Advertisement — 728 x 90

4. Pivot to Wide Form

Python — editable
Figure 7: Data pivoted to wide form — one column per vehicle type.

The data is now in a wide form, so now we can plot each vehicle type separately. Since we have two types we will need to call .plot twice:

Python — editable
Figure 8: Two separate lines — one per vehicle type. But no labels or legend yet.

5. Polish the Graph — Labels, Legend, and Grid

Figure 8 shows two lines, one for each vehicle type. But the graph doesn't look the best. The x-axis values are overlapping each other. There is no x-axis label and a y-axis label. There is no legend. Let's rectify all these issues:

Python — editable
Figure 9: Polished graph with axis labels, rotated x-ticks, legend, and grid.

The axis labels are in place, the x-labels are rotated, and a nice grid is in place to read the values. Additionally, there is a legend as well. So now you can answer the business question clearly. EV registrations show a clear upward trend throughout 2025, overtaking diesel around June-July. Meanwhile, diesel registrations are steadily declining.

This reflects the broader market shift towards electric vehicles. The crossover point — where EV registrations surpass diesel — is a key milestone for the automotive industry. December shows a spike in EV registrations, likely driven by end-of-year incentives and fleet purchases before new regulations take effect.

Ad Advertisement — 728 x 90

Summary

You've learned how to create graphs in Python with matplotlib.pyplot:

  1. Create a simple plotplt.figure() to create a figure, add_subplot(111) for axes, and ax.plot(x, y) to draw lines
  2. Plot data from a pandas DataFrame — pivot long-form data to wide form with pd.pivot(), then plot each column separately
  3. Polish the graph — add axis labels with set_xlabel() / set_ylabel(), rotate x-ticks, add a legend, set y-limits, and enable grid lines

Try editing the code blocks above — change the figsize, plot different columns, try a bar chart with ax.bar() instead of ax.plot(), or experiment with different colours and line styles.

Ad Advertisement — 728 x 90

References

Suhith Illesinghe
Curiosity is the first step to make a difference. I hope to inspire others to explore, build and champion collaborative growth.
Follow on Medium ↗