Python Matplotlib Markers Module Examples
Below are some examples of Matplotlib.markers module in Python:
Example 1: Visualizing Multiple Marker Shapes
In this example, a scatter plot is generated using Matplotlib, presenting three distinct marker shapes: circles, squares, and crosses. Each marker type corresponds to a dataset derived from a base set of data points, offering a visual comparison of their distribution.
Python3
import matplotlib.pyplot as plt data_x = [ 1 , 2 , 3 , 4 ] data_y1 = [ 1 , 4 , 9 , 16 ] data_y2 = [y_val + 2 for y_val in data_y1] data_y3 = [y_val + 4 for y_val in data_y1] plt.scatter(data_x, data_y1, marker = 'o' , label = 'Circle' ) plt.scatter(data_x, data_y2, marker = 's' , label = 'Square' ) plt.scatter(data_x, data_y3, marker = 'x' , label = 'Cross' ) plt.legend() plt.show() |
Output:
Example 2: Exploring Line Filling Styles in Matplotlib Markers
In this example, various line filling styles provided by Matplotlib are visualized using distinct markers. Each line consists of five identical points, and the filling style for each line iteration is displayed alongside its respective markers.
Python3
import numpy as np import matplotlib.pyplot as plt from matplotlib.lines import Line2D # Draw 5 points for each line each_point = np.ones( 5 ) style = dict (color = 'tab:green' , linestyle = ':' , marker = 'D' , markersize = 15 , markerfacecoloralt = 'tab:red' ) figure, axes = plt.subplots() # Plot all filling styles. for y, fill_style in enumerate (Line2D.fillStyles): axes.text( - 0.5 , y, repr (fill_style), horizontalalignment = 'center' , verticalalignment = 'center' ) axes.plot(y * each_point, fillstyle = fill_style, * * style) axes.set_axis_off() axes.set_title( 'filling style' ) plt.show() |
Output:
Example 3: Visualizing Filled Markers in Matplotlib
In this example, the Matplotlib library is utilized to showcase filled marker styles across two subplots. Each subplot displays a set of filled markers, with their textual representations alongside, arranged in distinct columns.
Python3
import numpy as np import matplotlib.pyplot as plt from matplotlib.lines import Line2D # Drawing 3 points for each line plotted_points = np.ones( 4 ) txt_style = dict (horizontalalignment = 'right' , verticalalignment = 'center' , fontsize = 12 , fontdict = { 'family' : 'monospace' }) style = dict (linestyle = ':' , color = '0.5' , markersize = 10 , mfc = "C0" , mec = "C0" ) # helper function for axes formatting def format_ax(ax): ax.margins( 0.2 ) ax.set_axis_off() ax.invert_yaxis() # helper function for splitting list def split(a_list): i_half = len (a_list) / / 2 return (a_list[:i_half], a_list[i_half:]) figure, axes = plt.subplots(ncols = 2 ) for ax, markers in zip (axes, split(Line2D.filled_markers)): for y, marker in enumerate (markers): ax.text( - 0.5 , y, repr (marker), * * txt_style) ax.plot(y * plotted_points, marker = marker, * * style) format_ax(ax) figure.suptitle( 'filled markers' , fontsize = 14 ) plt.show() |
Output:
Matplotlib.markers module in Python
Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. The matplotlib.markers
module provides a collection of marker styles used for data visualization in Matplotlib plots. These markers represent individual data points, enhancing clarity and distinction within graphs and charts.