在 SpanSelector 对象的 onselect 函数中使用非全局变量

using non-global variables inside onselect function of SpanSelector object

每次我 select 切片时,我都试图根据图中可见的特征将数据(pandas.DataFrame 多列)切片并保存到单独的文件中。到目前为止,我使用了 matplotlib SpanSelector 及其 onselect 函数。但是,这只适用于全局变量,因为现在似乎有一种简单的方法可以将 DataFrame 传递到函数中。有什么办法可以避免每次都声明一个全局变量吗?

DataFrame 本身来自将输入文件读入 DataFrame 的程序。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.widgets import SpanSelector



def get_data():
    # example of DataFrame, real Data will come from input-files
    x = np.arange(100,step=0.2)
    y = np.sin(x)
    y2 = np.cos(x)
    data = pd.DataFrame(np.array((y,y2)).transpose(), index=x, columns=["a","b"])
    return data

def cut_data(data_frame):
    # use a single plot or as many subplots as there are columns in dataframe if more than one
    if data_frame.shape[1] == 1:
        fig, ax = plt.subplots(data_frame.shape[1], 1, sharex=True)
        ax.plot(data_frame)
        span = SpanSelector(ax, onselect, 'horizontal', useblit=True,
                        rectprops=dict(alpha=0.35, facecolor='red'), span_stays=True)
    else:
        fig, axlst = plt.subplots(data_frame.shape[1], 1, sharex=True)
        for n, col in enumerate(data_frame):
            axlst[n].plot(data_frame[col])
        span = SpanSelector(axlst[0], onselect, 'horizontal', useblit=True,
                        rectprops=dict(alpha=0.35, facecolor='red'), span_stays=True)
    plt.show()

def onselect(xmin, xmax):
    pass
    # get indices of x-values each time a subset of the data is selected
    # slice every column in DataFrame and save to file as new DataFrame


cut_data(get_data())

我写了一个可调用对象来规避这个问题

class OnselectObject(object):
'''
This classed is used to circumvent the limitations of the matplotlib SpanSelector object
'''
def __init__(self, data_frame):
    self.data = data_frame

def __call__(self, xmin, xmax):
    cut_data = self.data[:][xmin:xmax] # this is awesome
    save_cut_data(cut_data)