Skip to content

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

Closed

Description

@breedlun

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

Activity

  1. added this to the milestone on Apr 17, 2015
  2. tacaswell commented on Apr 17, 2015

    @tacaswell
    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.

  3. breedlun commented on Apr 17, 2015

    @breedlun
    ContributorAuthor

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

    Anyways, thanks for the temporary work around.

  4. modified the milestones: , on Jul 11, 2015
  5. yongzhez commented on Feb 29, 2016

    @yongzhez
    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?

  6. yongzhez commented on Feb 29, 2016

    @yongzhez
    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

  7. tacaswell commented on Feb 29, 2016

    @tacaswell
    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.

  8. added 2 commits that reference this issue on Mar 4, 2016
  9. added a commit that references this issue on May 22, 2016
  10. modified the milestones: , on Oct 3, 2017
  11. efiring commented on Jan 26, 2018

    @efiring
    Member

    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

  12. breedlun commented on Jan 26, 2018

    @breedlun
    ContributorAuthor

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

  13. modified the milestones: , v2.2.0 on Feb 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions