Created
July 6, 2018 12:50
-
-
Save thomasaarholt/a55857252b79622fb854e5630393a108 to your computer and use it in GitHub Desktop.
Plot hyperspy signal nicely
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
def plot_im(s, ax=None, scalebar=True, label=True, clim=None, cmap=None, cbar=True, loc=4, axislabel=False, colorbar_format="scientific", colorbar_decimals=1, dont_show=False, **kwargs): | |
""" | |
Plots the HS image nicely with a scalebar. Label can be modified or turned off. | |
Use dont_show=True to not show a plot, but still return it. | |
Returns: | |
fig - the figure | |
ax - the axes | |
im - the image data | |
""" | |
import matplotlib.pyplot as plt | |
xax = s.axes_manager[-2] | |
yax = s.axes_manager[-1] | |
if xax.scale == 1: | |
xax.scale = xax.scale | |
if yax.scale == 1: | |
yax.scale = yax.scale | |
x = xax.size | |
y = yax.size | |
imagesize = 8 # inches | |
if x > y: | |
w = imagesize | |
h = y/x * imagesize | |
else: #y > x: | |
h = imagesize | |
w = x/y * imagesize | |
if ax == None: | |
figure, ax = plt.subplots(figsize=(w,h)) | |
else: | |
figure = ax.figure | |
if cmap == None: | |
cmap=plt.get_cmap("Greys_r") | |
vmin = None | |
vmax = None | |
for key, value in kwargs.items(): | |
vmin = value if key == "vmin" else None | |
vmax = value if key == "vmax" else None | |
if clim==True: | |
# Plot clim within two median absolute deviations of median | |
median = np.median(s.data) | |
deviation = mad(s.data) | |
clim = [median - 5*deviation, median + 5*deviation] | |
im = ax.imshow( | |
np.rot90(s.data, k=0), | |
interpolation='nearest', | |
extent=[ | |
xax.low_value, | |
xax.high_value, | |
yax.high_value, | |
yax.low_value], | |
vmin=vmin, | |
vmax=vmax, | |
clim=clim, | |
cmap=cmap, | |
) | |
ax.set_xticks([]) | |
ax.set_yticks([]) | |
if axislabel == True: | |
ax.set_xlabel(xax.name) | |
ax.set_ylabel(yax.name) | |
if scalebar == True: | |
add_scalebar(ax,s,loc=loc) | |
if label: | |
if type(label) == str: | |
text = label | |
else: | |
text = s.metadata.General.title | |
ax.annotate( | |
s=text, | |
xy=(xax.low_value,yax.low_value), | |
#xy=(s.axes_manager[1].low_value,s.axes_manager[0].high_value), # This was the old one, may have been correct in some cases | |
xytext=(5, -5), | |
va='top', | |
ha='left', | |
textcoords='offset points', | |
fontsize = 12, | |
bbox=dict(facecolor='white', alpha=1, pad=5, | |
), | |
) | |
if cbar==True: | |
add_colourbar(figure, ax, im, colorbar_format, colorbar_decimals) | |
if dont_show == True: | |
plt.close() | |
return figure, ax, im |
Hi! Good point, shall add them as soon as possible. Might be a few days as I'm currently on holiday without a laptop. Out of curiosity, how did you find this?
Thanks for responding. I am trying to use HyperSpy to convert TIA images to TIFF, and want to display the image nicely. I didn't find a way to remove the white border and the title of the image by just using s.plot() function.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, where are the add_scalebar and add_colorbar functions defined?