In this Matplotlib Tutorial we are going to learn How to Control Matplotlib Styles, so you can customize Matplotlib styles in three sections, you can customize Colors, Line Styles and Marker Styles.
Also you can check More GUI Development Tutorials in the below link.
1: PyQt5 GUI Development Tutorials
2: TKinter GUI Development Tutorials
3: Pyside2 GUI Development Tutorials
4: Kivy GUI Development Tutorials
Controlling Colors
Sometimes when you are plotting, you want to customize the colors for Matplotlib graph, you want to Control Matplotlib Styles , for example if you are plotting lines, you can customize every color for lines.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import matplotlib.pyplot as plt import numpy as np #our data x = np.array([1,2,3]) #plotting three lines with three colors plt.plot(x, 'y') plt.plot(x+1, 'm') plt.plot(x+2, 'c') #showing the plot plt.show() |
So you can see in the above code we have specified the color for our lines.
If you run the code this will be the result.
There are different colors in Matplotlib, this is the table for Matplotlib colors.
Color | Color Name |
b | blue |
c | cyan |
g | green |
k | black |
m | magenta |
r | red |
w | white |
y | yellow |
Controlling Line Styles
The lines that we have saw in the above example, it is just a line without any dots or dashes, in Matplotlib also you can use different line styles.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import matplotlib.pyplot as plt import numpy as np #our data x = np.array([1,2,3]) #plotting three lines with three colors plt.plot(x, '--', x+1, '-.', x+2, ':',x+3,'-') #showing the plot plt.show() |
So you can see that in the plot function we have used our line styles.
If you run the complete code this will be the result.
There are different line styles in Matplotlib, this is the table for Matplotlib line styles.
Style Abbreviation | Style |
– | Solid Line |
— | Dashed Line |
-. | Dash Dot Line |
: | Dotted Line |
Control Marker Styles
Matplotlib provides a lot of customization options for markers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import matplotlib.pyplot as plt import numpy as np #our data x = np.array([1,2,3]) #plotting markers plt.plot(x, 'x', x+0.7, 'o', x+1, 'D', x+1.5, '^', x+2, 's') #showing the plot plt.show() |
If you run the code this will be the result.
There are different Marker Styles in Matplotlib that you can, we have just used some of them in this table.
Marker abbreviation | Marker style |
. | Point marker |
, | Pixel marker |
o | Circle marker |
v | Triangle down |
_ | Horizontal line |
| | Vertical line |
d | Thin diamond |
D | Diamond marker |
+ | Plus marker |
Subscribe and Get Free Video Courses & Articles in your Email