Dual Y-Axes on Imshow

Good afternoon all,

One last matplotlib question for the group for today. On one of my GUI plots, I’m calling imshow on an array of data (to display it in the same way MATLAB’s imagesc command does). I’d like to add a second y-axis to the right side of the plot that is completely dependent on the values on the primary y-axis. Essentially, for each y-axis tick point, I’ll put the y-axis ‘value’ into a formula and then put the result on the second y-axis. I did this in MATLAB by essentially overlaying a second set of axes over the plot, but I haven’t found the exact way to do it with matplotlib yet. I’ve seen a few examples online, but they all use the second overlaid plot to actually plot new data - I wouldn’t be doing this.

Would I need to use the twinx (or twiny) function?

Are there examples of this on the web that I haven’t found that somebody could point me towards?

Thank you.

Jason Stone, on 2011-02-18 14:39, wrote:

Good afternoon all,
One last matplotlib question for the group for today. On one of my GUI
plots, I'm calling imshow on an array of data (to display it in the same way
MATLAB's imagesc command does). I'd like to add a second y-axis to the
right side of the plot that is completely dependent on the values on the
primary y-axis. Essentially, for each y-axis tick point, I'll put the
y-axis 'value' into a formula and then put the result on the second y-axis.
I did this in MATLAB by essentially overlaying a second set of axes over
the plot, but I haven't found the exact way to do it with matplotlib yet.
I've seen a few examples online, but they all use the second overlaid plot
to actually plot new data - I wouldn't be doing this.
Would I need to use the twinx (or twiny) function?
Are there examples of this on the web that I haven't found that somebody
could point me towards?

Hi Jason,

here's an example that does what you want, using e^x as the
formula, change the paramter to fmtr to suit your needs:

ax = plt.subplot(1,1,1)
ax.plot(np.sin(np.linspace(0,np.pi)))
ax2 = ax.twinx()
ax2._sharey = ax # share both x and y
fmtr = mpl.ticker.FuncFormatter(lambda x,pos: "%.2f"%np.exp(x))
ax2.yaxis.set_major_formatter(fmtr)
plt.draw()

best,

···

--
Paul Ivanov
314 address only used for lists, off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7