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:
- How to create a simple plot with
matplotlib.pyplot - 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.
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.
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.
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:
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:
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:
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.
4. Pivot to Wide Form
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:
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:
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.
Summary
You've learned how to create graphs in Python with matplotlib.pyplot:
- Create a simple plot —
plt.figure()to create a figure,add_subplot(111)for axes, andax.plot(x, y)to draw lines - Plot data from a pandas DataFrame — pivot long-form data to wide form with
pd.pivot(), then plot each column separately - 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.
References
- Original article: How to create a graph in Python? — Medium
- matplotlib documentation: matplotlib.pyplot.figure
- matplotlib documentation: Axes.plot
- pandas documentation: pandas.DataFrame.pivot