问题描述
我正在创建一个脚本来使用 wxPython 和 wxmPlot.interactive module 交互式探索数据可视化。
在使用 wxPython FileDialog 按下 Open 按钮后打开文件时,它会被读入 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,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,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()
解决方法
您可以通过以下方式获取“当前视图”的限制:
import numpy as np
import wxmplot.interactive as wi
x = np.linspace(-20,20,601)
y = np.sin(x/4.5) + x/50
display = wi.plot(x,y,label='test')
# now do some zooming,panning,etc
print(display.panel.get_viewlimits())
可能会打印如下内容:
(-21.0,21.0,-1.2026941911431666,1.2026941911431666)
我认为这就是你要找的。p>