[Matplotlib-users] NavigationToolbar2Wx with mplot3d
Hi, I have two questions about using NavigationToolbar2Wx with mplot3d. 1/ Initially the 3D scatter plot will rotate as usual with a mouse, but after selecting the 'pan' or 'zoom' buttons the plot responds with some confusion. How can I restore it to rotation only, i.e. disconnect the zoom or pan behaviour? 2/ When using the 'save' button I get different behaviours depending on the backend. With 'WXAgg' the saved png image shows only the axes, not the scatter points. The scatter points and axes do appear correctly in a pdf file. Using the 'WX' backend gives both scatter points and axes for the png file. My full application has a mix of 2D and 3D plots (separate notebooks) and it would be preferable for users if all plots could usedthe common toolbar. I see this behaviour running matplotlib 1.0.1 with Python 2.6.6 and wxPython 2.8.11.0 under Windows XP with the example below. #--- # adapted from example code "embedding_in_wx2.py" import numpy as np import matplotlib # uncomment the following to use wx rather than wxagg #matplotlib.use('WX') #from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas # comment out the following to use wx rather than wxagg matplotlib.use('WXAgg') from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas from matplotlib.backends.backend_wx import NavigationToolbar2Wx from matplotlib.figure import Figure from mpl_toolkits.mplot3d import Axes3D import wx class CanvasFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self,None,-1, 'CanvasFrame',size=(550,350)) self.SetBackgroundColour(wx.NamedColor("WHITE")) self.figure = Figure() self.canvas = FigureCanvas(self, -1, self.figure) self.axes = Axes3D(self.figure) xs = np.random.rand(100) ys = np.random.rand(100) zs = np.random.rand(100) self.axes.scatter(xs, ys, zs) self.sizer = wx.BoxSizer(wx.VERTICAL) self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW) self.SetSizer(self.sizer) self.Fit() self.add_toolbar() # comment this out for no toolbar def add_toolbar(self): self.toolbar = NavigationToolbar2Wx(self.canvas) self.toolbar.Realize() if wx.Platform == '__WXMAC__': # Mac platform (OSX 10.3, MacPython) does not seem to cope with # having a toolbar in a sizer. This work-around gets the buttons # back, but at the expense of having the toolbar at the top self.SetToolBar(self.toolbar) else: # On Windows platform, default window size is incorrect, so set # toolbar width to figure width. tw, th = self.toolbar.GetSizeTuple() fw, fh = self.canvas.GetSizeTuple() # By adding toolbar in sizer, we are able to put it at the bottom # of the frame - so appearance is closer to GTK version. # As noted above, doesn't work for Mac. self.toolbar.SetSize(wx.Size(fw, th)) self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND) # update the axes menu on the toolbar self.toolbar.update() def OnPaint(self, event): self.canvas.draw() class App(wx.App): def OnInit(self): 'Create the main window and insert the custom frame' frame = CanvasFrame() frame.Show(True) return True app = App(0) app.MainLoop() # Regards, Keith This electronic transmission and any documents accompanying this electronic transmission contain confidential information belonging to the sender. This information may be legally privileged. The information is intended only for the use of the individual or entity named above. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or the taking of any action in reliance on or regarding the contents of this electronically transmitted information is strictly prohibited. -- All the data continuously generated in your IT infrastructure contains a definitive record of customers, application performance, security threats, fraudulent activity and more. Splunk takes this data and makes sense of it. Business sense. IT sense. Common sense. http://p.sf.net/sfu/splunk-d2dcopy1___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] NavigationToolbar2Wx with mplot3d
Matplotlib 1.1 fixes the NavigationToolbar2Wx issue with saving the .png image. Thanks for the excellent support. Regards. Keith From: ben.v.r...@gmail.com [mailto:ben.v.r...@gmail.com] On Behalf Of Benjamin Root Sent: Saturday, 24 September 2011 2:59 a.m. To: Keith Jones Cc: matplotlib-users@lists.sourceforge.net Subject: Re: [Matplotlib-users] NavigationToolbar2Wx with mplot3d On Wednesday, September 21, 2011, Benjamin Root mailto:ben.r...@ou.edu>> wrote: > > > On Wednesday, September 21, 2011, Keith Jones > mailto:k.jo...@irl.cri.nz>> wrote: >> Hi, >> >> I have two questions about using NavigationToolbar2Wx with mplot3d. >> >> >> >> 1/ Initially the 3D scatter plot will rotate as usual with a mouse, but >> after selecting the 'pan' or 'zoom' buttons the plot responds with some >> confusion. How can I restore it to rotation only, i.e. disconnect the zoom >> or pan behaviour? >> > > That is a bug that should be resolved in the upcoming release. Use the right > mouse button for zooming instead. In the upcoming release, the zoom and pan > button should have no effect on axes3d objects, if I remember correctly. > > >> >> >> 2/ When using the 'save' button I get different behaviours depending on the >> backend. With 'WXAgg' the saved png image shows only the axes, not the >> scatter points. The scatter points and axes do appear correctly in a pdf >> file. Using the 'WX' backend gives both scatter points and axes for the png >> file. >> > > I dont use WxAgg regularly. I will use your code to test this. > > Ben Root I could not reproduce your problem with WxAgg backend on the development branch. I could only test on Linux, though. However, if there is a difference between platforms with WxAgg backend, then it is likely a Wx bug and not a mpl bug (although the fact that the Wx backend worked is odd). We are putting out an RC of v1.1.0 later today. Could you try out that version within the next few days and let me know if it still happens for you? Ben Root This electronic transmission and any documents accompanying this electronic transmission contain confidential information belonging to the sender. This information may be legally privileged. The information is intended only for the use of the individual or entity named above. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or the taking of any action in reliance on or regarding the contents of this electronically transmitted information is strictly prohibited. -- All the data continuously generated in your IT infrastructure contains a definitive record of customers, application performance, security threats, fraudulent activity and more. Splunk takes this data and makes sense of it. Business sense. IT sense. Common sense. http://p.sf.net/sfu/splunk-d2d-oct___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] wxpython matplotlib figure resize
Hi Josh, Yes I too am using the wx backend. Regards, Keith From: Tony Yu [mailto:tsy...@gmail.com] Sent: Friday, 13 July 2012 3:16 p.m. To: Keith Jones Cc: Joshua Koehler; matplotlib-users@lists.sourceforge.net Subject: Re: [Matplotlib-users] wxpython matplotlib figure resize On Thu, Jul 12, 2012 at 9:27 PM, Keith Jones mailto:k.jo...@irl.cri.nz>> wrote: Hi, I am interested in a better solution to this problem, but I have used this simple method to force the right size. def tickle(self): #gets frame to redraw and resize, not elegant. if self.IsMaximized(): #then needs a kick self.Restore() self.Maximize() else: x,y = self.GetSize() self.SetSize((x-1, y-1)) self.SetSize((x, y)) Regards, Keith From: Joshua Koehler [mailto:jjkoehl...@gmail.com<mailto:jjkoehl...@gmail.com>] Sent: Saturday, 7 July 2012 1:51 a.m. To: matplotlib-users@lists.sourceforge.net<mailto:matplotlib-users@lists.sourceforge.net> Subject: [Matplotlib-users] wxpython matplotlib figure resize Hi all, I am currently trying to use matplotlib with wxPython and all is going well except for one annoying issue that I can't figure out. I initialize a wxcanvas object with a figure and then throughout the life of the program I want the canvas' figure to change and display the corresponding plot. I can get the change of figure, but when the program goes to plot, the figure isn't the right size. It changes to the right size only when I manually resize the figure (see attached images). Is there some command that I am missing? This is the update sequence I am using: self.figure = figure self.canvas.figure.clear() self.canvas.figure = self.figure self.canvas.draw() self.color_background() #self.GetParent().Layout() #self.SetSizer(self.main_sizer) #self.Fit() self.SendSizeEvent() As you can tell from the comments (there are more in my code), I have tried a variety of ways to update the figure off the bat. Thanks! Josh This may be similar to a Qt-backend bug, which didn't take the toolbar into account when resizing the figure: https://github.com/matplotlib/matplotlib/pull/756 It doesn't quite fit, though, because Josh's original example shows a figure that's the correct size (but the axes doesn't fill the figure). The toolbar issue tended to squish the axes in the vertical direction (at least in the GUI window), whereas Josh's example is squished in the horizontal direction. Keith: Are you also using the Wx-backend? I don't have Wx installed so I can't provide much help. I've been planning to take a look at a similar issue in the Tk backend, but haven't had time. Best, -Tony This electronic transmission and any documents accompanying this electronic transmission contain confidential information belonging to the sender. This information may be legally privileged. The information is intended only for the use of the individual or entity named above. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or the taking of any action in reliance on or regarding the contents of this electronically transmitted information is strictly prohibited. -- Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] wxpython matplotlib figure resize
Hi Josh, I am not quite sure what information you need, but I have an application with several wx notebooks, each with pages carrying related but different 2D and 3D plots. I hope this overview covers what you want. In the code below I set up the axes and toolbar in each panel ready to accept plots as required. I use wxFormbuilder to create MyFrame1 and then this code to add graphs and other functionality. The 'tickle' function is called when a notebook is selected for display. import wx from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg from matplotlib.figure import Figure from mpl_toolkits.mplot3d import Axes3D class ProjectFrame(bare_gui.MyFrame1): def __init__( self, parent): bare_gui.MyFrame1.__init__( self, parent) ... self.Create2DGraph(self.CT_graph_1, "Current / %")#CT notebook self.Create2DGraph(self.VT_graph_1, "Voltage/ %")#VT notebook ... self.Show(True) def Create2DGraph(self, panel, xlabel): panel.figure = Figure(None) panel.canvas = FigureCanvas(panel, -1, panel.figure) panel.axes1 = panel.figure.add_subplot(2,1,1) panel.axes2 = panel.figure.add_subplot(2,1,2) panel.axes1.set_xlabel(xlabel) panel.axes1.set_ylabel('Error / %') panel.axes2.set_xlabel(xlabel) panel.axes2.set_ylabel('Phase / crad') panel.sizer = wx.BoxSizer(wx.VERTICAL) panel.sizer.Add(panel.canvas, 1, wx.LEFT | wx.TOP | wx.GROW) panel.SetSizer(panel.sizer) panel.Fit() self.add_2Dtoolbar(panel) def add_2Dtoolbar(self, panel): panel.toolbar = NavigationToolbar2WxAgg(panel.canvas) panel.toolbar.Realize() tw, th = panel.toolbar.GetSizeTuple() fw, fh = panel.canvas.GetSizeTuple() panel.toolbar.SetSize(wx.Size(fw, th)) panel.sizer.Add(panel.toolbar, 0, wx.LEFT | wx.EXPAND) # update the axes menu on the toolbar panel.toolbar.update() def Create3DGraph(self, panel): panel.figure = Figure(None) panel.canvas = FigureCanvas(panel, -1, panel.figure) panel.ax = Axes3D(panel.figure) #these labels should be selected later for specific components panel.ax.set_xlabel('Current / %') panel.ax.set_ylabel('Phase / degree') panel.ax.set_zlabel('Error / %') panel.sizer = wx.BoxSizer(wx.VERTICAL) panel.sizer.Add(panel.canvas, 1, wx.LEFT | wx.TOP | wx.GROW) panel.SetSizer(panel.sizer) panel.Fit() self.add_2Dtoolbar(panel) From: Joshua Koehler [jjkoehl...@gmail.com] Sent: Friday, 13 July 2012 10:45 p.m. To: matplotlib-users@lists.sourceforge.net Subject: Re: [Matplotlib-users] wxpython matplotlib figure resize Hi Keith, How are you storing the plot in the frame/panel? Josh On Jul 12, 2012, at 11:59 PM, Keith Jones wrote: Hi Josh, Yes I too am using the wx backend. Regards, Keith From: Tony Yu [mailto:tsy...@gmail.com] Sent: Friday, 13 July 2012 3:16 p.m. To: Keith Jones Cc: Joshua Koehler; matplotlib-users@lists.sourceforge.net<mailto:matplotlib-users@lists.sourceforge.net> Subject: Re: [Matplotlib-users] wxpython matplotlib figure resize On Thu, Jul 12, 2012 at 9:27 PM, Keith Jones mailto:k.jo...@irl.cri.nz>> wrote: Hi, I am interested in a better solution to this problem, but I have used this simple method to force the right size. def tickle(self): #gets frame to redraw and resize, not elegant. if self.IsMaximized(): #then needs a kick self.Restore() self.Maximize() else: x,y = self.GetSize() self.SetSize((x-1, y-1)) self.SetSize((x, y)) Regards, Keith From: Joshua Koehler [mailto:jjkoehl...@gmail.com<mailto:jjkoehl...@gmail.com>] Sent: Saturday, 7 July 2012 1:51 a.m. To: matplotlib-users@lists.sourceforge.net<mailto:matplotlib-users@lists.sourceforge.net> Subject: [Matplotlib-users] wxpython matplotlib figure resize Hi all, I am currently trying to use matplotlib with wxPython and all is going well except for one annoying issue that I can't figure out. I initialize a wxcanvas object with a figure and then throughout the life of the program I want the canvas' figure to change and display the corresponding plot. I can get the change of figure, but when the program goes to plot, the figure isn't the right size. It changes to the right size only when I manually resize the figure (see attached images). Is there some command that I am missing? This is the update sequence I am using: self.figure = figure self.canvas.figure.clear() self.canvas.figure = self.figure self.canvas.draw()
Re: [Matplotlib-users] wxpython matplotlib figure resize
Thanks Josh this is much appreciated. I will definitely have a look at this approach. Just for clarification, my code does only create the figures when the frame is initialised. The plots are then updated/cleared as required. Regards, Keith From: Joshua Koehler [mailto:jjkoehl...@gmail.com] Sent: Tuesday, 17 July 2012 12:12 a.m. To: matplotlib-users@lists.sourceforge.net Subject: Re: [Matplotlib-users] wxpython matplotlib figure resize Yes, my apologies for being a bit vague. Hopefully the following can help you out, but I can't make promises. It did fix the issue for me. First off, from reading other threads about handling figures, it might be better to use matplotlib.pylab.figure instead of matplotlib.figure. Apparently the former registers the figure with a manager which allows for better cleanup. Second, it looks to me from your code as if you are creating a figure every time a new plot is created. Is this correct? Or are these functions just called on startup? If you are creating a new figure each time for a plot, (which is what I was originally doing) you should redesign so you keep the same figure and then just update the subplots. Clear the figure and then add the appropriate subplots. Here is a segment from my code: def plot(figure, rows, cols, graph, title): figure.clear() plt.figure(figure.number) pos_counter = 1 for g in graph: g.axes = figure.add_subplot(rows, cols, pos_counter) if title: g.axes.set_title(g.plot_title) plot_nx(g, plot_options=g.plot_options) pos_counter = pos_counter + 1 figure.canvas.draw() plt is the pylab module from matplotlib and plot_nx is an internal call to networkx plotting capabiility (same as if you called the pyplot.plot). If you do use pylab, make sure to include the plt.figure(figure.number) call as this seems to be a bug (or perhaps this just is with networkx integration). This setup works for me rather well. I no longer have to manually resize anything. I hope that helps. If not, I am at a loss for what would be a good way to proceed. I, probably like you, spent a long time trying to figure this one out and found this to be the best solution. It might be good to go with the clunky mechanism since it works and wait until this is addressed later (if it is indeed a bug) Regards, Josh On Jul 14, 2012, at 4:13 AM, Keith Jones wrote: Hi Josh, I am not quite sure what information you need, but I have an application with several wx notebooks, each with pages carrying related but different 2D and 3D plots. I hope this overview covers what you want. In the code below I set up the axes and toolbar in each panel ready to accept plots as required. I use wxFormbuilder to create MyFrame1 and then this code to add graphs and other functionality. The 'tickle' function is called when a notebook is selected for display. import wx from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg from matplotlib.figure import Figure from mpl_toolkits.mplot3d import Axes3D class ProjectFrame(bare_gui.MyFrame1): def __init__( self, parent): bare_gui.MyFrame1.__init__( self, parent) ... self.Create2DGraph(self.CT_graph_1, "Current / %")#CT notebook self.Create2DGraph(self.VT_graph_1, "Voltage/ %")#VT notebook ... self.Show(True) def Create2DGraph(self, panel, xlabel): panel.figure = Figure(None) panel.canvas = FigureCanvas(panel, -1, panel.figure) panel.axes1 = panel.figure.add_subplot(2,1,1) panel.axes2 = panel.figure.add_subplot(2,1,2) panel.axes1.set_xlabel(xlabel) panel.axes1.set_ylabel('Error / %') panel.axes2.set_xlabel(xlabel) panel.axes2.set_ylabel('Phase / crad') panel.sizer = wx.BoxSizer(wx.VERTICAL) panel.sizer.Add(panel.canvas, 1, wx.LEFT | wx.TOP | wx.GROW) panel.SetSizer(panel.sizer) panel.Fit() self.add_2Dtoolbar(panel) def add_2Dtoolbar(self, panel): panel.toolbar = NavigationToolbar2WxAgg(panel.canvas) panel.toolbar.Realize() tw, th = panel.toolbar.GetSizeTuple() fw, fh = panel.canvas.GetSizeTuple() panel.toolbar.SetSize(wx.Size(fw, th)) panel.sizer.Add(panel.toolbar, 0, wx.LEFT | wx.EXPAND) # update the axes menu on the toolbar panel.toolbar.update() def Create3DGraph(self, panel): panel.figure = Figure(None) panel.canvas = FigureCanvas(panel, -1, panel.figure) panel.ax = Axes3D(panel.figure) #these labels should be selected later for specific components panel.ax.set_xlabel('Current / %') panel.ax.set_ylabel('Phase / degree') panel.ax.set_zlabel('Error / %') panel.sizer = wx.BoxS