Skip to content

Commit 5e8f3e4

Browse files
authored
Merge pull request #35 from DanPorter/add_3d_intensity_plot
Add 3D intensity plot
2 parents 1f58f80 + a579d81 commit 5e8f3e4

File tree

4 files changed

+119
-551
lines changed

4 files changed

+119
-551
lines changed

Dans_Diffraction/classes_plotting.py

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def plot_distance(self, min_d=0.65, max_d=3.20, labels=None,
175175
axs[i].hist(dist[site]['dist'], range=ranges,
176176
bins=int((ranges[1] - ranges[0]) / step))
177177
axs[i].set(ylabel='n. Atoms')
178-
axs[-1].set(xlabel='$\AA$')
178+
axs[-1].set(xlabel=r'$\AA$')
179179
if len(dist) > 1:
180180
for ax in axs.flat:
181181
ax.label_outer()
@@ -955,7 +955,86 @@ def plot_3Dlattice(self,q_max=4.0,x_axis=[1,0,0],y_axis=[0,1,0],centre=[0,0,0],c
955955
ax.set_zlim3d(-q_max, q_max)
956956

957957
fp.labels(self.xtl.name,'Qx','Qy','Qz')
958-
958+
959+
def plot_3Dintensity(self, q_max=4.0, central_hkl=(0, 0, 0), show_forbidden=False):
960+
"""
961+
Plot Reciprocal Space lattice points in 3D, with point size and colour based on the intensity
962+
"""
963+
from matplotlib.colors import Normalize
964+
965+
# Generate lattice of reciprocal space points
966+
hmax, kmax, lmax = fc.maxHKL(q_max, self.xtl.Cell.UVstar())
967+
HKL = fc.genHKL([hmax, -hmax], [kmax, -kmax], [lmax, -lmax])
968+
HKL = HKL + central_hkl # reflection about central reflection
969+
Q = self.xtl.Cell.calculateQ(HKL)
970+
intensity = self.xtl.Scatter.intensity(HKL)
971+
reflections = intensity > 0.1
972+
extinctions = intensity < 0.1
973+
974+
# Create plot
975+
fig = plt.figure(figsize=self._figure_size, dpi=self._figure_dpi)
976+
ax = fig.add_subplot(111, projection='3d')
977+
978+
# Create cell box
979+
uvw = np.array([[0., 0, 0], [1, 0, 0], [1, 0, 1], [1, 1, 1], [1, 1, 0], [0, 1, 0], [0, 1, 1],
980+
[0, 0, 1], [1, 0, 1], [1, 0, 0], [1, 1, 0], [1, 1, 1], [0, 1, 1], [0, 1, 0], [0, 0, 0],
981+
[0, 0, 1]])
982+
bpos = self.xtl.Cell.calculateQ(uvw + central_hkl)
983+
ax.plot(bpos[:, 0], bpos[:, 1], bpos[:, 2], '-', c='k') # cell box
984+
fp.plot_arrow(bpos[[0, 1], 0], bpos[[0, 1], 1], bpos[[0, 1], 2], col='r', width=4, arrow_size=10) # a*
985+
fp.plot_arrow(bpos[[0, 5], 0], bpos[[0, 5], 1], bpos[[0, 5], 2], col='g', width=4, arrow_size=10) # b*
986+
fp.plot_arrow(bpos[[0, 7], 0], bpos[[0, 7], 1], bpos[[0, 7], 2], col='b', width=4, arrow_size=10) # c*
987+
988+
cmap = plt.get_cmap('hot_r')
989+
vmin, vmax = 0, np.median(intensity[intensity > 0.1])
990+
991+
norm_intensity = intensity / intensity.max()
992+
# colours = cmap(norm_intensity)
993+
norm = Normalize(vmin=vmin, vmax=vmax)
994+
max_point_size = 500
995+
sizes = max_point_size * norm_intensity
996+
997+
# Reflections
998+
sct = ax.scatter(Q[reflections,0], Q[reflections,1], Q[reflections,2],
999+
c=intensity[reflections], s=sizes[reflections],
1000+
marker='o', cmap=cmap, norm=norm)
1001+
# Extinctions
1002+
if show_forbidden:
1003+
ax.scatter(Q[extinctions,0], Q[extinctions,1], Q[extinctions,2], marker='x', c='k', s=20)
1004+
1005+
cx, cy, cz = self.xtl.Cell.calculateQ(central_hkl).squeeze()
1006+
ax.set_xlim3d(cx - 2 * q_max, cx + 2 * q_max)
1007+
ax.set_ylim3d(cy - 2 * q_max, cy + 2 * q_max)
1008+
ax.set_zlim3d(cz - 2 * q_max, cz + 2 * q_max)
1009+
1010+
fp.labels(self.xtl.name, 'Qx', 'Qy', 'Qz')
1011+
plt.colorbar(sct, label='intensity')
1012+
1013+
def plot_intensity_histogram(self, q_max=4.0):
1014+
"""
1015+
Plot histogram of intensity of reflections
1016+
"""
1017+
# Generate lattice of reciprocal space points
1018+
hmax, kmax, lmax = fc.maxHKL(q_max, self.xtl.Cell.UVstar())
1019+
HKL = fc.genHKL([hmax, -hmax], [kmax, -kmax], [lmax, -lmax])
1020+
# HKL = HKL + centre # reflection about central reflection
1021+
Q = self.xtl.Cell.calculateQ(HKL)
1022+
qmag = fg.mag(Q)
1023+
intensity = self.xtl.Scatter.intensity(HKL)
1024+
1025+
n_bins = 100 if len(qmag) > 100 else len(qmag)
1026+
log_bins = np.logspace(0, np.log10(intensity.max()), n_bins)
1027+
1028+
fig = plt.figure(figsize=self._figure_size, dpi=self._figure_dpi)
1029+
ax = fig.add_subplot(111)
1030+
# ax.hist(np.log10(intensity[intensity > 0]), bins=log_bins)
1031+
ax.hist(np.log10(intensity + 1), bins=log_bins)
1032+
ax.set_xscale('log')
1033+
ax.set_xlabel('intensity')
1034+
ax.set_ylabel('Count')
1035+
ax.set_title(self.xtl.name)
1036+
1037+
9591038
def quick_intensity_cut(self,x_axis=[1,0,0],y_axis=[0,1,0],centre=[0,0,0], q_max=4.0,cut_width=0.05):
9601039
"""
9611040
Plot a cut through reciprocal space, visualising the intensity as different sized markers

Dans_Diffraction/functions_plotting.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,8 @@ def labels(ttl=None, xvar=None, yvar=None, zvar=None, legend=False, size='Normal
135135
plt.gca().set_ylabel(yvar, fontsize=lab, fontname=font)
136136

137137
if zvar is not None:
138-
# Don't think this works, use ax.set_zaxis
139138
plt.gca().set_zlabel(zvar, fontsize=lab, fontname=font)
140-
for t in plt.gca().zaxis.get_major_ticks():
141-
t.label.set_fontsize(tik)
142-
t.label.set_fontname(font)
139+
plt.gca().zaxis.set_tick_params(labelsize=lab, labelfontfamily=font)
143140

144141
if legend:
145142
plt.legend(loc=0, frameon=False, prop={'size': leg, 'family': 'serif'})

0 commit comments

Comments
 (0)