1

I am aware of the markevery option that allows to only place a marker at every n-th point. However, I'm using the MaxNLocator to define tick positions and would like to display markers at those tick positions only. How can I display markers at tick positions only, either using an option or manually placing them?

danijar
  • 32,406
  • 45
  • 166
  • 297

1 Answers1

2

You can get the ticks with ax.get_xticks(), find the closest point corresponding to each xtick and then get the index to the x-values. With the indices it is easy to plot the xtick-values.

#!/usr/bin/env python
# a bar plot with errorbars
import numpy as np
import matplotlib.pyplot as plt
plt.close('all')

def find_nearest(array,value):
    idx = (np.abs(array-value)).argmin()
    return idx

x = np.linspace(-1, 1, 100)
y = x**2

fig, ax = plt.subplots(1, 1)

ax.plot(x, y, '--')

xtick = ax.get_xticks()

idx = [find_nearest(x, tick) for tick in xtick]

ax.plot(x[idx], y[idx], 'ro')

plt.show()  
Community
  • 1
  • 1
pathoren
  • 1,634
  • 2
  • 14
  • 22