Matplotlib Linestyle and It’s Customizations in Python

In this article, we will discuss Matplotlib Linestyle in Python. The Matplotlib library of Python is used for data visualization due to its wide variety of chart types. It has properties that can be manipulated to create chart styles. The matplotlib.pyplot.plot(*args, **kwargs) method of matplotlib.pyplot is used to plot the graphs. We can specify the graph style like color or line style. We look into various ways of implementing linestyles in Python. Before that, let me brief you on the different Matplotlib linestyles available.

Different Matplotlib Linestyle in Python

CharacterDefinition
Solid line
Dashed line
-.dash-dot line
Dotted line
. Point marker
oCircle marker
,Pixel marker
vtriangle_down marker
^triangle_up marker
< triangle_left marker
>triangle_right marker
1tri_down marker
2tri_up marker
3tri_left marker
4 tri_right marker
ssquare marker
ppentagon marker
*star marker
hhexagon1 marker
Hhexagon2 marker
+Plus marker
xX marker
DDiamond marker
dthin_diamond marker
| vline marker

Color codes used with Matplotlib Linestyle

bblue
ggreen
rred
ccyan
mmagenta
yyellow
kblack
wwhite

Example of Matplotlib Linestyle

import matplotlib.pyplot as plt 
import random as random 
  
students = ["Jane","Joe","Beck","Tom", 
            "Sam","Eva","Samuel","Jack", 
            "Dana","Ester","Carla","Steve", 
            "Fallon","Liam","Culhane","Candance", 
            "Ana","Mari","Steffi","Adam"] 
  
marks=[] 
for i in range(0,len(students)): 
     marks.append(random.randint(0, 101)) 
  
plt.xlabel("Students") 
plt.ylabel("Marks") 
plt.title("CLASS RECORDS") 
plt.plot(students,marks,'m--')

OUTPUT:

Example of Matplotlib Linestyle

EXPLANATION:

In the above example, firstly, we import the matplotlib.pyplot library. The ‘students’ list is created to add student’s names. The marks list is created with the random.randint() method. Then, the X and Y axis are labeled, and the graph is given a title. Finally, the graph is plotted using matplotlib.pyplot.plot() method. Abbreviated color code and line style are used. The color code chosen is ‘m’, which is magenta, and the line style chosen is ‘–, ‘which is dashed line style.

Matplotlib Linestyle color

from matplotlib import pyplot as plt
import numpy as np

xa = np.linspace(0, 5, 20)

ya = xa**2
plt.plot(xa, ya, color='lightcoral')

ya = 3*xa
plt.plot(xa, ya, color='#4b0082')  #A shade of purple

plt.show()

OUTPUT:

Linestyle color

EXPLANATION:

Apart from single character colors, we can also implement different shades of a color to Matplotlib linestyle. The plt.plot function has a collection of optional parameters to do so. Single character colors are implemented as ‘r,’ ‘g,’ etc. There are 140 shades of CSS Color names such as sea green, steel-blue, tomato, etc. For Matplotlib Linestyle, the names should be written in lower case. The RGB colors can be implemented as “#rrggbb,” which indicates the color as a six-digit hex value, which indicates the amount of red, green, and blue each as a 2 digit hex value (00 to FF). In the above example, both CSS color names and RGB color is used for the Matplotlib Linestyle in Python.

Customized Linestyles

from matplotlib import pyplot as plt
import numpy as np

xa = np.linspace(0, 5, 20)

ya = xa**2
plt.plot(xa, ya, color='lightcoral', linewidth=4, linestyle=':')

ya = 3*xa
plt.plot(xa, ya, color='#4b0082', linewidth=6,
         linestyle=(0, (5, 2, 1, 2)), dash_capstyle='round')

plt.show()

OUTPUT:

Customized Linestyles

EXPLANATION:

We can customize linestyles in Matplotlib Python. We can modify the width of the plotline using the linewidth parameter. For the default plot, the line width is in pixels. So we will typically use 1 for a thin line, 2 for a medium line, 4 for a thick line, or more if we want a really thick line. We can set the line style using the linestyle parameter. It can take strings such as “–,” “-.” etc. Alternatively, it can take a structure like this: (offset,(on, off, on, off….)).

The ‘offset,’ the first parameter, is the length before the pattern starts. This is usually zero. The second value is a tuple of on/off values. In this example, the value is (5, 2, 1, 2) for the first plot, which means the line will consist of a dash of 5 units, a gap of 2 units, a dash of 1 unit, a gap of 2 units. A “unit” in this case represents the width of the line. This pattern is repeated for the entire length of the line. The purple plot also has dash_capstyle set to “round.” This means each dash has rounded ends instead of being rectangular.

Matplotlib Linestyle along with Markers

from matplotlib import pyplot as plt
import numpy as np

xa = np.linspace(0, 5, 20)

ya = xa**2
plt.plot(xa, ya, 'g-o')

ya = 3*xa
plt.plot(xa, ya, 'r-s')

plt.show()

OUTPUT:

Matplotlib Linestyle along with Markers

EXPLANATION:

A marker is a symbol like a small dot, square, diamond, etc. That indicates a data point on the graph. With Matplotlib linestyle, markers can be controlled by a simple text string or by a set of parameters that give more options. The markers appear at the data points that we define. Since we have used np.linspace to define 20 equally spaced data points between x=0 and x=5, we see 20 markers at these points. The “g-o” specifies a green curve, a solid line, and a circle marker (the letter o). And the “r-s” specifies a red curve, a solid line, and a square marker (the letter s).

Must Read

Matplotlib Marker in Python With Examples and Illustrations
Numpy Gradient | Descent Optimizer of Neural Networks
Matplotlib Table in Python With Examples
Matplotlib Boxplot With Customization in Python

Conclusion

In this article, we have learned about different examples of using Matplotlib Linestyle in Python. We discussed three ways of setting color for the linestyles. Also, the linestyle can be customized as per the user’s desire based on parameters like line width, dash_capstyle, etc. Markers can also be used along with linestyles at required data points. Refer to this article in case of any doubt regarding the Matplotlib Linestyles.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments