471,348 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Matplotlib twinx with wx

1
I am building a wxPython application that plots some data using matplotlib. I am embedding several axis within a figure by creating several repeatedly calling

ax1 =fig.add_axes([0.1, 0.7, 0.8, 0.2], **axprops)

where axprops is a dict(). This way the plots can share an X-axis. However, I wanted to have one plot have two sets of information. I am familiar with the two_scales.py example, but the same calls do not seem to work in the wx app. Only the original plot succeeds and formats the left axis, while nothing is displayed on the right axis. Is twinx() not supported in wx?

-pJmp
Jul 3 '07 #1
6 6883
import pylab
ax2 = pylab.twinx()
Jul 19 '07 #2
bartonc
6,596 Expert 4TB
import pylab
ax2 = pylab.twinx()
Thanks, hattawayd, for the very useful information.

I hope you stick around,
Barton
Jul 20 '07 #3
jlm699
314 100+
I am trying to accomplish the same thing but with no luck... here is my code:

Expand|Select|Wrap|Line Numbers
  1. class CanvasFrame(wx.Panel):
  2.  
  3.     def __init__(self, parent):
  4.         wx.Panel.__init__(self, parent, -1)
  5.  
  6.         self.SetBackgroundColour(wx.NamedColor("WHITE"))
  7.  
  8.         self.figure = Figure()
  9.         self.ax1 = self.figure.add_subplot(111)
  10.         t = arange(0.0,3.0,0.01)
  11.         s = sin(2*pi*t)
  12.  
  13.         self.ax1.plot(t,s)
  14.         self.ax1.set_xlabel('t')
  15.         self.ax1.set_ylabel('sin(t)')
  16.  
  17.         self.ax2 = pylab.twinx()
  18.         s2 = pylab.exp(t)
  19.  
  20.         self.ax2.plot(t, s2, 'r.')
  21.         self.ax2.set_ylabel('exp(t)')
  22.         self.ax2.yaxis.tick_right()
  23.  
  24.         self.figure_canvas = FigureCanvas(self, -1, self.figure)
  25.  
  26.         # Note that event is a MplEvent
  27.         self.figure_canvas.mpl_connect('motion_notify_event', self.UpdateStatusBar)
  28.         self.figure_canvas.Bind(wx.EVT_ENTER_WINDOW, self.ChangeCursor)
  29.  
  30.         self.sizer = wx.BoxSizer(wx.VERTICAL)
  31.         self.sizer.Add(self.figure_canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
  32.         self.SetSizer(self.sizer)
  33.         self.Fit()
  34.  
  35.         self.toolbar = NavigationToolbar2Wx(self.figure_canvas)
  36.         self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
  37.         self.toolbar.Show()
This is displayed in a wx.Frame however it only displays the first axis (the sin wave one), and nothing for the second. I am assuming that my problem is that I am not associating the second axis with the figure, however with the horrifyingly lackluster matplotlib API/documentation I am at a complete loss of how to do this.
Aug 6 '07 #4
jlm699
314 100+
I cannot figure this out and it's just killing me!
I tried using pylab's show() method as in the two_scales example and a window pops up with both y-axes displayed correctly, but then I close that window and my actual application appears with only a single y-axis. Here's my codes.

code: (python)
Expand|Select|Wrap|Line Numbers
  1. class CanvasFrame(wx.Panel):
  2.     def __init__(self, parent):
  3.         wx.Panel.__init__(self, parent, -1)
  4.  
  5.         self.SetBackgroundColour(wx.NamedColor("WHITE"))
  6.  
  7.         self.time_range = 100.0
  8.         self.time_div = 0.5
  9.         self.curr_time = 0.0
  10.         self.y = [0.0]
  11.         self.x = [0.0]
  12.  
  13.         self.tv = track_vars(self)
  14.  
  15.         self.sizer = wx.BoxSizer(wx.VERTICAL)
  16.         self.sizer.Add(self.tv.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
  17.         self.SetSizer(self.sizer)
  18.         self.Fit()
  19.  
  20.         self.toolbar = NavigationToolbar2Wx(self.tv.canvas)
  21.         self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)
  22.         self.toolbar.Show()
  23.  
  24. class MainWindow(wx.Frame):
  25.     def __init__(self, *args, **kwargs):
  26.         wx.Frame.__init__(self, *args, **kwargs)
  27.  
  28.         icon = wx.Icon(name = 'rDT.ico', type = wx.BITMAP_TYPE_ICO)
  29.         self.Bind(wx.EVT_CLOSE, self.OnExit)
  30.         self.SetIcon(icon)
  31.         self.ds = None
  32.  
  33.         # Set-up a Status Bar divided into two fields
  34.         self.sb = self.CreateStatusBar(2)
  35.  
  36.         # Set up splitter window
  37.         self.splitwin = SplitterWindow(self)
  38.  
  39.         # Set up the texual dataviewer for the left pane
  40.         self.lc = CheckListCtrl(self.splitwin)
  41.         self.lc.InsertColumn(0, "Variable Name")
  42.         self.lc.InsertColumn(1, "Value")
  43.         self.currentItem = 0
  44.         self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.lc)
  45.  
  46.         # Set up the visual dataviewer for the right pane
  47.         self.graph = CanvasFrame(self.splitwin)
  48.  
  49.         # Now we build our menus
  50.         menu = self.MakeMenu()
  51.         self.SetMenuBar(menu)
  52.  
  53.         # Now initialize the split window for program start-up appearance
  54.         self.splitwin.SetMinimumPaneSize(275)
  55.         self.splitwin.SplitVertically(self.lc, self.graph, 275)
  56.  
  57.         # Setup our Heartbeat and Collection timers
  58.         Global.heartbeat_timer_id = wx.NewId()
  59.         Global.collection_timer_id = wx.NewId()
  60.         Global.heartbeat_timer = wx.Timer(self, Global.heartbeat_timer_id)
  61.         Global.collection_timer = wx.Timer(self, Global.collection_timer_id)
  62.         wx.EVT_TIMER(self, Global.heartbeat_timer_id, self.OnHeartbeat)
  63.         wx.EVT_TIMER(self, Global.collection_timer_id, self.OnCollection)
  64.  
  65.         return None
  66.  
  67. class track_vars:
  68.     def __init__(self, parent):
  69.         self.figure = Figure()
  70.         self.ax = self.figure.add_subplot(111)
  71.         self.ax.set_xlabel('Time')
  72.         self.ax.set_ylabel('Testing data collection')
  73.         self.ax2 = pylab.twinx()
  74.         self.ax2.set_ylabel('Second y-axis')
  75.         self.ax2.yaxis.tick_right()
  76.         pylab.show()
  77.         self.canvas = FigureCanvas(parent, -1, self.figure)
