Denver Dang
Denver Dang

Reputation: 2615

Show custom tick value in plot

Let's say I have made a plot, and in that plot there is a specific point where I draw vertical line from to the x-axis. This point has the x-value 33.55 for example. However, my tick separation is something like 10 or 20 from 0 to 100. So basically: Is there a way in which I can add this single custom value to the tick axis, so it shows together with all the other values that where there before ?

Upvotes: 0

Views: 323

Answers (1)

gmds
gmds

Reputation: 19885

Use np.append to add to the array of ticks:

import numpy as np

from matplotlib import pyplot as plt

x = np.random.rand(100) * 100
y = np.random.rand(100) * 100

fig, ax = plt.subplots(figsize=(8, 6))

ax.scatter(x, y)

ax.set_xticks(np.append(ax.get_xticks(), 33.55))

enter image description here

Note that if your plot is not big enough, the tick labels may overlap.

If you want the new tick to "clear its orbit", so to speak:

special_value = 33.55
black_hole_radius = 10
new_ticks = [value for value in ax.get_xticks() if abs(value - special_value) > black_hole_radius] + [special_value]

ax.set_xticks(new_ticks)

enter image description here

Upvotes: 1

Related Questions