In this tutorial, we will learn how to create a line chart using Matplotlib in Python. Matplotlib's plot function allows us to create both linear and curved lines while offering customization options such as color, width, and marker size.
Creating a Basic Line Chart
Let's start by plotting a simple line chart.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 9, 10)
y = 2 * x
plt.plot(x, y, "b-")
plt.show()
Explanation
-
Importing Libraries: We import Matplotlib and set it as
plt.

import matplotlib.pyplot as plt
-
Defining Data:
x = [1,2,3,4,5,6,7,8,9,10]
y = [1,2,3,4,5,6,7,8,9,10]
-
Plotting the Graph: Using
plt.plot()withxon the horizontal axis andyon the vertical axis.
plt.plot(x, y)
plt.show()
Customizing Line Style and Color
You can modify the appearance of the line by specifying color and style.
import matplotlib.pyplot as plt
x = [1,2,3,4,5,6,7,8,9,10]
y = [1,2,3,4,5,6,7,8,9,10]
plt.plot(x, y, "r--") # Red dashed line
plt.show()

Adding Titles
To set the title of the plot:
plt.title('Plot using Python')
You can also customize the title's font and alignment:
plt.title("Left title", fontdict={'family': 'serif', 'color' : 'darkblue','weight': 'bold','size': 16}, loc='left')
plt.title("Center title", fontdict={'family': 'monospace', 'color' : 'red','weight': 'bold','size': 16}, loc='center')
plt.title("Title right", fontdict={'family': 'fantasy', 'color' : 'black','weight': 'bold','size': 16}, loc='right')
Labeling Axes
To label the x-axis and y-axis:
plt.xlabel('X-axis values')
plt.ylabel('Y-axis values')
With custom styles:
plt.xlabel('X-axis values', family='serif', color='r', weight='normal', size=16, labelpad=6)
plt.ylabel('Y-axis values', family='serif', color='r', weight='normal', size=16, labelpad=6)
Adding Grid to the Plot
You can enable the grid using:
plt.grid(True)
Line Customization Options
Line Colors
Matplotlib provides built-in color aliases:
| Alias | Color |
|---|---|
| b | blue |
| g | green |
| r | red |
| c | cyan |
| m | magenta |
| y | yellow |
| k | black |
| w | white |
Line Styles
Matplotlib supports different line styles:
| Line Style | Description |
| - | Solid Line |
| -- | Dashed Line |
| : | Dotted Line |
| -. | Dash-Dot Line |
Adjusting Line Width
You can modify the line width using the linewidth parameter:
plt.plot(x, y, "r--", linewidth=2)
Or use the abbreviated version:
plt.plot(x, y, "r--", lw=2)
Example:
import matplotlib.pyplot as plt
x = [1,2,3,4,5,6,7,8,9,10]
y = [1,2,3,4,5,6,7,8,9,10]
plt.plot(x, y, "r--", linewidth=5)
plt.title('Graph using Python')
plt.xlabel('X-axis values')
plt.ylabel('Y-axis values')
plt.show()
Conclusion
In this tutorial, we explored how to create and customize line charts in Matplotlib. In the next section, we will explore more advanced customization options and graphing techniques.
Copyright statement: Unless otherwise indicated, all articles are original to this site, please cite the source when sharing.
Article link:http://pybeginners.com/python-data-manipulation/how-to-create-a-line-chart-in-python-with-matplotlib/
License agreement:Creative Commons Attribution-NonCommercial 4.0 International License