Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

python - How to implement autoscale for multiple axis with legend picking in matplotlib

Following the questions in Hiding lines after showing a pyplot figure and How to autoscaled graphs with picking legend (matplotlib)? I'm currently trying to figure out how I can add an autoscale option to multiple axes.

I changed the matplotlib example about legend picking and added an second axis:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure(figsize=(8, 6))
gs = gridspec.GridSpec(2, 1, figure=fig)

x = np.arange(10)
ax1 = fig.add_subplot(gs[0, 0])
line1, = ax1.plot(x, 1 * x, label='y=1x')
line2, = ax1.plot(x, 5 * x, label='y=5x')
leg1 = ax1.legend(loc='upper left', fancybox=True, shadow=True)
leg1.get_frame().set_alpha(0.4)


ax2 = fig.add_subplot(gs[1, 0])
line3, = ax2.plot(x, 5 * x ** 2, label='y=5x^2')
line4, = ax2.plot(x, 1 * x ** 2, label='y=1x^2')
leg2 = ax2.legend(loc='upper left', fancybox=True, shadow=True)
leg2.get_frame().set_alpha(0.4)

# we will set up a dict mapping legend line to orig line, and enable
# picking on the legend line
lines = [line1, line2]
lined = dict()
for legline, origline in zip(leg1.get_lines(), lines):
    legline.set_picker(5)  # 5 pts tolerance
    lined[legline] = origline

def onpick(event):
    # on the pick event, find the orig line corresponding to the
    # legend proxy line, and toggle the visibility
    legline = event.artist
    origline = lined[legline]
    vis = not origline.get_visible()
    origline.set_visible(vis)
    # Change the alpha on the line in the legend so we can see what lines
    # have been toggled
    if vis:
        legline.set_alpha(1.0)
    else:
        legline.set_alpha(0.2)
    ax1.relim(visible_only=True)
    ax1.autoscale_view()
    fig.canvas.draw()

fig.canvas.mpl_connect('pick_event', onpick)

plt.show()

But now I'm stuck on how to adapt the "on_pick" function to implement .relim(visible_only=True) and .autoscale_view() to work for several axis.

Furthermore I don't really understand how I need to change the following part of the code so that I actually get my legend picking working for multiple axes.

lines = [line1, line2]
lined = dict()
for legline, origline in zip(leg1.get_lines(), lines):
    legline.set_picker(5)  # 5 pts tolerance
    lined[legline] = origline

An alternative solution would be to implement the autoscale function for the code provided by Kany in Hiding lines after showing a pyplot figure. Not sure how to get that done though.

Any help?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...