I am creating a script to interactively explore data visualizations using wxPython and wxmPlot.interactive module .
When a file is opened after the Open Button is pressed using a wxPython FileDialog, it is read into a pandas DataFrame. Pressing the Plot Button results in the data being plotted on an interactive plot. If I can access the x values for the new plot when clicking on the plot and dragging to select an area of interest, I will be able to find the matching y coordinates in the pandas DataFrame. Or is there a direct way to access x and y limits on the zoomed plot using the returned PlotFrame object from the plot() method? So far I can open a csv file, plot it and zoom in a region of interest. What I need is to understand how to get the coordinates of the new zoomed in plot in order to save the corresponding data to a file with just that data in the zoomed region. Any help with this would be greatly appreciated.
My code is as follows:
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()
You can get the limits for the "current view" with something like this:
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())
which might print something like:
(-21.0, 21.0, -1.2026941911431666, 1.2026941911431666)
I think that is what you are looking for.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.