Discussion:
[Matplotlib-users] Hide overlapping tick marks in axes corner
Julian Irwin
2016-01-28 19:49:33 UTC
Permalink
Hello,

I am looking for a way to hide tick marks (not the labels!) that coincide
with axis lines. I think this is a problem for me because of the relative
line thicknesses of my axis lines and tick marks, but I want to leave those
thicknesses unchanged (I like the look of the thickness settings I am using
now).

Here is a screenshot of what I'm talking about:

[image: Inline image 1]

I know this looks minor, but it is quite obvious on some plots and I'd
really like to get rid of it.

Thanks,
Julian Irwin
Oscar Benjamin
2016-01-29 16:49:40 UTC
Permalink
I am looking for a way to hide tick marks (not the labels!) that coincide with axis lines. I think this is a problem for me because of the relative line thicknesses of my axis lines and tick marks, but I want to leave those thicknesses unchanged (I like the look of the thickness settings I am using now).
Try this:

from matplotlib import pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot([0, 1], [0, 1])
print(ax.get_xticks())
ax.set_xticks(ax.get_xticks()[1:-1]) # Remove first and last ticks
print(ax.get_xticks())

--
Oscar
Julian Irwin
2016-01-31 19:53:03 UTC
Permalink
Thanks for your suggestion Oscar. I tried editing the ticks like this, but
this method removes both the tick marks and the labels.

I think I have found a decent solution. Unfortunately my solution required
a very particular order of operations. It is much less convenient than the
functions provided in the API like tick_params(), which don't care if you
have run plt.draw() ahead of time...

1) Run all of the setup for the plot and also the plotting commands
(ax.plot(), ax.hist()...whatever)

2) Run `plt.draw()` because this updates the tick objects contained in
your axes.

3) Grab your Tick objects: `ticks = ax.[x/y]axis.[major/minor]Ticks`

4) For each tick you want to hide do:
`tick.tick1On = False`
`tick.tick2On = False`
The `1` and `2` refer to the bottom, top (left, right) for the x (y) axis
respectively.

5) Run plt.show(), fig.show() or fig.savefig or whatever else you are using.

Ahhhhh, no messy ticks in the corner!

Julian
Post by Julian Irwin
I am looking for a way to hide tick marks (not the labels!) that
coincide with axis lines. I think this is a problem for me because of the
relative line thicknesses of my axis lines and tick marks, but I want to
leave those thicknesses unchanged (I like the look of the thickness
settings I am using now).
from matplotlib import pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot([0, 1], [0, 1])
print(ax.get_xticks())
ax.set_xticks(ax.get_xticks()[1:-1]) # Remove first and last ticks
print(ax.get_xticks())
--
Oscar
Loading...