How To Plot A Line Graph Using Python (15 Examples)

Possibly the most simple of all plots are line graphs, line graphs are a great way to represent information that changes continuously over time. In this article, we will show you 15 ways to plot a line graph using python programming.

When should you use a line graph?

As mentioned previously, line graphs are best when you want to show how the value of something changes over time, or compare how several things change over time compared to each other. The reason why we want to use line graphs is that it is simple, easy to understand, and efficient. This allows us to forecast data and analyze trends of data.

Dataset used to plot the line graph

The dataset we will be using is from Kaggle, this dataset contains GDP per capita across several years, provides a GDP provides an economic snapshot of a country, used to estimate the size of an economy and growth rate.

The dataset we will be using can be downloaded on the Kaggle website here:

If that link doesn’t work, you can directly download the CSV file here:

To analyze the data, what I have done is create a python file in the same folder as to where my CSV file is located, then called pandas pd.read_csv function to read our CSV file and print the first 5 rows and last 5 rows of each of our attributes using the print function.

import pandas as pd #importing our pandas library
df = pd.read_csv('gdp-per-capita.csv') #reading our CSV file
print(df) #printing the dataframe
Line Graph

From the table above, we will be primarily importing data from our “Entity”, “Year” and “GDP per Capita” columns.

Line Graphs Using Matplotlib Pyplot

Matplotlib python package is by far one of the most widely used and oldest python data packages for visualizations currently available. One of the great things about matplotlib is it comes with wide amounts of graphs available, one of them is their line graph.

Below we will show you a couple of ways you can graph line charts using matplotlib.

1. Line Graph

To create a line graph using Matplotlib, it’s as simple as just declaring the x-axis and y-axis values.

In this line graph, the x-axis was created through the use of dataframe’s pandas function to extract values that are equal to “United States” GDP and its corresponding years.

This was then used to plot our line graph using the function “plt.plot(x values, y values)”.

Line graph with matplotlib
Line graph with matplotlib python code

2. Colored Line Graph

Matplotlib plot function also provides the ability to change the color of your line graph. To change the color of your line graph, you can simply use the attribute “color” within your “plt.plot()” function and declare the relevant colors.

Colored line graph using matplotlib
coloured line graph python code

3. Dotted Line Graph

An option that matplotlib provides is the ability to change the style of your lines. To create a dotted or dashed line graph in matplotlib we can use the attribute “linestyle” within matplotlib’s line graph plot function and declare it as “dashed”.

dotted line graph matplotlib
dotted line graph python code

4. Marked Line Graph

Another option that matplotlib offers are the ability to create markers. To create markers on our line graph, we can use the attribute “marker” within matplotlib’s line graph plot function and input the value as ‘o’.

Similarly, matplotlib also offers much more marked symbols, you can learn more on their website here.

marked line graph matplotlib
marked line graph code

5. Line Graph With Grids

We can also create a grid in matplotlib, to create our grid we can simply use the function “plt.grid()” and declare the value as “True” to enable a grid on our line graph.

Line graph with grids matplotlib
line graph with python code

6. Line Graph With Annotations

One of the great things that matplotlib offers is the ability to annotate your graphs, there are many ways to annotate your graphs. The one I like is plotting a verticle line and inputting a brief text.

Matplotlib allows you to input vertical line with their function “plt.axvline()” and annotation text with “plt.text()”.

Line graph with annotations in matplotlib
line graph with annotations using python

7. Stacked Line Graph

To create a stacked line graph in matplotlib, we need to declare two charts.

In our current chart, two x and y values were declared, one for Venezuela and the other for the United States. This is then plotted using the function “ax.plot()” to declare the line graphs.

Stacked line graph matplotlib
python code for line graph

8. Stacked Dotted Line Graph

Much like our singular line graph, we can also edit the attributes of our individual line graphs in the stacked line chart. To edit the style of the corresponding line chart, we can just use the attribute “linestyle” to adjust the style of the line on the particular variable which plots our specific line graph.

Stacked graph
Stacked graph code

9. Stacked Line Graph With markers

Similarly, creating a line graph with markers also works with stacked line graphs. This can be completed using our “marker” attribute and declaring the specific marker style.

US GDP vs Venezuela GDP With markers
US GDP vs Venezuela GDP With markers python code

10. Stacked Line Graph With Annotations

As you increase more details within a chart, it is recommended to put annotations. Luckily putting annotations on a stacked line graph is similar to a singular line graph. Much like our singular line graph, we can just simply add the variables “plt.axvline()” and “plt.text()” on our chart to put annotations.

Annoted graph of US GDP vs Venezuela GDP
Python code for annotated line graph in matplotlib

Line Graphs Using Plotly

The Plotly Python library is an interactive, open-source plotting library. One of the benefits of Plotly is it has superior navigation and interactive abilities and provides more visually appealing charts compared to matplotlib.

11. Line graph

To make a line graph using plotly we need to import the package first then use the function “px.line()”, afterward we just need to input our data frame variable that reads our CSV file and then the corresponding x-axis and y-axis.

The added attribute “labels=dict(x=”Year”, y=”GDP”)” allows us to label our x and y axis.

Plotly line graph
Python code for plotly

12. Line Graph With Markers

Similarly like matplotlib, plotly also offers charts with markers we can turn on markers for our graph by declaring marker as “True” within our “px.line()” function.

13. Stacked Line Graph

An option that plotly offers is the ability to plot multiple graphs in one chart, there are many ways to plot line graphs on the same y-axis. One way is to graph an additional line graph is to use the function “px.add_scatter()” and declare the mode to “line” this essentially turns the scatter graph to a line graph.

Line Graphs Using Seaborn

Seaborn is a data visualization library built on top of matplotlib, but with an added benefit, which is the ability to provide more attractive visualizations and functions.

14. Line Graph With Confidence Interval

A function that seaborn provides is the ability to combine several data points into a confidence interval. If we were to plot all our countries and their corresponding GDP and Years.

The line plot will look like something below.

Luckily plotly offers their function “sns.lineplot” where we can consolidate all our lines into a channel through the use of our confidence intervals.

As shown in the graph below it indicates a clear confidence band to depict the upper and lower confidence bounds for all the main points graphed from our dataset.

15. Line Graph With Confidence Interval Error Bars

Similarly, we can also adjust the style of our confidence bands, within our “sns.lineplot” function there is an added attribute called “err_style” which allows us to change the style of our bars. In our case, we can declare the style as “bars” which essentially graphs the confidence intervals into a scaled line bar to depict the confidence intervals within each year of our data points.

Furthermore, there is also the ability to adjust our confidence intervals, this can be completed by using the attribute “ci” within our “sns.lineplot” function.