用 pandas 数据框进行matplotlib图例选择不起作用 [英] matplotlib legend picking with pandas dataframe doesn't work

查看:79
本文介绍了用 pandas 数据框进行matplotlib图例选择不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框:

 >>>60.1 65.5 67.3 74.2 88.5 ...A1 0.45 0.12 0.66 0.76 0.22B4 0.22 0.24 0.12 0.56 0.34B7 0.12 0.47 0.93 0.65 0.21... 

我正在尝试创建折线图,并能够启用/禁用某些折线(例如显示或隐藏图例中的某些项).我找到了

我得到了情节,但无法单击图例项目并显示或隐藏它们.

我的最终目标:能够使用matplotlib中的on_pick函数以交互方式显示或隐藏图例中的线条.

我了解我对文档的这一部分有疑问:

 行= [第1行,第2行]lined = {}#将图例行映射到原始行.适用于legline,zip中的origline(leg.get_lines(),行):legline.set_picker(True)#在图例行上启用拾取.lined [legline] = origline 

如我所见,这里的行是一对一"的.在我的脚本中,我使用pandas和T来获取每一行.不知道该如何处理.

解决方案

首先,您需要提取图中的所有line2D对象.您可以使用 ax.get_lines()来获取它们.这里是示例:

 将numpy导入为np导入matplotlib.pyplot作为plt将熊猫作为pd导入ts = pd.Series(np.random.randn(1000),index = pd.date_range("1/1/2000",周期= 1000))ts = ts.cumsum()df = pd.DataFrame(np.random.randn(1000,4),index = ts.index,column = list("ABCD"))df = df.cumsum()无花果,ax = plt.subplots()df.plot(ax = ax)行数= ax.get_lines()leg = ax.legend(fancybox = True,shadow = True)lined = {}#将图例行映射到原始行.适用于legline,zip中的origline(leg.get_lines(),行):legline.set_picker(True)#在图例行上启用拾取.lined [legline] = origlinedef on_pick(事件):#在选择事件中,找到与图例对应的原始行#proxy行,并切换其可见性.延长线= event.artistorigline =内衬[legline]可见=不是origline.get_visible()origline.set_visible(可见)#更改图例中行的alpha,以便我们可以看到哪些行#已切换.legline.set_alpha(1.0,如果可见,则为0.2)fig.canvas.draw()fig.canvas.mpl_connect('pick_event',on_pick)plt.show() 

I have the following dataframe:

>>>  60.1   65.5    67.3    74.2    88.5 ... 
A1   0.45   0.12    0.66    0.76    0.22
B4   0.22   0.24    0.12    0.56    0.34
B7   0.12   0.47    0.93    0.65    0.21
...

i'm trying to create line plot and to be able to enable/ disable some lines (like to display or hide certain items from the legend). I have found this . here the example is with numpy and not with pandas dataframe. When I try to apply it on my pandas df I manage to create plot but is not interactive:

%matplotlib notebook
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()
test.T.plot()
fig.canvas.mpl_connect('pick_event', on_pick)
plt.show()

I get plot but can't click on the legend items and display or hide them.

My end goal: to be able to display or hide lines interactively from legend using the on_pick function from matplotlib.

edit: I understand that I have problem with this part of the documentation:


lines = [line1, line2]
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

as I see that here the lines are taken "one by one" ut in my script I use pandas and T in order to get each line. not sure how to deall with this.

解决方案

Firstly, you need to extract all line2D objects on the figure. You can get them by using ax.get_lines(). Here the example:

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

ts = pd.Series(np.random.randn(1000), index=pd.date_range("1/1/2000", periods=1000))
ts = ts.cumsum()
df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list("ABCD"))
df = df.cumsum()
fig, ax = plt.subplots()
df.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()

这篇关于用 pandas 数据框进行matplotlib图例选择不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