[Matplotlib-users] trouble removing tick labels

Slavin, Jonathan jslavin at cfa.harvard.edu
Fri Mar 20 14:56:19 EDT 2020


Hi Eric,

I think I've isolated the problem. In my figure I have been including
colorbars for each plot. In my simple example, which I include below,
label_outer works without the colorbars but not with them.

Regarding using sharey, I am using the same y-axis in each row, but they
differ between rows. If I use sharey, things get all messed up - the
smaller images are shrunken and only take up part of the figure. In the end
I was able to turn off the tick labelling by doing things like:
for label in ax.get_yticklabels(which='both'):
            label.set_visible(False)

I think this may have to do with the attributes of axs getting overwritten
or something. If I print
axs[1,1].get_subplotspec().colspan.start
in the case with colorbars I get 0, whereas if I do it without colorbars, I
get 1 as I should. Same applies to other axes and to rows.

Here's the minimal example:

import numpy as np
import matplotlib.pyplot as plt

fig,axs = plt.subplots(2,2)
img0 = np.random.random((10,10))
img1 = np.random.random((10,10))
img2 = np.random.random((10,10))
img3 = np.random.random((10,10))
cax0 = axs[0,0].imshow(img0,extent=[0.,10.,0.,10.])
fig.colorbar(cax0,ax=axs[0,0],pad=0.01)
cax1 = axs[0,1].imshow(img1,extent=[0.,10.,0.,10.])
fig.colorbar(cax1,ax=axs[0,1],pad=0.01)
cax2 = axs[1,0].imshow(img2,extent=[0.,10.,0.,10.])
fig.colorbar(cax2,ax=axs[1,0],pad=0.01)
cax3 = axs[1,1].imshow(img3,extent=[0.,10.,0.,10.])
fig.colorbar(cax3,ax=axs[1,1],pad=0.01)
for ax in axs.flat:
    ax.label_outer()
plt.show()

On Fri, Mar 20, 2020 at 11:04 AM <matplotlib-users-request at python.org>
wrote:

> Date: Thu, 19 Mar 2020 12:42:31 -1000
> From: Eric Firing <efiring at hawaii.edu>
> To: matplotlib-users at python.org
> Subject: Re: [Matplotlib-users] trouble removing tick labels
> Message-ID: <77e44a65-c519-705e-24ad-dc13f7c204c3 at hawaii.edu>
> Content-Type: text/plain; charset=utf-8; format=flowed
>
> Jon,
>
> If you are removing the tick labels it suggests that the y-axes are all
> matched, in which case using
>
> fig, axs = plt.subplots(2, 2, sharey=True)
>
> should do what you want.
>
> If not, please supply a minimal but complete example illustrating the
> problem.
>
> Eric
>
> On 2020/03/19 11:36 AM, Slavin, Jonathan wrote:
> > Hi,
> >
> > I'm making a plot that's a grid of 4 x 4 axes. I'd like to remove the
> > tick labels for the y axes that are not in the first column. I tried
> > using what I saw in an example:
> > for ax in axs.flat:
> >  ? ? ax.label_outer()
> > but that seems to have no effect. I found where the code that defines
> > that function is and tried to use that code directly:
> > for ax in axs.flat:
> >  ? ? if ax.colNum != 0:
> >  ? ? ? ? for label in ax.get_yticklabels(which='both'):
> >  ? ? ? ? ? ? label.set_visible(False)
> >  ? ? ? ? ax.get_yaxis().get_offset_text().set_visible(False)
> >  ? ? ? ? ax.set_ylabel("")
> > but again no effect. Does anyone have any ideas why these are not
> > working? Any help would be appreciated.
> >
> > Regards,
> > Jon
>
-- 
Jonathan D. Slavin
Astrophysicist - High Energy Astrophysics Division
Center for Astrophysics | Harvard & Smithsonian
Office: (617) 496-7981 | Cell: (781) 363-0035
60 Garden Street | MS 83 | Cambridge, MA 02138
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20200320/18e7c38e/attachment.html>


More information about the Matplotlib-users mailing list