Below is the working code to plot two different data series with different units on the same graph, with the same x co-ordinates:
generate some data
x = range(0, 10)
y1 = [i*i for i in x]
y2 = [pylab.sin(0.4*i) for i in x]
the data share x axis but have different y units
figure = pylab.gcf() # Get the current figure
orig_axis = pylab.gca() # Get the current axis
orig_axis.set_axis_off() # Turn it off to avoid complications
use this for the overlapping axes
box = [0.14, 0.14, 0.72, 0.72]
This uses the first set of data
axis1 = figure.add_axes(box, label = ‘axis1’)
axis1.plot(x, y1, ‘-^y’)
axis1.set_ylabel(‘AXIS 1 LABEL’)
axis1.set_xlabel(‘SHARED X LABEL’)
This uses the second set of data
Note the same box region is used but the label must be different
axis2 = figure.add_axes(box, label = ‘axis2’)
axis2.plot(x, y2, ‘-sb’)
axis2.set_ylabel(‘AXIS 2 LABEL’)
Write out to a file
pylab.savefig(‘out.png’, dpi = 100, transparent = True)
I can’t use show() in this case because there is no Transparent parameter.
It’s still a botch.
I tried using alpha but it didn’t seem to work at all?
Does anyone have a better implementation of this or better ideas?