每次选择切片时,我都在尝试根据绘图中可见的特征将数据(具有多列的 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())