Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Second y-axis does not scale correctly when using zoom tool #9616

Closed as not planned
@DietBru

Description

@DietBru

Hello,
when making a plot with two differently scaled y-axes by following this example, the second scale becomes incorrect after using the zoom tool (see example below). Interestingly, reading the ylim values (see example below) yields in the expected results. Using ax.set_ylim() and using the paning tool do not show this behavior.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.set(title="Two y-scales", ylabel="x / m")
# right ax:
axr = ax.twinx()
axr.set_ylabel("x / cm")


def track_yax(ax_):
    """Syncronize second y-axis with first axis. """
    y1, y2 = ax.get_ylim()
    axr.set_ylim(100*y1, y2*100)
    # axr.figure.canvas.draw() # does not make a difference
    yr1, yr2 = axr.get_ylim()
    print(f"Lower: {y1:+.5g} -> {yr1:+.5g}\t Upper: {y2:+.5g} -> {yr2:+.5g}")


# automatically update ylim of ax2 when ylim of ax1 changes.
ax.callbacks.connect("ylim_changed", track_yax)


# Do the plotting:
ax.plot([0, 5, 2, 4, 1], 'o-')
ax.grid(True)

plt.show()

figure1

Matplotlib version

  • Operating system: Debian 4.13.4-2 (Sid) x86_64 GNU/Linux
  • Matplotlib version: 2.1.0.post484.dev0+g883096231 (git master)
  • Matplotlib backend: Qt5Agg
  • Python version: Python 3.6.3 on Anaconda 5.0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions