Saturday 26 August 2023

Customizing Marker Styles in Matplotlib

Markers are small symbols like circle, square, cross etc., placed at each data point in a plot. Using ‘marker’ argument of plot method, we can specify the marker style at data points in a plot.

 

Following marker styles are supported.

a.   'o' (circle),

b.   's' (square),

c.    'x' (cross),

d.   '+' (plus sign) etc.,

 

marker_styles.py

import matplotlib.pyplot as plt
import numpy as np

# Sample data
x1 = [5, 10, 15, 20, 25]
y1 = [20, 40, 60, 80, 100]

x2 = [10, 20, 30, 40, 50]
y2 = [30, 60, 90, 120, 150]

x3 = [15, 30, 45, 60, 75]
y3 = [8, 16, 24, 32, 40]

x4 = [12, 24, 36, 48, 60]
y4 = [15, 30, 45, 60, 75]

plt.plot(x1, y1, color='red', label='solid-line', linestyle='-', marker='o')
plt.plot(x2, y2, color='g', label='dashed-line', linestyle='--', marker='s')
plt.plot(x3, y3, color='#0000FF', label='dotted-line', linestyle=':', marker='x')
plt.plot(x4, y4, color=(0, 1, 1), label='dashed-dotted-line', linestyle='-.', marker='+')

# To display the legend on the plot
plt.legend()

plt.show()

 

Output


  

Previous                                                    Next                                                    Home

No comments:

Post a Comment