[Matplotlib-users] trouble removing tick labels

Scott Lasley selasley at icloud.com
Fri Mar 20 15:26:50 EDT 2020


You can use axis.tick_params to turn off the labels.  Replacing

for ax in axs.flat:
    ax.label_outer()

with

for ax in axs.flat[1::2]:
    ax.tick_params(labelleft=False)

will turn off the left tick labels on the plots in the second column

Hth,
Scott

> On Mar 20, 2020, at 2:56 PM, Slavin, Jonathan <jslavin at cfa.harvard.edu> wrote:
> 
> 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
> 
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users



More information about the Matplotlib-users mailing list