The matplotlib offers a variety of line styles which could be used to customize the appearance of lines in your plot. The line styles include solid, dotted, dashed, dasheddot, loosley dotted, dotted, densely dotted, long dash with offset, loosely dashed, dashed, densely dashed, loosely dashdotted, dashdotted, densely dashdotted, dashdotdotted, loosely dashdotted, and densely dashdotted.
In the following example we will create a simple plot. For this plot we need to import required librarires (numpy, matplotlib), create data that will be plotted, and plot the data using the plt.plot function.
First step - import required libraires.
import numpy as np import matplotlib.pyplot as plt
Second ste- generate the data that will be plotted. We will create x and y coordinates. The x corrdinates will be created using np.linspace function in 0 to 10 range and it will contain 100 values. The y values will be created using np.sin() function.
x = np.linspace(0,10,100) y = np.sin(x)
Third step - Defining the plot parameters and showing the plot. The size of the plot will be defined using plt.figure and the size will be defined using the figsize parameter. We will set the size 12 by 8 inches. Then the line plot will be created using plt.plot function where arguments of this function are previously created x and y coordaintes. We will add the plt.title() function, set the plt.grid(True) function and finally show the plot using plt.show() function.
plt.figure(figsize=(12,8)) plt.plot(x,y) plt.title("Simple Sine Function") plt.grid(True)> plt.show()
The result is shwon in Figure 1.
Figure 1 - simple plot of the size function.
In this example we will customize the line showing sine function by setting the line stlye to dashed which is done with linestyle parameter. Then we will set the linewidth or width of the line to 10 points, where 1 point equals 1/72 of an inch.Finally we will set the color of the line plot to black. The modificiations are shown in the following block.
plt.figure(figsize=(12,8)) plt.plot(x,y,linestyle='--', linewidth=10, color = 'black') plt.title("Simple Sine Function") plt.grid(True) plt.show()
The results of the preivous code is shown in Figure 2.
Figure 2 - Sine plot with customized line
This tutorial demonstrated how to customize lines in Matplotlib to enhance your plots. If you have any comments, suggestions, or questions, feel free to share them below. Your feedback is always appreciated!