Does anyone have ANY idea why this is not working embedded in a wxPython application?! Anyone? Anyone? Bueller?... Bueller?
Aug 13 '07 #5
jlm699
314 100+
Ok, well I figured it out finally... and to any of those poor souls who in the future are trying to accomplish anything similar I basically had to just do it manually (kind of a jerry-rig) and scrap twinx. Here's my code:
Expand|Select|Wrap|Line Numbers
  1. import matplotlib
  2. from matplotlib.figure import Figure
  3. from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
  4. ...
  5.         self.figure = Figure()
  6.         self.ax1 = self.figure.add_subplot(111)
  7.         self.ax1.set_ylabel('Value')
  8.         self.ax1.yaxis.tick_left() # w/o this the ticks were echoed on right side
  9.         self.ax2 = self.figure.add_subplot(111, sharex=self.ax1, frameon=False)
  10.         self.ax2.set_xlabel('Samples')
  11.         self.ax2.yaxis.tick_right()
  12.         self.canvas = FigureCanvas(parent, -1, self.figure)
  13.         self.canvas.realize()
  14.         self.canvas.draw()
  15.  
I know, it's very simple and should have been more obvious to me earlier, but oh well. The one thing that I did not bother tackling was figuring out how to place the ylabel on the right side of the graph for ax2, as using ax.set_ylabel placed it right below the label for ax1.

If anyone has any suggestions I'd appreciate them, but I'm not really concerned about putting a label on the right side as it's rather redundant anyway.
Aug 23 '07 #6
jougs
1
The one thing that I did not bother tackling was figuring out how to place the ylabel on the right side of the graph for ax2, as using ax.set_ylabel placed it right below the label for ax1.
Does anyone have a solution to the problem of the position of the second ylabel? My data is such that I really need two different labels, but all my attempts have failed so far.

Thanks!

Greetings,
Jougs!
Oct 19 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by John Hunter | last post: by
2 posts views Thread by Grant Edwards | last post: by
reply views Thread by spross | last post: by
1 post views Thread by luca72 | last post: by
4 posts views Thread by Bill Jackson | last post: by
4 posts views Thread by John Henry | last post: by
2 posts views Thread by Durand | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.