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]=5000math.exp(-0.1tnow)
fcst_data.xgas[n]=600*math.sin(tnow)
fcst_data.xwat[n]=3000math.exp(1.0-0.2tnow)
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