简体   繁体   中英

matplotlib(mpl_connect) in for loop to create many interactive plots does not work

I have a for loop that generated different dataframe (pandas) and then plot it. I want to create many interactive plots so I can display and hide different lines in my graph. For that I'm using the on_pick function (as mentioned here )

The problem is that when I plot one table, it works and I have interactive legend, but when I try to plot serveral charts in for loop, non of the legends are interactive anymore.

df = pd.DataFrame(np.array([[0.45,0.12,0.66,0.76,0.22],[0.22,0.24,0.12,0.56,0.34],[0.12,0.47,0.93,0.65,0.21]]),
                    columns=[60.1,65.5,67.3,74.2,88.5])
df['name']=['A1','B4','B7']
df=df.set_index('name')

#plot alone:
fig, ax = plt.subplots()
df.T.plot(ax=ax)
lines = ax.get_lines()
leg = ax.legend(fancybox=True, shadow=True)
lined = {}  # Will map legend lines to original lines.
for legline, origline in zip(leg.get_lines(), lines):
    legline.set_picker(True)  # Enable picking on the legend line.
    lined[legline] = origline

def on_pick(event):
    #On the pick event, find the original line corresponding to the legend
    #proxy line, and toggle its visibility.
    legline = event.artist
    origline = lined[legline]
    visible = not origline.get_visible()
    origline.set_visible(visible)
    #Change the alpha on the line in the legend so we can see what lines
    #have been toggled.
    legline.set_alpha(1.0 if visible else 0.2)
    fig.canvas.draw()

fig.canvas.mpl_connect('pick_event', on_pick)
plt.show()

the result: plot that I can enable and disable the lines in the legend: 在此处输入图像描述

#plot many plots in for loop:
nums=[5,8,0.3]

for n in nums:
    db=df*n
    
    fig, ax = plt.subplots()
    db.T.plot(ax=ax)
    lines = ax.get_lines()
    leg = ax.legend(fancybox=True, shadow=True)
    lined = {}  # Will map legend lines to original lines.
    for legline, origline in zip(leg.get_lines(), lines):
        legline.set_picker(True)  # Enable picking on the legend line.
        lined[legline] = origline
        
    def on_pick(event):
        #On the pick event, find the original line corresponding to the legend
        #proxy line, and toggle its visibility.
        legline = event.artist
        origline = lined[legline]
        visible = not origline.get_visible()
        origline.set_visible(visible)
        #Change the alpha on the line in the legend so we can see what lines
        #have been toggled.
        legline.set_alpha(1.0 if visible else 0.2)
        fig.canvas.draw()
        
    fig.canvas.mpl_connect('pick_event', on_pick)
    plt.show()
    

result: I get the plots but can't play with which lines will be displayed. 在此处输入图像描述

*when I touch the lines it still shows interactively the x and y values but the legend is not interactive.

My end goal: to generate more than one interactive plot in for loop in matplotlib, with the ability to enable and disable the legend items.

Here's how to show them simultaneously, using fig as a carrier for lined

Multiple, Separate Plots

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.DataFrame(np.array([[0.45,0.12,0.66,0.76,0.22],[0.22,0.24,0.12,0.56,0.34],[0.12,0.47,0.93,0.65,0.21]]),
                    columns=[60.1,65.5,67.3,74.2,88.5])
df['name']=['A1','B4','B7']
df=df.set_index('name')

#plot many plots in for loop:
nums=[5,8,0.3]


def on_pick(event):
    #On the pick event, find the original line corresponding to the legend
    #proxy line, and toggle its visibility.
    legline = event.artist
    origline = event.canvas.figure.lined[legline]
    visible = not origline.get_visible()
    origline.set_visible(visible)
    #Change the alpha on the line in the legend so we can see what lines
    #have been toggled.
    legline.set_alpha(1.0 if visible else 0.2)
    event.canvas.draw()

for n in nums:
    db=df*n
    
    fig, ax = plt.subplots()
    db.T.plot(ax=ax)
    lines = ax.get_lines()
    leg = ax.legend(fancybox=True, shadow=True)
    fig.lined = {}  # Will map legend lines to original lines.
    for legline, origline in zip(leg.get_lines(), lines):
        legline.set_picker(True)  # Enable picking on the legend line.
        fig.lined[legline] = origline
        
        
    fig.canvas.mpl_connect('pick_event', on_pick)

plt.show()

Using Subplots

在此处输入图像描述

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.DataFrame(np.array([[0.45,0.12,0.66,0.76,0.22],[0.22,0.24,0.12,0.56,0.34],[0.12,0.47,0.93,0.65,0.21]]),
                    columns=[60.1,65.5,67.3,74.2,88.5])
df['name']=['A1','B4','B7']
df=df.set_index('name')

#plot many plots in for loop:
nums=[5,8,0.3]


def on_pick(event):
    #On the pick event, find the original line corresponding to the legend
    #proxy line, and toggle its visibility.
    legline = event.artist
    origline = event.canvas.figure.lined[legline]
    visible = not origline.get_visible()
    origline.set_visible(visible)
    #Change the alpha on the line in the legend so we can see what lines
    #have been toggled.
    legline.set_alpha(1.0 if visible else 0.2)
    event.canvas.draw()

nrows = int(np.ceil(np.sqrt(len(nums))))
ncols = int(np.ceil(len(nums) / nrows))
fig, axs = plt.subplots(nrows=nrows,ncols=ncols)
if not isinstance(axs,np.ndarray):
  axs = np.array([[axs]])
if len(axs.shape)==1:
  axs = np.expand_dims(axs,axis=1)

fig.lined = {}  # Will map legend lines to original lines.
for idx,n in enumerate(nums):
    db=df*n
    
    ax = axs[int(idx/ncols),idx % ncols]
    
    db.T.plot(ax=ax)
    lines = ax.get_lines()
    leg = ax.legend(fancybox=True, shadow=True)
    for legline, origline in zip(leg.get_lines(), lines):
        legline.set_picker(True)  # Enable picking on the legend line.
        fig.lined[legline] = origline
        
        
fig.canvas.mpl_connect('pick_event', on_pick)
plt.show()

you can use PyQT5 for multiple dynamic real time graphs https://eli.thegreenplace.net/2009/05/23/more-pyqt-plotting-demos

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM