How to plot two dotted lines and set marker using Matplotlib?

In this article, we will learn how to plot two dotted lines with custom markers using Matplotlib. Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.

Prerequisites

First, we need to import the matplotlib.pyplot module ?

import matplotlib.pyplot as plt

Pyplot is a collection of command-style functions that make matplotlib work like MATLAB, providing an easy interface for plotting.

Basic Example with Dotted Lines

Let's start with a simple example plotting two dotted lines ?

import matplotlib.pyplot as plt

# Define coordinates for two lines
line1_x = [10, 20, 30]
line1_y = [20, 40, 10]

line2_x = [10, 20, 30]
line2_y = [40, 10, 30]

# Plot both lines with dotted style
plt.plot(line1_x, line1_y, color='blue', linewidth=3, label='Line 1', linestyle='dotted')
plt.plot(line2_x, line2_y, color='red', linewidth=3, label='Line 2', linestyle='dotted')

# Add labels and title
plt.xlabel('X AXIS')
plt.ylabel('Y AXIS')
plt.title('PLOTTING DOTTED LINES')
plt.legend()

plt.show()

Adding Markers to Dotted Lines

We can enhance our plot by adding markers to highlight data points ?

import matplotlib.pyplot as plt

# Define coordinates
line1_x = [10, 20, 30]
line1_y = [20, 40, 10]

line2_x = [10, 20, 30]
line2_y = [40, 10, 30]

# Plot with dotted lines and markers
plt.plot(line1_x, line1_y, color='blue', linewidth=2, label='Line 1', 
         linestyle='dotted', marker='o', markersize=8)
plt.plot(line2_x, line2_y, color='red', linewidth=2, label='Line 2', 
         linestyle='dotted', marker='s', markersize=8)

# Customize the plot
plt.xlabel('X AXIS')
plt.ylabel('Y AXIS')
plt.title('DOTTED LINES WITH MARKERS')
plt.legend()
plt.grid(True, alpha=0.3)

plt.show()

Different Marker Styles

Matplotlib offers various marker styles to choose from ?

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [2, 4, 1, 5, 3]
y2 = [1, 3, 5, 2, 4]
y3 = [3, 1, 4, 3, 5]

# Different marker styles
plt.plot(x, y1, linestyle=':', marker='o', label='Circle markers')
plt.plot(x, y2, linestyle=':', marker='^', label='Triangle markers')  
plt.plot(x, y3, linestyle=':', marker='D', label='Diamond markers')

plt.xlabel('X Values')
plt.ylabel('Y Values')
plt.title('Dotted Lines with Different Markers')
plt.legend()
plt.grid(True, alpha=0.3)

plt.show()

Customizing Line and Marker Properties

Parameter Description Example Values
linestyle Style of the line 'dotted', ':', 'dashed', '--'
marker Marker shape 'o', 's', '^', 'D', '*'
markersize Size of markers 5, 8, 10
linewidth Thickness of line 1, 2, 3

Complete Example

import matplotlib.pyplot as plt

# Sample data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales_2022 = [25, 30, 35, 40, 45]
sales_2023 = [30, 25, 40, 35, 50]

# Create the plot
plt.figure(figsize=(10, 6))

plt.plot(months, sales_2022, color='blue', linewidth=2, 
         linestyle='dotted', marker='o', markersize=8, 
         label='Sales 2022')

plt.plot(months, sales_2023, color='red', linewidth=2, 
         linestyle='dotted', marker='s', markersize=8, 
         label='Sales 2023')

# Customize the plot
plt.xlabel('Months')
plt.ylabel('Sales (in thousands)')
plt.title('Monthly Sales Comparison - Dotted Lines with Markers')
plt.legend()
plt.grid(True, alpha=0.3)

plt.tight_layout()
plt.show()

Conclusion

Plotting dotted lines with markers in Matplotlib is achieved using the linestyle and marker parameters in the plot() function. This combination helps create visually appealing charts that clearly highlight data points while maintaining a distinctive line style.

Updated on: 2026-03-25T17:50:44+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements