# 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**.

**The easiest way to plot a line graph in python is by using the function plt.plot() from the package matplotlib.pyplot.**

However, there are several ways to plot line graphs in python. That is why in this article, we will show you 15 ways to plot a line graph using python programming.

**Free Bonus: ****Click Here To Get A FREE Introduction to Data Science in Python** and learn how to effectively analyze and visualize your data in Python 3 by leveraging popular Data Science modules like Matplotlib (for charts and graphs) and pandas (for tabular data)

## 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.

Before, you can use matplotlib you need to install and import the package which can be done using the code below:

```
!pip install matplotlib
import matplotlib.pyplot as plt
```

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 attribute in the exact order within the `plt.plot`

function.

Basically the `plt.plot`

function in its default state draws a line from point to point, so it automatically recognises data as a line graph.

```
year = [1650, 1700, 1800, 1900, 2000]
GDP = [3000, 4000, 5000, 6000, 7000]
plt.plot(year, GDP)
```

As shown above, its quite simple to plot a line graph, where we just declared the x-axis as the year and our GDP value as the y axis.

### 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.

```
year = [2000, 2005, 2010, 2015, 2030]
GDP = [6000, 10000, 15000, 16000, 27000]
plt.plot(year, GDP, color = 'red')
```

### 3. Dotted Line Graph

Since in the default state matplotlib’s `plt.plot`

draws graphs by connecting datapoints together.

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`

.

`plt.plot(year, gdp, linestyle='dashed')`

### 4. Marked Line Graph

Using markers is a great way to show the volatility or changes from one dot point to another.

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.

`plt.plot(year, gdp, marker='o')`

### 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.

```
plt.plot(year, gdp)
plt.grid(True)
```

### 6. Line Graph With Annotations

Annotations are a great way to put context into your graphs. One of the great things that matplotlib offers is the ability to annotate your graphs, there are many ways to annotate your graphs.

Below, we annotated the graph using a vertical line and floating text to show the great financial crash in 2008.

Matplotlib allows you to input vertical line with their function `plt.axvline()`

as you can see in the function, it stands for plotting a vertical line on the x-axis. Within the function, you’ll need to declare the x-axis you want your line to be.

In terms of text, you can use `plt.text()`

and input the x,y-axis with a string text inside.

```
plt.axvline(2007, color='r', linestyle='dashed')
plt.text(2007.5, 40000, 'Great Financial Crash', fontsize=8)
```

### 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.

### 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.

### 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.

### 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.

## 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.

### 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.

## 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.