On Thursday 29 April 2010 15:02:34 James Jack wrote:
> Below is the working code to plot two different data series with different
> units on the same graph, with the same x co-ordinates:
>
>
>
> import pylab
> # 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.set_title('TITLE')
> axis1.plot(x, y1, '-^y')
> axis1.set_ylabel('AXIS 1 LABEL')
> axis1.set_xlabel('SHARED X LABEL')
> axis1.spines['right'].set_visible(False)
> # 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.yaxis.set_ticks_position('right')
> axis2.yaxis.set_label_position('right')
> axis2.set_ylabel('AXIS 2 LABEL')
> axis2.spines['bottom'].set_visible(False)
> axis2.spines['top'].set_visible(False)
> axis2.spines['left'].set_visible(False)
> # Write out to a file
> pylab.savefig('out.png', dpi = 100, transparent = True)
>
>
> Issues:
>
> 1) I can't use show() in this case because there is no Transparent
> parameter.
> 2) It's still a botch.
> 3) I tried using alpha but it didn't seem to work at all?
>
>
> Does anyone have a better implementation of this or better ideas?
>
> Many thanks
> James
Hi James,
I'm not sure I completely understand your problem, but for me it seems like
the twinx-method of an Axes instance is what you need (see for instance:
http://matplotlib.sourceforge.net/examples/api/two_scales.html)
for your example something like the following should work
import pylab
# 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
box = [0.14, 0.14, 0.72, 0.72]
ax1 = figure.add_axes(box)
ax1.set_ylabel('axis1', color='b')
ax2 = ax1.twinx()
ax2.set_ylabel('axis2', color='g', )
ax1.plot(x, y1, '-^y', color='blue')
ax2.plot(x, y2, '-^y', color='green')
# Make the y-tick labels of first axes match the line color.
for tl in ax1.get_yticklabels():
tl.set_color('b')
pylab.show()
Kind regards,
Matthias
------------------------------------------------------------------------------
_______________________________________________
Matplotlib-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-users