473,811 Members | 2,485 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Matplotlib twinx with wx

1 New Member
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 7157
hattawayd
1 New Member
import pylab
ax2 = pylab.twinx()
Jul 19 '07 #2
bartonc
6,596 Recognized Expert Expert
import pylab
ax2 = pylab.twinx()
Thanks, hattawayd, for the very useful information.

I hope you stick around,
Barton
Jul 20 '07 #3
jlm699
314 Contributor
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 Contributor
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 Contributor
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 New Member
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
2631
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 plot types: line plots, bar charts, log plots, images, pseudocolor plots, legends, date plots, finance charts and more. What's new since matplotlib 0.50 This is the first wide release in 5 months and there has been a tremendous amount of...
2
11658
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. http://matplotlib.sourceforge.net/matplotlib.pylab.html#-contour appears to be a good reference if you already know how to use contour(), but I could glean zero clues from it on how to actually use contour(). For example, it doesn't explain what the actual formats/types of the parameters. It...
0
2603
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 so first, i installed python 2.4. that works great! if i type 'python' in the terminal, it loads python 2.4. after that, i loaded and installed the matplotlib package from this
1
2974
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) * ((2*math.pi*freq)**2)/10 dati.append(forza) ax1 = subplot(111) plot(dati)
4
3356
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 on the matplotlib mailing list, but I have a hard enough time browsing sourceforge's achives (frequent internal server errors). Here is some output:
3
4729
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 on other comps... i used py2exe... i wrote a setup.py whose contents are : from distutils.core import setup import py2exe import matplotlib setup(console=, options={ 'py2exe': { 'packages' : ,
4
9695
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 I attempt to run the exe created. In searching the web, it appears this is an issue when others tried to use py2exe as well. Unfortunately, the few hits I saw doesn't include enough details to inspire me as to what I should be doing in my
0
4336
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 for this purpose but every time I try to make this exe using a setup file I get these errors. Final.py is my python code. Traceback (most recent call last): File "Final.py", line 5, in <module> File "pylab.pyc", line 1, in <module> File...
2
5120
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: http://durand.zephyrhosting.net/tremcs/graph_all.png Would there be any way to fix this? I was thinking of rotating the text so that there was enough space for each one but the best solution would be to only display text with the right scale. IE, with a 7 day graph, each day would...
0
9728
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10648
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10389
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10402
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9205
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7670
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3018
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.