Re: [Matplotlib-users] trouble removing tick labels

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()

···

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

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@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@python.org> wrote:
Date: Thu, 19 Mar 2020 12:42:31 -1000
From: Eric Firing <efiring@hawaii.edu>
To: matplotlib-users@python.org
Subject: Re: [Matplotlib-users] trouble removing tick labels
Message-ID: <77e44a65-c519-705e-24ad-dc13f7c204c3@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@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

I think you want "sharey='row'".

Eric

···

On 2020/03/20 8:56 AM, Slavin, Jonathan wrote:

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.

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

Hi Eric et al.,

So I have confirmed that the thing that prevents label_outer from working the way I would expect it to is that after adding a colorbar to the subplot axes, the attributes colNum and rowNum are set to 0. Would you consider this a bug? I think I would. I’m thinking that it’s because adding the colorbar creates a new set of axes into which the colorbar is placed. A minimal example that illustrates the issue is:

fig,axs = plt.subplots(1,2)

img0 = np.random.random((5,5))

img1 = np.random.random((5,5))

cax0 = axs[0].imshow(img0)

fig.colorbar(cax0,ax=axs[0])

cax1 = axs[1].imshow(img1)

print(f’axs[1].column = {axs[1].colNum}’) # value is 1

fig.colorbar(cax1,ax=axs[1])

print(f’axs[1].column = {axs[1].colNum}’) # values is 0

plt.show()

Jon

···

On Fri, Mar 20, 2020 at 2:56 PM Slavin, Jonathan jslavin@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@python.org wrote:

Date: Thu, 19 Mar 2020 12:42:31 -1000

From: Eric Firing efiring@hawaii.edu

To: matplotlib-users@python.org

Subject: Re: [Matplotlib-users] trouble removing tick labels

Message-ID: 77e44a65-c519-705e-24ad-dc13f7c204c3@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


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