LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.
Python Data Science Matplotlib Line Choose Linestyle Style----Or, 'solid'(default)----'-', 'dotted'----':', 'dashed'----'--', 'dashdot'----'-.'
Linestyle: The keyword argument linestyle, or shorter ls, is used to change the style of the plotted line.
Example: Use a dotted line.
Code
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 6, 2, 13])
plt.plot(ypoints, linestyle = 'dotted')
plt.show()
the output will be
Example 2: Use a dashed line.
Code
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 6, 2, 13])
plt.plot(ypoints, linestyle = 'dashed')
plt.show()
the output will be
Shorter Syntax: The line style that can be written in a shorter syntax.
The linestyle can be written as ls
dotted can be written as :
dashed can be written as --
Example 3: Shorter syntax.
Code
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 6, 2, 13])
plt.plot(ypoints, ls = ':')
plt.show()
the output will be
Line Styles:
You can select any of these styles.
Style ----Or
'solid'(default)----'-'
'dotted' ----':'
'dashed' ----'--'
'dashdot' ----'-.'
'None' ----"or"
Line Color: The keyword argument color or the shorter c can be used to set the color of the line.
Example 4: Set the line color to red
Code
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([2, 6, 1, 15])
plt.plot(ypoints, color = 'r')
plt.show()
the output will be
Example 5: You can also use Hexadecimal color values. Set the line color to green
Code
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, c = '#4CAF50')
plt.show()
the output will be
Example 6: Changing color values with 140 supported color names. Set the line color to 'hotpink'.
Code
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, c = 'hotpink')
plt.show()
the output will be
Line Width: The keyword argument linewidth or the shorter lw is used to change the width of the line.
The value is a floating number, in points.
Example 7: Plot with a 20.5pt wide line.
Code
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([4, 9, 3, 18])
plt.plot(ypoints, linewidth = '20.5')
plt.show()
the outputwill be
Multiple Lines: You can plot as many lines as you like by simply adding more plt.plot() functions.
Example 8: Draw two lines by specifying a plt.plot() function for each line
Code
import matplotlib.pyplot as plt
import numpy as np
y1 = np.array([4, 8, 1, 10])
y2 = np.array([5, 2, 7, 11])
plt.plot(y1)
plt.plot(y2)
plt.show()
the output will be
Plot Many Lines By adding the Points for the X- and Y-axis.
One can also plot many lines by adding the points for the x- and y-axis
for each line in the same plt.plot() function.
(In the example 8 we only specified the points on the y-axis, meaning that:
the points on the x-axis got the the default values (0, 1, 2, 3).)
The x- and y- values come in pairs.
Example 9: Draw two lines by specifiyng the x- and y-point values for both lines.
Code
import matplotlib.pyplot as plt
import numpy as np
x1 = np.array([0, 1, 2, 3])
y1 = np.array([4, 8, 1, 9])
x2 = np.array([0, 1, 2, 3])
y2 = np.array([7, 2, 8, 10])
plt.plot(x1, y1, x2, y2)
plt.show()
the output will be