How to stop x labels from displaying after a certain value

33 Views Asked by At

I'm having the hardest time achieving the following:
I need to stop the x labels from showing after the vertical dashed line seen in the image:

Basically, I want to eliminate the 5 in this example.

I successfully stopped the ticks with the condition placed on "set_ticks", but the labels keep displaying.

My code:

ax2 = plt.subplot()
ax2.pcolormesh(xext, ye, Zext, cmap='hot')
ax2.set_xticks([a for a in xext if a<myval], minor=True)
ax2.axvline(myval, linestyle='--', color='white')
plt.show()

A solution like writing a list of [1,2,3,4] would not help me.

I need this to work for a large number of examples where all I know is the limit value, or myval from the code above.

(I am restating and narrowing down a question I posted before, now deleted.)

1

There are 1 best solutions below

1
On BEST ANSWER

You only changed the minor ticks (the very short lines) on the x-axis. The major ticks also should be changed. ax.get_xticks() gives a list of the current ticks, which you can filter and apply again:

import matplotlib.pyplot as plt
import numpy as np

fig, ax2 = plt.subplots()
xext = np.arange(9.1, step=.1)
ye = np.arange(6)
zext = np.random.randn(len(ye) - 1, len(xext) - 1).cumsum(axis=1)
zext -= zext.mean(axis=1, keepdims=True)
ax2.pcolormesh(xext, ye, zext, cmap='hot')
myval = 6.28
ax2.set_xticks([a for a in xext if a < myval], minor=True)
xticks = ax2.get_xticks()
ax2.set_xticks([a for a in xticks if a < myval])
ax2.axvline(myval, linestyle='--', color='white')
plt.show()

example plot