Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

## Re: [Matplotlib-users] two scales in the same plot

 Re: [Matplotlib-users] two scales in the same plot From: Ondrej Certik - 2009-04-30 05:15:20 ```On Fri, Apr 24, 2009 at 1:10 PM, Sandro Tosi wrote: > Hi Ondrej, > nice to see you here :) Nice to see you too! :) > > On Fri, Apr 24, 2009 at 22:02, Ondrej Certik wrote: >> Hi, >> >> is there a way to have one plot with two functions, one using some >> scale, the other one a different scale and show for example one scale >> on the left, the other scale on the right? > > sure, twinx() is what you're looking for; here is a simple example: > > import matplotlib.pyplot as plt > import numpy as np > x = np.arange(0., np.e, 0.01) > y1 = np.exp(-x) > y2 = np.log(x) > fig = plt.figure() > ax1 = fig.add_subplot(111) > ax1.plot(x, y1) > ax1.set_ylabel('Y values for exp(-x)') > ax2 = ax1.twinx() > ax2.plot(x, y2, 'r') > ax2.set_xlim([0,np.e]) > ax2.set_ylabel('Y values for ln(x)') > ax2.set_xlabel('Same X for both exp(-x) and ln(x)') > > The values on X has to be of the same scale, tough, else the graph > would look really weird. Thanks a lot for the code. That worked. I was meeting some deadline, so I forgot to reply that it worked. Thanks Ryan and Jouni as well! Ondrej ```

### Thread view

 [Matplotlib-users] two scales in the same plot From: Ondrej Certik - 2009-04-24 20:02:49 ```Hi, is there a way to have one plot with two functions, one using some scale, the other one a different scale and show for example one scale on the left, the other scale on the right? I want to plot an atomic potential (one scale) and the corresponding wave functions (different scale) in the same plot. I tried to look through all examples and search this list, but didn't find anything. Thanks a lot, Ondrej ```
 Re: [Matplotlib-users] two scales in the same plot From: Sandro Tosi - 2009-04-24 20:10:56 ```Hi Ondrej, nice to see you here :) On Fri, Apr 24, 2009 at 22:02, Ondrej Certik wrote: > Hi, > > is there a way to have one plot with two functions, one using some > scale, the other one a different scale and show for example one scale > on the left, the other scale on the right? sure, twinx() is what you're looking for; here is a simple example: import matplotlib.pyplot as plt import numpy as np x = np.arange(0., np.e, 0.01) y1 = np.exp(-x) y2 = np.log(x) fig = plt.figure() ax1 = fig.add_subplot(111) ax1.plot(x, y1) ax1.set_ylabel('Y values for exp(-x)') ax2 = ax1.twinx() ax2.plot(x, y2, 'r') ax2.set_xlim([0,np.e]) ax2.set_ylabel('Y values for ln(x)') ax2.set_xlabel('Same X for both exp(-x) and ln(x)') The values on X has to be of the same scale, tough, else the graph would look really weird. Cheers, -- Sandro Tosi (aka morph, morpheus, matrixhasu) My website: http://matrixhasu.altervista.org/ Me at Debian: http://wiki.debian.org/SandroTosi ```
 Re: [Matplotlib-users] two scales in the same plot From: Ondrej Certik - 2009-04-30 05:15:20 ```On Fri, Apr 24, 2009 at 1:10 PM, Sandro Tosi wrote: > Hi Ondrej, > nice to see you here :) Nice to see you too! :) > > On Fri, Apr 24, 2009 at 22:02, Ondrej Certik wrote: >> Hi, >> >> is there a way to have one plot with two functions, one using some >> scale, the other one a different scale and show for example one scale >> on the left, the other scale on the right? > > sure, twinx() is what you're looking for; here is a simple example: > > import matplotlib.pyplot as plt > import numpy as np > x = np.arange(0., np.e, 0.01) > y1 = np.exp(-x) > y2 = np.log(x) > fig = plt.figure() > ax1 = fig.add_subplot(111) > ax1.plot(x, y1) > ax1.set_ylabel('Y values for exp(-x)') > ax2 = ax1.twinx() > ax2.plot(x, y2, 'r') > ax2.set_xlim([0,np.e]) > ax2.set_ylabel('Y values for ln(x)') > ax2.set_xlabel('Same X for both exp(-x) and ln(x)') > > The values on X has to be of the same scale, tough, else the graph > would look really weird. Thanks a lot for the code. That worked. I was meeting some deadline, so I forgot to reply that it worked. Thanks Ryan and Jouni as well! Ondrej ```
 Re: [Matplotlib-users] two scales in the same plot From: Ryan May - 2009-04-24 20:14:37 Attachments: Message as HTML ```On Fri, Apr 24, 2009 at 3:02 PM, Ondrej Certik wrote: > Hi, > > is there a way to have one plot with two functions, one using some > scale, the other one a different scale and show for example one scale > on the left, the other scale on the right? > > I want to plot an atomic potential (one scale) and the corresponding > wave functions (different scale) in the same plot. I tried to look > through all examples and search this list, but didn't find anything. Look at pyplot.twinx() or pyplot.twiny() Ryan -- Ryan May Graduate Research Assistant School of Meteorology University of Oklahoma Sent from Norman, Oklahoma, United States ```
 Re: [Matplotlib-users] two scales in the same plot From: Jouni K. Seppänen - 2009-04-24 20:15:47 ```Ondrej Certik writes: > is there a way to have one plot with two functions, one using some > scale, the other one a different scale and show for example one scale > on the left, the other scale on the right? Yes, with twinx: see http://matplotlib.sourceforge.net/api/pyplot_api.html?highlight=twinx#matplotlib.pyplot.twinx -- Jouni K. Seppänen http://www.iki.fi/jks ```