使用 wxmplot 交互模块放大绘图后如何访问数据?
How can I access the data after zooming in a plot using wxmplot interactive module?
我正在创建一个脚本,以使用 wxPython 和 wxmPlot.interactive module 交互式探索数据可视化。
当使用 wxPython FileDialog 按下打开按钮后打开文件时,它被读入 pandas DataFrame。按下绘图按钮会导致在交互式绘图上绘制数据。如果我可以在单击绘图并拖动到 select 感兴趣的区域时访问新绘图的 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()
您可以通过以下方式获取“当前视图”的限制:
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)
我认为这就是你要找的。
我正在创建一个脚本,以使用 wxPython 和 wxmPlot.interactive module 交互式探索数据可视化。
当使用 wxPython FileDialog 按下打开按钮后打开文件时,它被读入 pandas DataFrame。按下绘图按钮会导致在交互式绘图上绘制数据。如果我可以在单击绘图并拖动到 select 感兴趣的区域时访问新绘图的 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()
您可以通过以下方式获取“当前视图”的限制:
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)
我认为这就是你要找的。