DEV Community

squawk1711
squawk1711

Posted on

Create Line Graph in Matplotlib

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

import matplotlib.pyplot as plt
import numpy as np
Enter fullscreen mode Exit fullscreen mode

2. Define the y-axis points

ypoints = np.array([1, 7, 5, 9])
Enter fullscreen mode Exit fullscreen mode

3. Plot the y-axis points into the graph

plt.plot(ypoints)
Enter fullscreen mode Exit fullscreen mode

4. Display the graph

plt.show()
Enter fullscreen mode Exit fullscreen mode

The result:

Result 1

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

import matplotlib.pyplot as plt
import numpy as np

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

plt.plot(ypoints)
plt.show()
Enter fullscreen mode Exit fullscreen mode

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:

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()
Enter fullscreen mode Exit fullscreen mode

The result:

Result 2

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:

import matplotlib.pyplot as plt
import numpy as np

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

plt.plot(ypoints, linestyle = "dotted")
plt.show()
Enter fullscreen mode Exit fullscreen mode

The result:

Result 3

Here is another example:

import matplotlib.pyplot as plt
import numpy as np

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

plt.plot(ypoints, linestyle = "dashed")
plt.show()
Enter fullscreen mode Exit fullscreen mode

The result:

Result 4

You can find all possible values for linestyle keyword argument from the following link: 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:

import matplotlib.pyplot as plt
import numpy as np

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

plt.plot(ypoints, color = "r")
plt.show()
Enter fullscreen mode Exit fullscreen mode

The result:

Result 5

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

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:

import matplotlib.pyplot as plt
import numpy as np

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

plt.plot(ypoints, marker = "o")
plt.show()
Enter fullscreen mode Exit fullscreen mode

The result:

Result 6

Here is another example:

import matplotlib.pyplot as plt
import numpy as np

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

plt.plot(ypoints, marker = "+")
plt.show()
Enter fullscreen mode Exit fullscreen mode

The result:

Result 7

You can refer to the following link to see the possible value for marker keyword argument: 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:

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()
Enter fullscreen mode Exit fullscreen mode

The result:

Result 8

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:

import matplotlib.pyplot as plt
import numpy as np

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

plt.plot(ypoints)
plt.grid()
plt.show()
Enter fullscreen mode Exit fullscreen mode

The result:

Result 9

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

Top comments (0)