【问题标题】:How can I access the data after zooming in a plot using wxmplot interactive module?使用 wxmplot 交互式模块放大绘图后如何访问数据?
【发布时间】: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()

  

【问题讨论】:

    标签: python pandas wxpython


    【解决方案1】:

    您可以通过以下方式获取“当前视图”的限制:

    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>

    【讨论】:

    • 感谢您的回答,它完美运行!我计划在主应用程序面板上添加一个按钮,用于将缩放区域中的数据保存到 CSV 文件。我可以在按钮单击事件上添加一个包含 display.panel.get_viewlimits() 调用的函数,以提供 x 和 y 限制,从 pandas 数据框中获取该数据并将其写入 csv。已经为此工作了一段时间,我非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    相关资源
    最近更新 更多