# matplotlib - overlaying plots.

Hyuga hyugaricdeau at gmail.com
Thu May 14 16:52:29 CEST 2009

```On May 14, 7:41 am, Ant <ant... at gmail.com> 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.
>
>
> Ant.

Just scale up the y-axis values of your second graph 200,000 times,
and specify in label that the y-axis for the second graph is velocity
scaled up 200000x for comparison purposes.  Nothing wrong with that--
it's done all the time.

On the other hand, I just took a peek at the matplotlib example
gallery, which is very diverse, and it has an example that I think is
exactly what you're looking for: http://matplotlib.sourceforge.net/examples/api/two_scales.html

```