This article will help you to create a Line Graph in Python using Matplotlib library.
Prerequisites:
- Python Programming Language
- Numpy Library
- 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
2. Define the y-axis points
ypoints = np.array([1, 7, 5, 9])
3. Plot the y-axis points into the graph
plt.plot(ypoints)
4. Display the graph
plt.show()
The result:
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()
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()
The result:
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()
The result:
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()
The result:
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()
The result:
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()
The result:
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()
The result:
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()
The result:
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()
The result:
You can find more information about grid function in the following link: https://www.w3schools.com/python/matplotlib_grid.asp
Top comments (0)