【发布时间】:2016-01-27 07:22:44
【问题描述】:
我想知道在 wxPython FigureCanvasWxAgg 实例中嵌入 matplotlib 图形时如何在 python 中执行以下伪代码:
需要用到以下物品:
-
---- 可以使用的进口商品----
导入 wx
从 matplotlib.figure 导入图
从 matplotlib.backends.backend_wxagg 导入 FigureCanvasWxAgg 作为 FigureCanvas
---------------------- em>---------------------------------
main_canvas;
shadow_canvas;
big_plot [一个 matplotlib 图形实例,其中有一个大图——就像你用 figure.add_subplots(1,1,1) 制作的那样];
small_subplots [一个 matplotlib 图形实例,其中包含 2 个子图 -- 你可以使用 figure.add_subplots(2,1,i),其中 1
一个名为 SwapView(main_canvas,shadow_canvas,big_plot,small_subplots) 的函数,它实质上将当前位于 shadow_canvas 中的图形与 main_canvas 中的图形交换(因此请在具有大图的图形和具有许多图形的图形之间不断切换小地块)
-
一个函数UpdateDisplay(),每次调用SwapView()时动态更新显示
******* PSEUDOCODE ******* main_canvas.show() shadow_canvas.hide() main_canvas has big_plot initially shadow_canvas has small_subplots initially if big_plot in main_canvas: SwapView(...) ---> should put big_plot in shadow_canvas and small_subplots in the main_canvas else: SwapView(...) ---> should put the small_subplots in shadow_canvas and the big_plot in main_canvas UpdateDisplay() ******* END OF CODE *******
这是我对这段代码的初步尝试,不幸的是我找不到找到当前显示的图形的方法。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import numpy as np
import wx
import time
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
class myframe(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,parent = None, id = -1, title = 'LoadFigure()', size = (800,800))
self.figurePanel = FigurePanel(parent = self)
canvas1 = self.figurePanel.canvas
canvas2 = self.figurePanel.enlarged_canvas
fig1 = self.figurePanel.enlarged_figure
fig2 = self.figurePanel.figure
fig1.set_canvas(canvas1) #enlarged_fig resides in canvas1
fig2.set_canvas(canvas2) #fig resides in canvas2
#Show both canvases ---> canvas2 will override canvas1, but when canvas2 hides canvas1 should show
canvas2.Show()
canvas1.Show()
self.Show()
print "Starting to swap displays!"
time.sleep(1)
for i in range(10):
print "run: %d"%i
self.SwapView(big_plot = fig1,small_plots = fig2,main_canvas = canvas1,shadow_canvas = canvas2)
time.sleep(1)
def SwapView(self,big_plot,small_plots,main_canvas,shadow_canvas):
'''
Keep swapping the main_canvas with the shadow_canvas to show either fig1 or fig2.
Initially, big_plot has main_canvas and small_plots have shadow_canvas
'''
wx.Yield()
print list(main_canvas)
print list(big_plot.get_children())
time.sleep(2)
for child in big_plot.get_children():
if child == main_canvas:
print 'big_plot has main_canvas'
big_plot.set_canvas(shadow_canvas)
small_plots.set_canvas(main_canvas)
main_canvas.draw()
wx.Yield()
main_canvas.Show()
else:
print 'big_plot has shadow_canvas'
for child in small_plots.get_children():
if child == main_canvas:
print 'small_plots has main_canvas'
small_plots.set_canvas(shadow_canvas)
big_plot.set_canvas(main_canvas)
main_canvas.draw()
wx.Yield()
main_canvas.Show()
else:
print 'small_plots has shadow_canvas'
class FigurePanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.figPanel = self
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.figure = Figure(figsize = (8,6.1), dpi =60)
self.ax = self.figure.add_subplot(1,1,1)
self.ax.plot([1,2,3],[1,2,3])
self.enlarged_figure = Figure(figsize = (8,6.1), dpi = 60)
self.ax1 = self.enlarged_figure.add_subplot(2,1,1)
self.ax2 = self.enlarged_figure.add_subplot(2,1,2)
self.ax1.plot([1,2,3],[1,4,9])
self.ax2.plot([1,2,3],[1,4,9])
self.canvas = FigureCanvas(self, -1, self.figure)
self.enlarged_canvas = FigureCanvas(self,-1,self.enlarged_figure)
self.Layout()
self.Fit()
if __name__ == "__main__":
app = wx.App(False)
fr = myframe()
app.MainLoop()
【问题讨论】:
标签: python user-interface matplotlib wxpython