For循环中用于创建许多交互式绘图的matplotlib(MPL_CONNECT)不起作用

原学程将引见For轮回顶用于创立很多接互式画图的matplotlib(MPL_CONNECT)没有起感化的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

成绩描写

我有1个for轮回,它死成分歧的数据帧( pandas ),而后画制它。
我想创立很多接互式画图,如许我便不妨在我的图表中显示以及隐蔽分歧的线。
为此,我应用了On_Pick函数(如前所述here)

成绩是,当我画制1个表时,它不妨任务,而且我有接互式图例,但是当我测验考试在for轮回中画制多个图表时,出有图例是接互式的。

df = pd.DataFrame(np.array([[0.四五,0.一二,0.六六,0.七六,0.二二],[0.二二,0.二四,0.一二,0.五六,0.三四],[0.一二,0.四七,0.九三,0.六五,0.二一]]),
  columns=[六0.一,六五.五,六七.三,七四.二,88.五])
df['name']=['A一','B四','B七']
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(一.0 if visible else 0.二)
 fig.canvas.draw()

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

成果:画制我不妨开用以及禁用图例中的线条:

#plot many plots in for loop:
nums=[五,8,0.三]

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(一.0 if visible else 0.二)
  fig.canvas.draw()
 fig.canvas.mpl_connect('pick_event', on_pick)
 plt.show()
 

成果:我获得了直线图,但是不克不及处置将显示哪些线。

*当我触摸线条时,它依然接互天显示x以及y值,但是图例没有是接互的。

我的终究目的:在matplotlib中的for轮回中死成多个接互式画图,并可以或许开用以及禁用图例项。

推举谜底

怎样同时显示它们,应用fig作为lined的载体

多个自力的天块

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

df = pd.DataFrame(np.array([[0.四五,0.一二,0.六六,0.七六,0.二二],[0.二二,0.二四,0.一二,0.五六,0.三四],[0.一二,0.四七,0.九三,0.六五,0.二一]]),
  columns=[六0.一,六五.五,六七.三,七四.二,88.五])
df['name']=['A一','B四','B七']
df=df.set_index('name')

#plot many plots in for loop:
nums=[五,8,0.三]


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(一.0 if visible else 0.二)
 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()

应用子图

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

df = pd.DataFrame(np.array([[0.四五,0.一二,0.六六,0.七六,0.二二],[0.二二,0.二四,0.一二,0.五六,0.三四],[0.一二,0.四七,0.九三,0.六五,0.二一]]),
  columns=[六0.一,六五.五,六七.三,七四.二,88.五])
df['name']=['A一','B四','B七']
df=df.set_index('name')

#plot many plots in for loop:
nums=[五,8,0.三]


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(一.0 if visible else 0.二)
 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)==一:
  axs = np.expand_dims(axs,axis=一)

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()

佳了闭于For轮回顶用于创立很多接互式画图的matplotlib(MPL_CONNECT)没有起感化的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。

0
没有账号?注册  忘记密码?