Skip to content

savefig re-scales xticks and labels of some (but not all) subplots #20765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fluviotect opened this issue Jul 30, 2021 · 5 comments · Fixed by #20771
Closed

savefig re-scales xticks and labels of some (but not all) subplots #20765

fluviotect opened this issue Jul 30, 2021 · 5 comments · Fixed by #20771
Milestone

Comments

@fluviotect
Copy link

fluviotect commented Jul 30, 2021

The x-ticks (and respective labels) for some of my subplot axes are re-scaled in the output png files.

image

While I didn't find this exact problem posted elsewhere, perhaps it's related to:
#12564

I say that b/c, the above image was my standard export at 300 dpi while I get my expected result at 120 dpi (below):

image

Changing the figsize changes the expression of the problem, but doesn't make it go away (e.g. 300 dpi at 14 x 20):

image

I also consulted :
[(https://github.com//issues/6800)]
[(https://github.com//issues/6638)]

but they are different expressions (of possibly a similar underlying problem) than mine.

I find it particularly odd that the minor x-ticks seem to stay put while the major x-ticks (and respective labels) shift.

Minimally reproducible code (with min/maxima of x-values in my data series plotted as points):

fig = plt.figure(constrained_layout=True, figsize=(10, 14))

# create top/bottom subfigs
(subfig_t, subfig_b) = fig.subfigures(2, 1, hspace=0.01, height_ratios=[3, 7])

# put ax0 in top subfig
ax0 = subfig_t.subplots()

# create left/right subfigs nested in bottom subfig
(subfig_bl, subfig_br) = subfig_b.subfigures(1, 2, wspace=0.01, width_ratios=[7, 2])

# put ax1-ax3 in gridspec of bottom-left subfig
gs = subfig_bl.add_gridspec(nrows=1, ncols=14)

ax1 = subfig_bl.add_subplot(gs[0, :1])
ax1.scatter(x=[-56.46881504821776, 24.179891162109396], y=[1500, 3600])


ax2 = subfig_bl.add_subplot(gs[0, 1:3], sharey=ax1)
ax2.scatter(x=[-126.5357270050049, 94.68456736755368], y=[1500, 3600])

ax3 = subfig_bl.add_subplot(gs[0, 3:14], sharey=ax1)
ax3.scatter(x=[-638.323228561402, 878.5568315887451], y=[1500, 3600])

ax2.set_ylabel('')
ax2.set_yticklabels([])
ax3.set_ylabel('')
ax3.set_yticklabels([])

b1_xlab = subfig_bl.supxlabel("MyLabel", x=0.5, size=12, fontweight='bold')
b1_xlab._autopos = True

# put ax4 in bottom-right subfig
ax4 = subfig_br.subplots()
ax4.scatter(x=[-36.00010414731702, 46.8026446716616], y=[1500, 3600])

subfig_br.supxlabel(' My Other Label', size=12, fontweight='bold')

dpi_lst = [120, 300]

for d in dpi_lst:
    out_plot = out_path + "MyFile_{}.png".format(d)
    plt.savefig(out_plot, dpi=d, transparent=True)    

Any suggestions for a work-around?

Operating system
Windows 10 Pro

Matplotlib Version
3.4.2

Python version
3.9.5

Jupyter version
3.0.16

Installation
conda

Conda channel
conda-forge

@jklymak
Copy link
Member

jklymak commented Jul 30, 2021

Confirmed - it looks like axis.get_tick_space doesn't work correctly with subfigures. Thanks for letting us know!

@fluviotect
Copy link
Author

fluviotect commented Jul 30, 2021

Happy to help.

I just noticed that the x-axis ticklabels (major) of the lower-right subplot (ax4 in subfig_br) shift when I change the figsize, but keep dpi constant:

300 dpi with a 14.3 x 20 fig:
image

300 dpi with a 10 x 14 fig:
image

In this instance the major and minor ticks seem to remain in-phase with one another.

I was unable to reproduce this with the test data in my original post. Let me know if you'd like me to upload my full data series.

@jklymak
Copy link
Member

jklymak commented Jul 30, 2021

Thanks, but this can be replicated with something much simpler...

@fluviotect
Copy link
Author

In terms of getting my current document sent off, is my best (short-term) bet to bail on subfigs, jump back to gridspec and manually locate pseudo-subxlabels via annotate?

@jklymak
Copy link
Member

jklymak commented Jul 30, 2021

I'd set the xticks manually for now (or patch matplotlib). I do have a fix for this that I'll submit in a few minutes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants