Create Line Graph in Matplotlib
Tags: python, datascience, machinelearning, tutorial
Published: 2023-01-28 22:30:22

This article will help you to create a Line Graph in Python using Matplotlib library.

**Prerequisites:**

1. Python Programming Language
2. Numpy Library
3. Matplotlib Library

**Table of contents:**

- Step-to-step
- Graph customization
    - X-axis Points
    - Line Styles
    - Line Colors
    - Markers
    - Labels
    - Grids

Line Graph can be created by utilizing `plot` function under `pyplot` submodule.

The following is the step-to-step to create a basic Line Graph in Matplotlib.

## Step-to-Step:

### 1. Import the required libraries

```python
import matplotlib.pyplot as plt
import numpy as np
```

### 2. Define the y-axis points

```python
ypoints = np.array([1, 7, 5, 9])
```

### 3. Plot the y-axis points into the graph

```python
plt.plot(ypoints)
```

### 4. Display the graph

```python
plt.show()
```

The result:

![Result 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxqd4mbxcth1frmvybzz.png)

Below is the full code from the above step-to-step instructions:

```python
import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([1, 7, 5, 9])

plt.plot(ypoints)
plt.show()
```

---

## Graph Customization

### 1. X-axis Points

If x-axis points is not defined, the default values will be 0, 1, 2, 3, … (depends on the length of the y-axis points). You can specifically set the value of x-axis for each y-axis, just make sure that the number of point is same in both axis.

Below is example of the code if you want to also specify the x-axis:

```python
import matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 4, 8, 10])
ypoints = np.array([1, 7, 5, 9])

plt.plot(xpoints, ypoints)
plt.show()
```

The result:

![Result 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0hu2j0dr79krb2s7ljp6.png)

### 2. Line Styles

keyword argument `linestyle` can be used to change the style of the plotted line.

Below is example of the code if you want to change the style of the plotted line:

```python
import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([1, 7, 5, 9])

plt.plot(ypoints, linestyle = "dotted")
plt.show()
```

The result:

![Result 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/maiim34mfmbw9y4dtjxp.png)

Here is another example:

```python
import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([1, 7, 5, 9])

plt.plot(ypoints, linestyle = "dashed")
plt.show()
```

The result:

![Result 4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/enxc79mr0c5tnoz16bss.png)

You can find all possible values for `linestyle` keyword argument from the following link: [https://www.geeksforgeeks.org/linestyles-in-matplotlib-python/](https://www.geeksforgeeks.org/linestyles-in-matplotlib-python/)

### 3. Line Colors

`color` keyword argument  can be used to change the color of the line.

The following is example of the code if you want to change the color of the line:

```python
import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([1, 7, 5, 9])

plt.plot(ypoints, color = "r")
plt.show()
```

The result:

![Result 5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l94jq4alh12u3895opk8.png)

There is a lot you can do with `color` keyword argument, you can see more example in the following link: [https://www.w3schools.com/python/matplotlib_line.asp](https://www.w3schools.com/python/matplotlib_line.asp)

### 4. Markers

You can use `marker` keyword argument to represent each point with specified marker.

Below is example of the code if you want to specify the marker:

```python
import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([1, 7, 5, 9])

plt.plot(ypoints, marker = "o")
plt.show()
```

The result:

![Result 6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/treilpbye0skap0nq4wg.png)

Here is another example:

```python
import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([1, 7, 5, 9])

plt.plot(ypoints, marker = "+")
plt.show()
```

The result:

![Result 7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hc5vng8h79icig6b467e.png)

You can refer to the following link to see the possible value for `marker` keyword argument: [https://matplotlib.org/stable/api/markers_api.html](https://matplotlib.org/stable/api/markers_api.html)

### 5. Labels

Functions `xlabel()` and `ylabel()` can be used to set the description for each axis.

The following is example of the code if you want to set the description of each axis:

```python
import numpy as np
import matplotlib.pyplot as plt

xpoints = np.array([1, 2, 4, 6])
ypoints = np.array([2, 4, 8, 12])

plt.plot(xpoints, ypoints)

plt.xlabel("Distance")
plt.ylabel("Car Fuel")

plt.show()
```

The result:

![Result 8](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/99enusvi5uwjanmncu9o.png)

### 6. Grids

You can use `grid()` function to add grid lines.

Below is the code example if you want to add grid lines to the plot:

```python
import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([1, 7, 5, 9])

plt.plot(ypoints)
plt.grid()
plt.show()
```

The result:

![Result 9](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f43vxnh5kt1adar3cxmh.png)

You can find more information about grid function in the following link: [https://www.w3schools.com/python/matplotlib_grid.asp](https://www.w3schools.com/python/matplotlib_grid.asp)