## matplotlib-users

 [Matplotlib-users] Multiple y axis plotting From: Etrade Griffiths - 2006-11-06 09:24:08 Attachments: Message as HTML ```Hi new to Matplotlib and struggling to make a plot that has three lines plotted on it: two are supposed to plot on the LH y axis and the third on the RH y axis. The code so far is import os import math import wx import wxmpl import numpy # ======================= # Define plot data window # ======================= class ForecastData: # Constructor def __init__(self, d): # Transfer parameters self.dt = d['dt'] # time step size self.tmax = d['tmax'] # maximum forecast time # Set up arrays (filled with zeroes) self.nstep=int(self.tmax/self.dt) self.xtim=numpy.zeros((self.nstep,)) self.xoil=numpy.zeros((self.nstep,)) self.xwat=numpy.zeros((self.nstep,)) self.xgas=numpy.zeros((self.nstep,)) # ================== # Define main window # ================== class PlotFrame(wxmpl.PlotFrame): # Constructor for main window def __init__(self, data): # Create a wxmpl PlotFrame instance wxmpl.PlotFrame.__init__(self, None, wx.ID_ANY, title='Production profile') fig = self.get_figure() # Create an Axes on the Figure to plot in. ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) ax2 = fig.add_axes([0.9, 0.1, -0.8, 0.8]) # Plot the data ax1.plot(data.xtim, data.xoil, '-g', label='oil rate') ax1.plot(data.xtim, data.xwat, '-b', label='water rate') ax2.plot(data.xtim, data.xgas, '-r', label='gas rate') # Add a legend ax1.legend() ax2.legend() # Set axis titles ax1.set_xlabel('time (years)', family='sans-serif') ax1.set_ylabel('liquid rate (m3/d)', family='sans-serif') ax2.set_ylabel('gas rate (m3/d)', family='sans-serif') # ======================== # Define application class # ======================== class App(wx.App): def OnInit(self): # Define data parameters for test model d={} d['dt']=0.1 # time step size d['tmax']=10.0 # maximum forecast time # Define data object fcst_data = ForecastData(d) # Fill data arrays with dummy data tnow = 0.0 for n in range(fcst_data.nstep): fcst_data.xtim[n]=tnow fcst_data.xoil[n]=5000*math.exp(-0.1*tnow) fcst_data.xgas[n]=600*math.sin(tnow) fcst_data.xwat[n]=3000*math.exp(1.0-0.2*tnow) tnow = tnow + fcst_data.dt # Display top level window self.frame = PlotFrame(fcst_data) self.frame.Show() self.SetTopWindow(self.frame) return True def main(): app = App(False) app.MainLoop() if __name__ == '__main__': main() About the only successful thing here is getting the RH y axis scale on the RH of the plot :-(. The lines on ax1 don't show up so I guess ax2 is obscuring it somehow (though I thought axisbg=None by default), the x axis for axis 2 runs in the wrong direction (presumably because width is negative) and the labels are all over the place. Hopefully, the labels problem should be fairly easy to sort out once I can see the lines but until then I'm a bit stuck. Tried to find an example of what I want to do, but most of them seem to use subplots whereas I just want all my plots on the same graph. Can somebody please point me in the right direction? Thanks in advance Alun Griffiths ```
 Re: [Matplotlib-users] Multiple y axis plotting From: John Hunter - 2006-11-06 14:27:09 ```>>>>> "Etrade" == Etrade Griffiths writes: Etrade> Hi new to Matplotlib and struggling to make a plot that Etrade> has three lines plotted on it: two are supposed to plot on Etrade> the LH y axis and the third on the RH y axis. The code so Etrade> far is ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) ax2 = fig.add_axes([0.9, 0.1, -0.8, 0.8]) ^ [left, bottom, width, height] negative numbers are not supported. I don't know what kind of layout you want, but start with something like ax1 = fig.add_axes([0.1, 0.1, 0.4, 0.8]) ax2 = fig.add_axes([0.55, 0.1, 0.4, 0.8]) and once you have a working plot tweak from there. JDH ```
 Re: [Matplotlib-users] Multiple y axis plotting From: Etrade Griffiths - 2006-11-06 14:59:05 Attachments: example_plot.pdf ```John am trying to make a plot something along the lines of the attached. This has a single plot area with separate y-axes on either side of the plot, rather than two plot areas, each with their own axis Best regards Alun Griffiths At 14:24 06/11/2006, John Hunter wrote: > >>>>> "Etrade" == Etrade Griffiths writes: > > Etrade> Hi new to Matplotlib and struggling to make a plot that > Etrade> has three lines plotted on it: two are supposed to plot on > Etrade> the LH y axis and the third on the RH y axis. The code so > Etrade> far is > > > ax1 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) > ax2 = fig.add_axes([0.9, 0.1, -0.8, 0.8]) > ^ > [left, bottom, width, height] > >negative numbers are not supported. I don't know what kind of layout >you want, but start with something like > > > ax1 = fig.add_axes([0.1, 0.1, 0.4, 0.8]) > ax2 = fig.add_axes([0.55, 0.1, 0.4, 0.8]) > >and once you have a working plot tweak from there. > >JDH ```
 Re: [Matplotlib-users] Multiple y axis plotting From: Etrade Griffiths - 2006-11-07 14:30:03 ```After poking around in the Pylab source, managed to sort the multiple line plotting using fig = self.get_figure() ax1 = fig.gca() ax2 = fig.add_axes(ax1.get_position(), sharex=ax1, frameon=False) so issue closed for the moment ```
 Re: [Matplotlib-users] Multiple y axis plotting From: John Hunter - 2006-11-07 15:34:27 ```>>>>> "Etrade" == Etrade Griffiths writes: Etrade> After poking around in the Pylab source, managed to sort Etrade> the multiple line plotting using Etrade> fig = self.get_figure() ax1 = fig.gca() ax2 = Etrade> fig.add_axes(ax1.get_position(), sharex=ax1, Etrade> frameon=False) Isn't this what the twinx function does - heave you seen http://matplotlib.sourceforge.net/examples/shared_axis_demo.py ? ```
 Re: [Matplotlib-users] Multiple y axis plotting From: Etrade Griffiths - 2006-11-07 16:01:27 ```John thanks - twinx was indeed where I "borrowed" some of the lines from. They're in the code explicitly 'cos it's a lot clearer to me than using twinx which is buried somewhere in the pylab interface Alun Griffiths At 15:32 07/11/2006, you wrote: > >>>>> "Etrade" == Etrade Griffiths writes: > > Etrade> After poking around in the Pylab source, managed to sort > Etrade> the multiple line plotting using > > Etrade> fig = self.get_figure() ax1 = fig.gca() ax2 = > Etrade> fig.add_axes(ax1.get_position(), sharex=ax1, > Etrade> frameon=False) > >Isn't this what the twinx function does - heave you seen >http://matplotlib.sourceforge.net/examples/shared_axis_demo.py ? ```