When creating visualizations in Matplotlib, customizing markers and colors is necessary to make data more engaging and easier to interpret. In this tutorial we will explore how to change marker, styles, colors, and add custom combinations for a professional-looking plot. Markers help indicate specific data points on a line or scatter plot. Matplotlib enables you to change marker type style, marker size, and colors.... The Matplotlib provides several markers, including circles, triangles, squares and more. The commonly used marker styles are:
Example 1 - Changing the marker type
For this example we will need two libraries i.e. numpy as np and matplotlib.pyplot as plt. The numpy is required to generate the data while matplotlib.pyplot module is required for plotting the data.
However in this example we want to show different types of the markers so we will show the original scatter plot with x,y coordinates. Then we will increase the values of y by adding the value of 0.5 to the coordinates multiple times.
So in one plot we will graphically represent 6 diffrent sine functions and these are: \begin{eqnarray} y_1 &=& \sin(x)\\ \nonumber y_2 &=& \sin(x+0.5)\\ \nonumber y_3 &=& \sin(x+1)\\ \nonumber y_4 &=& \sin(x+1.5)\\ \nonumber y_5 &=& \sin(x+2)\\ \nonumber y_6 &=& \sin(x+2.5)\\ \nonumber \end{eqnarray} It should be noted that first \(y_1\) points will be visualized using circle type marker ('o'), the \(y_2\) will be visualized using square type marker ('s'), the \(y_3\) will be visualized using traingle type marker ('^'), the \(y_4\) will be visualized using diamond marker ('D'),and the \(y_5\) marker will be visualized using x shape marker ('X').
Figure 1 - The scatter plot with customized markers
Example 2 - Customizing marker size and edge color
Besides the markerd type the matplotlib library offers you to customize the marker size and the edge color. In this example we will use the same libraries as in the previous example as well as the same data. The code for defining libraries and dataset is given below.
Figure 2 - Scatter plot with customized markers (marker size and edge color).
Example 3 - Changing marker colors
There are few methods how colors can be specified in the matplotlib i.e. using color names, hex codes, and RGB tuples. In all the cases we will use the same type of dataset as before. Here is the entire code how to generate the dataset with libraries.
Figure 3 - The scatter plot with customized colored markers.
- o - circle
- s - square
- "^" - triangle up
- "v" - triangle down
- "D" - diamond
- "X" - x shape
import numpy as npThe x coordinates will be generated using the np.linspacE() function while y coordinates will be generated using the np.sin() function where x coordinates will be input.
import matplotlib.pyplot as plt
x = np.linspace(0,10,10)The output of the script up to this point is given below.
y = np.sin(x)
print("x = {}".format(x))
print("y = {}".format(y))
x = array ([ 0. , 1.11111111 , 2.22222222, 3.33333333 , 4.44444444 , 5.55555556 , 6.66666667 , 7.77777778 , 8.88888889 , 10.])The basic code for creating the scatter plot is given below.
y = array ([ 0. , 0.8961922 , 0.79522006 ,-0.19056796 , -0.96431712 ,4 -0.66510151 , 0.37415123 , 0.99709789 , 0.51060568 ,-0.54402111])
plt.figure(figsize=(12,8))Using plt.fgiure(figsize=(12,8)) we have created an empty figure with size 12 by 8 inches. The plt.scatter(x,y, marker='o', label='Circle Marker') will create the scatter plot with points having x and y coordinates, marker = 'o' is the Circle marker that is used by default in the scatter plot. The label is the name that will be display in legend if the plt.legend is defined. The plt.grid(True) defines the grid which is added to the plot and the plt.show() will show the plot.
plt.scatter(x,y,marker='o', label='Circle Marker')
plt.grid(True)
plt.show()
However in this example we want to show different types of the markers so we will show the original scatter plot with x,y coordinates. Then we will increase the values of y by adding the value of 0.5 to the coordinates multiple times.
So in one plot we will graphically represent 6 diffrent sine functions and these are: \begin{eqnarray} y_1 &=& \sin(x)\\ \nonumber y_2 &=& \sin(x+0.5)\\ \nonumber y_3 &=& \sin(x+1)\\ \nonumber y_4 &=& \sin(x+1.5)\\ \nonumber y_5 &=& \sin(x+2)\\ \nonumber y_6 &=& \sin(x+2.5)\\ \nonumber \end{eqnarray} It should be noted that first \(y_1\) points will be visualized using circle type marker ('o'), the \(y_2\) will be visualized using square type marker ('s'), the \(y_3\) will be visualized using traingle type marker ('^'), the \(y_4\) will be visualized using diamond marker ('D'),and the \(y_5\) marker will be visualized using x shape marker ('X').
plt.scatter(x,y,marker='o', label='Circle Marker')Usin the previous code in one figure we will visualize 6 different sine function using different markers. The entire code created in this example is shown below.
plt.scatter(x,y+0.5,marker='s', label='Square Marker')
plt.scatter(x,y+1,marker='^', label='Traingle Up Marker')
plt.scatter(x,y+1.5,marker='v', label='Traingle Down Marker')
plt.scatter(x,y+2,marker='D', label='Diamond Marker')
plt.scatter(x,y+2.5,marker='X', label='X Shape Marker')
import numpy as npIn the previous code the plt.legend() function was used to show the labels of each scatte plot in the plot legend. After executing the previous code the result is shwon in Figure 1.
import matplotlib.pyplot as plt
x = np.linspace(0,10,10)
y = np.sin(x)
print("x = {}".format(x))
print("y = {}".format(y))
plt.figure(figsize=(12,8))
plt.scatter(x,y,marker='o', label='Circle Marker')
plt.scatter(x,y+0.5,marker='s', label='Square Marker')
plt.scatter(x,y+1,marker='^', label='Traingle Up Marker')
plt.scatter(x,y+1.5,marker='v', label='Traingle Down Marker')
plt.scatter(x,y+2,marker='D', label='Diamond Marker')
plt.scatter(x,y+2.5,marker='X', label='X Shape Marker')
plt.grid(True)
plt.legend()
plt.show()
import numpy as npThen we will create the scatter plot with marker type 'o' (circle), we will set the markersize to 200 (s = 200), marker edge width to 2 (linewidths = 2),and marker edge color to black (edgecolor = 'black'). The second set of points will be created with the offset of y-coordiantes by 2. The same marker type will be used, the markersize will be set to 300, the line width to 5, and the edge color to black. The label of the first set of points (x,y) will be Customized Marker_1 while the label of the second set of points (x,y+2) will be Customized Marker_2
import matplotlib.pyplot as plt
x = np.linspace(0,10,10)
y = np.sin(x)
plt.figure(figsize=(12,8))The entire code used in this example is shown below.
plt.scatter(x,y, marker = 'o', s = 200, linewidth = 2, edgecolor = 'black', label="Customized Marker")
plt.grid(True)
plt.legend()
plt.show()
import numpy as npAfter executing the previous code the scatter plot with two set of points where each set has its own customized marker size and edge color as shown in Figure 2.
import matplotlib.pyplot as plt
x = np.linspace(0,10,10)
y = np.sin(x)
print("x = {}".format(x))
print("y = {}".format(y))
plt.figure(figsize=(12,8))
plt.scatter(x,y, marker = 'o', s = 200, linewidth = 2, edgecolor = 'black', label="Customized Marker_1")
plt.scatter(x,y+2, marker = 'o', s = 300, linewidth = 5, edgecolor = 'black', label="Customized Marker_2")
plt.grid(True)
plt.legend()
plt.show()
import numpy as npIn this example we will define the color in three different ways:
import matplotlib.pyplot as plt
x = np.linspace(0,10,10)
y = np.sin(x)
- using color names ('red', 'blue', 'green',...)
- using hex codes (#000000, #2ca02c, #c3b530,...), and
- using red, green, and blue (RGB) normalized values in a tuple format. These tuples can define any color by setting the values from 0 to 1 (normalzed) for red, green, and blue.
plt.scatter(x,y, color = 'red')The next two functions \(y_3 = \sin(x)+ 1\) and the \(y_4 = \sin(x) + 1.5\) will be shown with #0000FF and #FF9A00 colors respectively.
plt.scatter(x,y+0.5, color = 'green')
plt.scatter(x,y+1, color = #ebebeb)The final two functions \(y_5 = \sin(x)+2\) and \(y_6 = \sin(x) + 2.5\) will be shown using colors (0.2, 0.6, 0.1) and (0.4, 0.23, 0.48).
plt.scatter(x,y+1.5, color = #FF9A00)
plt.scatter(x,y+2, color = (0.2, 0.6, 0.1))The entire code created in this example is shown below.
plt.scatter(x,y+2.5, color = (0.4, 0.23, 0.48))
import numpy as npThe result is shown in Figure 3.
import matplotlib.pyplot as plt
x = np.linspace(0,10,10) y = np.sin(x)
print("x = {}".format(x))
print("y = {}".format(y))plt.figure(figsize=(12,8))
plt.scatter(x,y, color = 'red')
plt.scatter(x,y+0.5, color = 'green')
plt.scatter(x,y+1, color = "#0000FF")
plt.scatter(x,y+1.5, color = "#FF9A00")
plt.scatter(x,y+2, color = (0.2, 0.6, 0.1))
plt.scatter(x,y+2.5, color = (0.4, 0.23, 0.48))
plt.grid(True)
plt.show()