Change the line markers in Matplotlib

Python

Matplotlib

Line

Change markers

Luc B.

Python

Matplotlib

Line Plot

Sometimes, line plots are better plotted with dots, circles, triangles, or diamonds as opposed to just a line.

Code Example

Use the format string parameter to plt.plot() to change the marker style in Matplotlib.

import matplotlib.pyplot as plt
import numpy as np

x_values = np.arange(0, 7)
y_values = np.array([0.27, 0.32, 0.35, 0.49, 0.55, 0.7, 0.95])

# Adjust the marker style here
plt.plot(x_values, y_values, ".")
plt.plot(x_values, y_values + 0.5, "o")
plt.plot(x_values, y_values + 1, "^")

plt.legend(['Line 1', 'Line 2', 'Line 3'])
plt.show()

png

For a list of valid marker strings, see this page in the Matplotlib docs

More Examples

Use the marker Parameter

For more control, use the marker parameter to plt.plot() instead of the format string. This allows you to keep the line between the marker points, among other things.

import matplotlib.pyplot as plt

x_values = [1, 2, 3, 4]
y_values = [0.27, 0.32, 0.35, 0.49]

# Adjust the marker style here
plt.plot(x_values, y_values, marker="D")

plt.show()

png