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](http://pirsquared.org) | GPG/PGP key id:
0x0F3E28F7
Paul,
I am currently doing something very similar and was hoping I could
ask for a little clarification. I want to have two y axes where the
ticks are in the same locations and the 2nd y-axis labels are just
twice the 1st y-axis labels. When I implement your example, I don’t
quite understand how to ensure the ticks are at the same locations.
Also, when the data changes to a new image, the 1st y-axis updates
but the 2nd y-axis does not. Is there a convenient way to force both
to update each time, or does the figure need to be cleared and
essentially built from scratch each time.
for example, with the following code:
import numpy as np
import matplotlib
import matplotlib.colorbar as cb
import matplotlib.pyplot as plt
y = np.reshape(np.arange(0, 1000000, 1), (20000, 50))
test = 'hot'
f1 = plt.figure(1)
f1.patch.set_facecolor('#c0c0c0')
ax1 = f1.add_axes([0.09, 0.15, 0.82, 0.80])
axc = f1.add_axes([0.09, 0.05, 0.82, 0.05])
im1 = ax1.imshow(y, cmap=test, aspect='auto', origin='lower')
cb.Colorbar(axc, im1, orientation='horizontal')
ax2 = ax1.twinx()
ax2._sharey = ax1 # share both x and y
fmtr = matplotlib.ticker.FuncFormatter(lambda x,pos:
“%.2f”%np.exp(x))
ax2.yaxis.set_major_formatter(fmtr)
plt.show()
The ticks on the right are obviously at different locations compared
to the left ticks. Also, if imshow was to be called a new array of
data, the ticks on the right would remain the same. So is there an
easy way to force the locations to be the same, and is there an easy
way force the right y-axis to update each time? Thank you very much
for your time and help.
-Thomas
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
iEYEARECAAYFAk1kEfIACgkQe+cmRQ8+KPe0vACfSMtFJ9KSRwqU34j6QevaSZqD
qM0An2WHMyKisrwDIyKaCcuygrsWvZbX
=OIU1
-----END PGP SIGNATURE-----
Free Software Download: Index, Search & Analyze Logs and other
IT data in
Real-Time with Splunk. Collect, index and harness all the fast
moving IT data
generated by your applications, servers and devices whether
physical, virtual
or in the cloud. Deliver compliance at lower cost and gain new
business
insights. [http://p.sf.net/sfu/splunk-dev2dev](http://p.sf.net/sfu/splunk-dev2dev)
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
[https://lists.sourceforge.net/lists/listinfo/matplotlib-users](https://lists.sourceforge.net/lists/listinfo/matplotlib-users)