Created
November 30, 2023 20:15
-
-
Save JeanOlivier/32011fc4e9b39930d96be73674ccbfec to your computer and use it in GitHub Desktop.
imshow_smart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pylab import * | |
def ax_add_comment(ax, comment, fontsize=10, x=.5,y=.97): | |
try: | |
ax.commenttext.remove() # We avoid writing it over itself repeatedly | |
except: | |
pass | |
if not hasattr(ax, "plot3D"): | |
ax.commenttext = ax.text(x, y, comment, horizontalalignment='center', verticalalignment = 'top', fontsize=fontsize, transform=ax.transAxes) | |
else: # 3D case | |
ax.datetext = ax.text2D(0.12*x, 0.9*y, comment, horizontalalignment='right', verticalalignment = 'bottom', fontsize=fontsize, transform=ax.transAxes) | |
# Better imshow | |
def extent_from_xy(x,y): | |
x = r_[x] | |
y = r_[y] | |
deltax = x[1]-x[0] | |
deltay = y[1]-y[0] | |
return [x[0]-deltax/2., x[-1]+deltax/2., y[0]-deltay/2., y[-1]+deltay/2.] | |
def _imshow_smart(self, x, y, z, interpolation='none', aspect='auto', origin='lower', *args, **kwargs): | |
cbar = kwargs.pop('colorbar', True) | |
ret = self.imshow(z, aspect=aspect, origin='lower', extent=extent_from_xy(x,y), interpolation=interpolation, *args, **kwargs) | |
if cbar: | |
self.colorbar = plt.colorbar(ret, ax=self) | |
return ret | |
matplotlib.axes.Axes.add_comment = ax_add_comment | |
matplotlib.axes.Axes.imshow_smart = _imshow_smart | |
imshow_smart = lambda *args, **kwargs: _imshow_smart(gca(), *args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment