【发布时间】:2021-08-25 01:22:40
【问题描述】:
我正在创建一个脚本来使用 wxPython 和 wxmPlot.interactive module 交互式探索数据可视化。
当使用 wxPython FileDialog 按下打开按钮后打开文件时,它会被读入 pandas DataFrame。按下绘图按钮会导致数据被绘制在交互式绘图上。如果在单击绘图并拖动以选择感兴趣区域时可以访问新绘图的 x 值,我将能够在 pandas DataFrame 中找到匹配的 y 坐标。或者是否有使用 plot() 方法返回的 PlotFrame 对象直接访问缩放图上的 x 和 y 限制的方法?到目前为止,我可以打开一个 csv 文件,绘制它并放大感兴趣的区域。我需要了解如何获取新的放大图的坐标,以便将相应的数据保存到仅在放大区域中包含该数据的文件中。对此的任何帮助将不胜感激。
我的代码如下:
import wx
import pandas as pd
import wxmplot.interactive as wi
import os
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title="Data Exploration Tool ")
panel = wx.Panel(self)
self.df = None
self.title = ''
my_sizer = wx.BoxSizer(wx.HORIZONTAL)
open_btn = wx.Button(panel, label='Open')
open_btn.Bind(wx.EVT_BUTTON, self.OnOpen)
my_sizer.Add(open_btn, 0, wx.ALL | wx.CENTER, 5)
plot_btn = wx.Button(panel, label='Plot')
plot_btn.Bind(wx.EVT_BUTTON, self.OnPlot)
my_sizer.Add(plot_btn, 0, wx.ALL | wx.CENTER, 5)
panel.SetSizer(my_sizer)
self.Show()
def OnPlot(self, event):
x = pd.to_datetime(self.df.iloc[:, 0], format='%m/%d/%y %H:%M %p' )
for i in range(1, len(self.df.columns)):
wi.plot(x, self.df.iloc[:, i], show_legend=True,
title=self.title, wintitle=self.title)
def OnOpen(self, event):
# ask the user what new file to open
defDir=''
defFile=''
fileDialog = wx.FileDialog(self, "Open CSV file",
defDir, defFile,
wildcard="(*.csv;*.xlsx)|*.csv;*.xlsx",
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
if fileDialog.ShowModal() == wx.ID_CANCEL:
return # the user changed their mind
# Proceed loading the file chosen by the user
pathname = fileDialog.GetPath()
print(pathname)
self.df = pd.read_csv(pathname, skiprows=12, parse_dates=True)
self.title = os.path.basename(pathname)
if __name__ == '__main__':
app = wx.App()
frame = MyFrame()
app.MainLoop()
【问题讨论】: