【问题标题】:Ploting "real size" figures in matplotlib (scrollbars instead of zooming)在 matplotlib 中绘制“真实大小”的图形(滚动条而不是缩放)
【发布时间】:2012-02-13 09:57:02
【问题描述】:

我在 matplotlib 中绘图时遇到问题。我需要绘制一个代表无线电资源及时分配的宽图。但是当时间段很大时,情节会缩小,我需要放大它以查看特定片段的内容。 我想要的是绘制数据而不缩放(“真实大小”)并使用滚动条水平滚动图(及时)。这可能吗?

【问题讨论】:

  • 您可以生成任意大小的绘图图像,然后使用您喜欢的程序查看它。我认为任何 matplotlib 后端都不支持滚动条——当然,您可以放大并使用手形工具滚动...
  • 好的,所以我假设我需要使用: fig.set_size_inches(XX,YY) fig.savefig('filename') 这似乎有效,但有什么方法可以设置将图形尺寸保存为 1:1 尺寸?因为很难确定下一个情节应该使用哪种尺寸,而且我每次都需要 chenge 脚本(或传递额外的 arg)?
  • 1:1 尺寸是什么意思?你用什么单位来指定你的情节?这些单位与您屏幕显示的像素有何关系?

标签: python matplotlib


【解决方案1】:

这是 scipy 食谱中的一个示例:

当在嵌入的 matplotlib 画布中绘制一个很长的序列时 一个 wxPython 应用程序,有时能够显示一个 序列的一部分,而不诉诸可滚动窗口,因此 两个轴仍然可见。这样做的方法如下:

from numpy import arange, sin, pi, float, size

import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
from matplotlib.figure import Figure

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self,parent, id, 'scrollable plot',
                style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER,
                size=(800, 400))
        self.panel = wx.Panel(self, -1)

        self.fig = Figure((5, 4), 75)
        self.canvas = FigureCanvasWxAgg(self.panel, -1, self.fig)
        self.scroll_range = 400
        self.canvas.SetScrollbar(wx.HORIZONTAL, 0, 5,
                                 self.scroll_range)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.canvas, -1, wx.EXPAND)

        self.panel.SetSizer(sizer)
        self.panel.Fit()

        self.init_data()
        self.init_plot()

        self.canvas.Bind(wx.EVT_SCROLLWIN, self.OnScrollEvt)

    def init_data(self):

        # Generate some data to plot:
        self.dt = 0.01
        self.t = arange(0,5,self.dt)
        self.x = sin(2*pi*self.t)

        # Extents of data sequence: 
        self.i_min = 0
        self.i_max = len(self.t)

        # Size of plot window:       
        self.i_window = 100

        # Indices of data interval to be plotted:
        self.i_start = 0
        self.i_end = self.i_start + self.i_window

    def init_plot(self):
        self.axes = self.fig.add_subplot(111)
        self.plot_data = \
                  self.axes.plot(self.t[self.i_start:self.i_end],
                                 self.x[self.i_start:self.i_end])[0]

    def draw_plot(self):

        # Update data in plot:
        self.plot_data.set_xdata(self.t[self.i_start:self.i_end])
        self.plot_data.set_ydata(self.x[self.i_start:self.i_end])

        # Adjust plot limits:
        self.axes.set_xlim((min(self.t[self.i_start:self.i_end]),
                           max(self.t[self.i_start:self.i_end])))
        self.axes.set_ylim((min(self.x[self.i_start:self.i_end]),
                            max(self.x[self.i_start:self.i_end])))

        # Redraw:                  
        self.canvas.draw()

    def OnScrollEvt(self, event):

     # Update the indices of the plot:
        self.i_start = self.i_min + event.GetPosition()
        self.i_end = self.i_min + self.i_window + event.GetPosition()
        self.draw_plot()

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(parent=None,id=-1)
        self.frame.Show()
        self.SetTopWindow(self.frame)
        return True

if __name__ == '__main__':
    app = MyApp()
    app.MainLoop()
  • 示例需要wxPythonnumpymatplotlibpip install numpy matplotlib wxPython 安装它们。

来源:https://scipy-cookbook.readthedocs.io/items/Matplotlib_ScrollingPlot.html

【讨论】:

  • 你能提供比链接更详细的信息吗?
  • 我同意,你的答案是正确的......我想我正在寻找它的 如何 的快速摘要,以防 scipy 网站关闭。但看看代码,似乎说的不仅仅是“使用 wxPython 应用程序”,还需要逐字逐句地解释代码的作用。为正确答案 +1,我只是分享我对答案的印象。
  • 好的,你说得对,值得说几句话而不仅仅是一个链接。解释代码的作用确实不值得,但一句话序言就值得了。
  • 嗨!对不起我久违了。但现在我回来了;)这个链接正是我一直在寻找的,但仍然不能满足我的所有需求......但这是以后的话题。我试图将此代码与我的绘图代码结合起来 - 请参阅编辑后的问题。
  • 链接失效。他们似乎已经删除了滚动情节的例子。我搜索了食谱,但什么也没找到。你知道这个问题的任何其他来源吗?请帮忙。
猜你喜欢
  • 2023-03-06
  • 2012-07-18
  • 2021-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多