7

So I have a graph that runs on an order of magnitude 10000 time steps, and thus I have a lot of data points and the xticks are spaced pretty far apart, which is cool, but I would like to to show on the xaxis the point at which the data is being plotted. In this case the xtick I want to show is 271. So is there a way to just "insert" 271 tick onto the x axis given that I already know what tick I want to display?

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
Ned U
  • 401
  • 2
  • 7
  • 15

1 Answers1

23

If it's not important that the ticks update when panning/zomming (i.e. if the plot is not meant for interactive use), then you can manually set the tick locations with the axes.set_xticks() method. In order to append one location (e.g. 271), you can first get the current tick locations with axes.get_xticks(), and then append 271 to this array.

A short example:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()

ax = fig.add_subplot(111)
ax.plot(np.arange(300))

# Get current tick locations and append 271 to this array
x_ticks = np.append(ax.get_xticks(), 271)

# Set xtick locations to the values of the array `x_ticks`
ax.set_xticks(x_ticks)

plt.show()

This produces

Axes with added tick location

As you can see from the image, a tick has been added for x=271.

sodd
  • 12,482
  • 3
  • 54
  • 62
  • Thank you, Also is there an easy way to get a vertical dashed line about that tick too? – Ned U Aug 12 '13 at 19:08
  • 2
    @NedU Do you want a line only at this tickmark, or at all tickmarks? If you want it only at x=271, you can use `ax.axvline(271, ls='--')`. If you want lines for all tickmarks (on x-axis), you can use `ax.grid(axis='x')`. – sodd Aug 12 '13 at 20:01