In This Article, You Will Learn About Python Matplotlib Line.
Python Matplotlib Line – Before moving ahead, let’s take a look at Python Matplotlib Markers
Table of Contents
Matplotlib Line
To change the style of plotted line, we can use the keyword argument linestyle.
Example: Using keyword argument “linestyle” to create a dashed line.
import matplotlib.pyplot as plt
import numpy as np
x = np.array([15, 10, 8, 4, 2])
plt.plot(x, linestyle = 'dashed')
plt.show()
As shown above, it returned a dashed line graph.
Example: Using keyword argument “linestyle” to create a dotted line.
import matplotlib.pyplot as plt
import numpy as np
x = np.array([15, 10, 8, 4, 2])
plt.plot(x, linestyle = 'dotted')
plt.show()
As a result, it returned a dotted line graph.
Shorter Syntax
The keyword argument linestyle can be written as –
linestyle as ls
dotted as :
dashed as —
Example: Using shorter syntax for creating a dashed line.
import matplotlib.pyplot as plt
import numpy as np
x = np.array([15, 10, 8, 4, 2])
plt.plot(x, ls = '--')
plt.show()
As can be seen, it returned a dashed line graph.
Example: Using shorter syntax for creating a dotted line.
import matplotlib.pyplot as plt
import numpy as np
x = np.array([15, 10, 8, 4, 2])
plt.plot(x, ls = ':')
plt.show()
As has been noted, it returned a dotted line graph.
Line Style
There are a few more styles –
Style | Short Syntax |
‘solid’ (default) | ‘-‘ |
‘dotted’ | ‘:’ |
‘dashed’ | ‘-.’ |
‘None’ | ‘ ‘ |
Line Color
To set the color of line, we can use keyword argument color.
Example: – Using keyword argument “color” to set a line color.
import matplotlib.pyplot as plt
import numpy as np
x = np.array([15, 10, 8, 4, 2])
plt.plot(x, color = 'g')
plt.show()
As can be seen, it returned a green line graph.
Line Width
To set the width of the line, we can use keyword argument linewidth or lw (short version).
The keyword argument “linewidth” takes float value in string as points.
Example: Using keyword argument “linewidth” to create a line of 10.8 width.
import matplotlib.pyplot as plt
import numpy as np
x = np.array([15, 10, 8, 4, 2])
plt.plot(x, linewidth = '10.8')
plt.show()
As a result, it returned a line with 10.8 width.
Multiple Lines
By using plt.plot() function, we can plot as many as want to plot.
Example: Drawing two different lines by using plt.plot() function.
import matplotlib.pyplot as plt
import numpy as np
plot_1 = np.array([15, 10, 8, 4, 2])
plot_2 = np.array([5, 10, 6, 8, 4])
plt.plot(plot_1)
plt.plot(plot_2)
plt.show()
As can be seen, it returned a graph with plotting two lines.
We can also plot as many as line wants to plot by adding for the points x-axis and y-axis for each line with the same plt.plot() function.
Note: In above example, graph had only y-axis point, means x-axis point was assumed by default i.e., (0, 1, 2, 3, 4).
Note: The value of x-axis and y-axis comes in pair.
Example: Drawing two lines by adding the x-axis and y-axis values for both lines.
import matplotlib.pyplot as plt
import numpy as np
y1_axis = np.array([15, 9, 8, 4, 2])
x1_axis = np.array([5, 10, 6, 8, 4])
y2_axis = np.array([8, 10, 8, 4, 2])
x2_axis = np.array([5, 4, 6, 8, 12])
plt.plot(y1_axis, x1_axis, y2_axis, x2_axis)
plt.show()
As shown above, it returned a graph with plotting two lines.