matplotlib - overlaying plots.

norseman norseman at
Thu May 14 12:14:34 EDT 2009

Ant wrote:
> Hi All,
> I am trying to get matplotlib to overlay a couple of graphs, but am
> getting nowhere. I originally thought that the following may work:
>>>> x = [1,2,3,4,5]
>>>> y = [2,4,6,8,10]
>>>> y2 = [1,4,9,16,25]
>>>> plot(x, y)
>>>> plot(x, y2)
> Now this works as desired, however, the actual case I have is more
> like this:
>>>> x = [1,2,3,4,5]
>>>> y = [2,4,6,8,10]
>>>> y2 = [.0001, .0002, .0003, .0004, .0005]
> Now the graph is useless, since the results are plotted on the same
> axis. What I really want is two different sets of axes, each scaled
> appropriately, but overlayed.
> The data I actually have, is one set of axes plotting distance against
> elevation, and a second plotting distance against speed. The former
> has (y-coord) units in the range 0-2000 ft and the latter 0 - 0.01
> miles/second. I want them on the same graph, so points can be easily
> correlated, but overlayed so that each line has a different scale on
> the y-axis. The closest I can get is to have two subplots, one above
> the other.
> Thanks in advance,
> Ant.

Use scalers to 'sync' the axis (x or y or both on the page)
The 'scales' along the axis need to match or you get your problem.

You know - like trying to do 1:1 but one is meters and the other feet.
That case needs two different graphs (or CAD drawings) each with its own 
legend.  The scales are not the same.  ie... 100meters is not 100feet
By scalling to same units/distance on graph you get overlay ability.

my first thought on your 0-2000ft and miles/sec is to convert miles to 
feet and multiply time by enough to be integer values and see if you get 
a better visual

one graph means one set of units per axis
plot each and scale one to the other
   (in CAD it's a case of meters/3.280833333_ to reduce to feet units
   .3048 * meters if using International scaler
   meters/3.280833333333(infinitely repeating 3s) is US Standard.
   The difference cost us a mars rover.
   You don't seem to be metric vs US but you do have miss matched units.
   miles vs feet, time vs (feet)elevation
   or whatever the units distance/elevation may be.
target_size / 2nd_graph unit => scaler for that axis (mostly)
   since the units are not completely the same type you may need
   to experiment.
     same units would be  time time          hours days seconds
                          linear linear      meter feet mile
                          angular angular    mils degrees rads
     and so forth.



More information about the Python-list mailing list