簡體   English   中英

使用 wxmplot 交互模塊放大 plot 后如何訪問數據?

[英]How can I access the data after zooming in a plot using wxmplot interactive module?

我正在創建一個腳本來使用 wxPython 和wxmPlot.interactive 模塊交互式地探索數據可視化。

當使用 wxPython FileDialog 按下打開按鈕后打開文件時,它會被讀入 pandas DataFrame。 按下 Plot 按鈕會導致數據繪制在交互式 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. 或者是否有直接的方法可以使用 plot() 方法返回的 PlotFrame object 訪問縮放后的 plot 的 x 和 y 限制? 到目前為止,我可以打開一個 csv 文件 plot 並放大感興趣的區域。 我需要了解如何在 plot 中獲取新的縮放坐標,以便將相應的數據保存到僅在縮放區域中包含該數據的文件中。 對此的任何幫助將不勝感激。

我的代碼如下:

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)

認為這就是你要找的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM