473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 7141
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

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

Similar topics

3
by: John Hunter | last post by:
matplotlib is a 2D plotting library for python. You can use matplotlib interactively from a python shell or IDE, or embed it in GUI applications (WX, GTK, and Tkinter). matplotlib supports many...
2
by: Grant Edwards | last post by:
I downloaded examples/contour_demo.py, and it doesn't run. I've searched both the user guide and the Wiki for "contour" and got zero hits. ...
0
by: spross | last post by:
hi all i have to use matplotlib on mac os x. on the official site of matplotlib, i found a link to precompiled python packages for mac os x: http://pythonmac.org/packages/py24-fat/index.html ...
1
by: luca72 | last post by:
Hello at all. I need to do a real time plot where on the frame i have this like limit line: import math dati = for freq in range(int(freqiniziale), (int(freqfinale )+ 1)): forza = float(massa)...
4
by: Bill Jackson | last post by:
Hi, I'm having some trouble plotting with the following matplotlibrc: text.usetex : True I tried clearing the cache files under ~/.matplotlib, but this did not help the problem. I'd post...
3
by: vajratkarviraj | last post by:
i hav python2.5, matplotlib0.90.1, and py2exe for python 2.5 all on windows xp... i hav a python program(letsc.py) which uses the matplotlib package... and i want 2 make an exe of it for distribution...
4
by: John Henry | last post by:
Has anybody been able to create an exe of their python applications involving matplotlib using pyinstall (ver 1.3)? I am getting a: RuntimeError: Could not find the matplotlib data files when...
0
by: PamMish1982 | last post by:
Hi all, I have recently started using Python and I am trying to make a GUI out of Tkinter. I am using matplotlib for the graphic purposes. I have to make a exe file from this code. I use py2exe...
2
by: Durand | last post by:
I got a really annoying problem with datetime graphs. The problem is that with a long range time graph, the text on the x axis keeps overlapping like here:...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.