Explain Codes LogoExplain Codes Logo

Set markers for individual points on a line

python
matplotlib
plotting
annotations
Anton ShumikhinbyAnton Shumikhin·Mar 6, 2025
TLDR

Straightaway solution: here's the quickest way to emphasize specific points on a line plot using matplotlib:

import matplotlib.pyplot as plt # Brace yourself, coordinates and corresponding markers are coming x, y = [1, 2, 3, 4, 5], [2, 3, 2, 3, 4] markers = ['o', 's', 'D', '^', '*'] # The avengers of markers # Quick and dirty way to plot plt.plot(x, y, linestyle='-') # Road to Infinity War [plt.plot(x[i], y[i], marker=markers[i], ls='') for i in range(len(x))] # Unleashing the heroes plt.show() # Snap! The Endgame

Here, we use ls='' to get rid of the line, plotting only markers (Avengers). The markers assemble according to the markers list.

Marker customizations and tips: The real Endgame

Getting fancy with marker styling

Give the points their props by designating specific roles to them:

import numpy as np import matplotlib.pyplot as plt # Prepare for some randomness np.random.seed(0) x = np.arange(10) y = np.random.rand(10) sizes = np.random.rand(10) * 100 # Introducing: variable sizes. Plot twist! colors = np.random.rand(10) # We love some color # Plotting shenanigans plt.plot(x, y, linestyle='--', color='g') # Plotting our naked line first plt.scatter(x, y, s=sizes, c=colors, cmap='viridis', alpha=0.6, edgecolors='w') # Dressing it up # Fancy pants gradient legend sc = plt.scatter([], [], s=100, c='purple', alpha=0.6, edgecolors='w') # To our color palette: I love you 3000. plt.colorbar(sc, label='Point Intensity') # Labels are love, labels are life plt.show()

By controlling the size, color, and opacity of the markers, we can display the data's hidden treasures. Add some cmap color-flair and let the colorbar work its magic to highlight the data spectrum.

The plot twists no one saw coming: Problems with marking points

Just like a bad Marvel movie, your plots can have their own villains:

  • The Infinity War of Overplotting - Too many markers can make your plot look like a cluttered space battle.
  • The Endgame of Scale - Your points are your heroes, not giant villainous space creatures. So, keep those marker sizes human-sized, will ya?
  • The Ragnarok of Colors - Not everyone sees your world as vibrant as Thor sees Jane. Be mindful of the color choice, especially for those with color vision deficiency.

Age of Shortcuts

Become quicker than Quicksilver using shorthand notations (-gD = green dash-dot line with diamond markers):

plt.plot(x, y, '-gD', markevery=[1, 4]) # Just a plot passing by. Nothing suspicious here.

This Quick-silver method is fast, but lacks the infinite possibilities given by the full methods. Choose your side wisely.

Adding annotations and grids: Sometimes, it's good to overdo things

Annotating markers: Because we don't mind read data

Your plot is a picture, so let it speak a thousand words:

plt.plot(x, y, '-g') # Here's a line plt.scatter(x, y, marker='D') # And here's a magic trick # Now, watch this for i, txt in enumerate(y): plt.annotate(f'{txt:.2f}', (x[i], y[i]), textcoords="offset points", xytext=(0,10), ha='center')

The annotate() function adds insightful labels, transforming your plot from monosyllabic Grunt to gifted sentience.

Gridlines: So good, even the Hulk can't smash them

Grids give your plots some context, allowing your data to become a perfect story:

plt.grid(True, which='both', linestyle='--', linewidth=0.5) # It's a grid thing, you wouldn't understand

With grids, your audience won't just "smash" through your data, they'll appreciate it (like Hulk and his tacos).