Change the marker frequency in Matplotlib




Change marker frequency

Luc B.



Line Plot

When data sets are large, Matplotlib becomes quite slow and can impede the exploratory phase of data analysis. An easy workaround is to plot just a subset of points instead of the entire data set. Matplotlib makes this easy with its markevery API.

While this approach allows users to quickly get a feel for data without throttling their laptop fans, data analysts shouldn't use subsampled data in final results unless explicitly specified in the methodology, as silently omitting data points is a dubious practice. In general, grouped sums and means are better methods of slimming down data that don't explicitly omit data.

Code Example

Use the markevery parameter to plt.plot() to change the marker frequency and only plot a subset of data points.

import matplotlib.pyplot as plt
import numpy as np

data = np.linspace(0, 10, 100)

# Change the marker frequency here
plt.plot(data, data, 'o', markevery=5)    # Mark every 5 points
plt.plot(data, data - 2, 'o')             # Mark every point

plt.legend(['Mark Every 5', 'Mark All'])