Skip to content

Tick label padding on first y-axis changes when adding a second y-axis #4346

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
breedlun opened this issue Apr 17, 2015 · 7 comments
Closed

Comments

@breedlun
Copy link
Contributor

When I have just the right set of commands, the tick label padding on the first y-axis changes when I add a second y-axis. Here are my minimal working examples.

This set of commands

import matplotlib as mpl
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = plt.subplot()
ax1.plot(range(2),range(2))
ax1.yaxis.set_tick_params(which = 'major', direction = 'out', \
    labelsize = mpl.rcParams['font.size'], size = mpl.rcParams['ytick.major.size'])
fig.savefig('No_Issue1.png')

produces a figure that looks fine to me:
no_issue1

This set of commands,

fig = plt.figure()
ax1 = plt.subplot()
ax1.plot(range(2),range(2))
ax2 = ax1.twinx()
fig.savefig('No_Issue2.png')

also produces a figure that looks fine, even after adding a second y-axis:

no_issue2

This set of commands

fig = plt.figure()
ax1 = plt.subplot()
ax1.plot(range(2),range(2))
ax1.yaxis.set_tick_params(which = 'major', direction = 'out', \
    labelsize = mpl.rcParams['font.size'], size = mpl.rcParams['ytick.major.size'])
ax2 = ax1.twinx()
fig.savefig('Issue.png')

however, messes with the tick label padding on the first y-axis:

issue

Apparently, the following commands

ax1.yaxis.set_tick_params(which = 'major', direction = 'out', \
    labelsize = mpl.rcParams['font.size'], size = mpl.rcParams['ytick.major.size'])
ax2 = ax1.twinx()

do not play nice together.

In case it matters, I am on matplotlib 1.4.0

@tacaswell tacaswell added this to the next point release milestone Apr 17, 2015
@tacaswell
Copy link
Member

If you reverse the order

fig = plt.figure()
ax1 = plt.subplot()
ax1.plot(range(2), range(2))
ax1.set_ylabel('y label')
ax2 = ax1.twinx()
ax1.yaxis.set_tick_params(which='major', labelsize=12, direction='out')

It works correctly.

@stretch97 You seem to have a talent for finding really odd subtle bugs.

@breedlun
Copy link
Contributor Author

Talent?! I think of it as a curse. =)

Anyways, thanks for the temporary work around.

@tacaswell tacaswell modified the milestones: next point release, proposed next point release Jul 11, 2015
@yongzhez
Copy link
Contributor

When debugging this, I noticed that autoscale_view when plot is called causes the problem. I'm on matplotlib 2.x branch and I get the following problem below.
figure_1-1

This happens after the below code is called in autoscale_view:

 handle_single_axis(
            scalex, self._autoscaleXon, self._shared_x_axes,
            'intervalx', 'minposx', self.xaxis, self._xmargin,
            margins['left'], margins['right'], self.set_xbound)
        handle_single_axis(
            scaley, self._autoscaleYon, self._shared_y_axes,
            'intervaly', 'minposy', self.yaxis, self._ymargin,
            margins['bottom'], margins['top'], self.set_ybound)

Is this is a new bug that popped up? if so, what would be considered a decent fix for autoscale_view?

@yongzhez
Copy link
Contributor

I was wondering if auto_scale is actually even needed in plot. I managed to have the below graph just by removing it
figure_1

@tacaswell
Copy link
Member

I think this is only working with out autoscale_view because the data limits are [0, 1], which also happen to be the default view limits.

Autoscaling the limits (with the margins) is one of the big style changes for 2.0 so that functionality needs to remain.

It also looks like the last image you posted still shows the extra space between the tick labels and the ticks on the left axes.

tacaswell added a commit that referenced this issue Mar 4, 2016
tacaswell added a commit that referenced this issue Mar 4, 2016
tacaswell added a commit to tacaswell/matplotlib that referenced this issue May 22, 2016
@tacaswell tacaswell modified the milestones: 2.1 (next point release), 2.2 (next next feature release) Oct 3, 2017
@efiring
Copy link
Member

efiring commented Jan 26, 2018

I'm closing this because the original problem above seems to be gone now. Reopen if I have misunderstood. With

fig = plt.figure()
ax1 = plt.subplot()
ax1.plot(range(2),range(2))
ax1.yaxis.set_tick_params(which = 'major', direction = 'out', \
    labelsize = mpl.rcParams['font.size'], size = mpl.rcParams['ytick.major.size'])
ax2 = ax1.twinx()
fig.savefig('Issue.png')

I get
issue

@efiring efiring closed this as completed Jan 26, 2018
@breedlun
Copy link
Contributor Author

Yes, the problem does appear to be gone. Thank you to whomever inadvertently solved it!

@QuLogic QuLogic modified the milestones: needs sorting, v2.2.0 Feb 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants