From 8f6fb1c0fa4665ac137242e778b7fd636f921ec8 Mon Sep 17 00:00:00 2001 From: Kostas Vilkelis <kostasvilkelis@gmail.com> Date: Wed, 12 Feb 2020 00:41:02 +0000 Subject: [PATCH] fixed plot in lecture 10 --- src/10_xray.md | 216 +++++++++++++++++++------------------------------ 1 file changed, 81 insertions(+), 135 deletions(-) diff --git a/src/10_xray.md b/src/10_xray.md index 2493d7b3..9c85e63b 100644 --- a/src/10_xray.md +++ b/src/10_xray.md @@ -100,162 +100,108 @@ which is clearly 1. ### Example of a two-dimensional reciprocal lattice ```python -# Define the lattice vectors +# Define primitive lattice vectors a1 = np.array([1,0]) a2 = np.array([0.5,sqrt(3)/2]) -a1_alt = -a1-a2 -a2_alt = a1_alt + a1 -a2_c = np.array([0,sqrt(3)]) +# Compute reciprocal lattice vectors +b1,b2 = np.linalg.inv(np.array([a1,a2]).T) @ np.eye(2)*2*pi -# Produces the lattice to be fed into plotly -def lattice_generation(a1,a2,N=6): +fig = make_subplots(rows=1, cols=2,shared_yaxes=True,subplot_titles=('Real Space', 'Reciprocal Space')) + +# Generates the lattice given the lattice vectors +def lattice_generation(a1,a2,N): grid = np.arange(-N//2,N//2,1) xGrid, yGrid = np.meshgrid(grid,grid) return np.reshape(np.kron(xGrid.flatten(),a1),(-1,2))+np.reshape(np.kron(yGrid.flatten(),a2),(-1,2)) - -# Produces the dotted lines of the unit cell -def dash_contour(a1,a2, vec_zero = np.array([0,0]), color='Red'): - dotLine_a1 = np.transpose(np.array([a1,a1+a2])+vec_zero) - dotLine_a2 = np.transpose(np.array([a2,a1+a2])+vec_zero) +def subplot(a1,a2,col): + N = 6 + lat_points = lattice_generation(a1,a2,N) + line_a1 = np.transpose([[0,0],a1]) + line_a2 = np.transpose([[0,0],a2]) + dotLine_a1 = np.transpose([a1,a1+a2]) + dotLine_a2 = np.transpose([a2,a1+a2]) - def dash_trace(vec,color): - trace = go.Scatter( - x=vec[0], - y=vec[1], - mode = 'lines', - line_width = 2, - line_color = color, - line_dash='dot', - visible=False - ) - return trace - return dash_trace(dotLine_a1,color),dash_trace(dotLine_a2,color) - -# Makes the lattice vector arrow -def make_arrow(vec,text,color = 'Red',vec_zero = [0,0],text_shift = [-0.2,-0.1]): - annot = [dict( - x=vec[0]+vec_zero[0], - y=vec[1]+vec_zero[1], - ax=vec_zero[0], - ay=vec_zero[1], - xref='x', - yref='y', - axref='x', - ayref = 'y', - showarrow=True, - arrowhead=2, - arrowsize=1, - arrowwidth=3, - arrowcolor = color - ), - dict( - x=(vec[0]+vec_zero[0])/2+text_shift[0], - y=(vec[1]+vec_zero[1])/2+text_shift[1], - xref='x', - ayref = 'y', - text = text, - font = dict( - color = color, - size = 20 - ), - showarrow=False, - ) - ] - return annot - -# Create the pattern -pattern_points = lattice_generation(a1,a2) -pattern = go.Scatter(x=pattern_points.T[0],y=pattern_points.T[1],mode='markers',marker=dict( - color='Black', - size = 20, - symbol = 'star-open-dot') + fig.add_trace( + go.Scatter(visible=False, x=line_a1[0],y=line_a1[1],mode='lines',line_color='red' + ), row = 1, col = col ) - -# Lattice Choice A -latticeA = go.Scatter(visible = False,x=pattern_points.T[0],y=pattern_points.T[1],mode='markers',marker=dict( - color='Red', - size = 10, - ) + fig.add_trace( + go.Scatter(visible=False, x=line_a2[0],y=line_a2[1],mode='lines',line_color='red' + ), row = 1, col = col ) - -# Lattice Choice B -latB_points = lattice_generation(a1,a2) -latticeB = go.Scatter(visible = False, x=latB_points.T[0]+0.5,y=latB_points.T[1],mode='markers',marker=dict( - color='Blue', - size = 10, - ) + fig.add_trace( + go.Scatter(visible=False, x=dotLine_a1[0],y=dotLine_a1[1],mode='lines',line_color='red',line_dash='dot' + ), row = 1, col = col ) - -# Annotate the lattice vectors -# Button 2 -bt2_annot = make_arrow(a1,'$a_1$') + make_arrow(a2,'$a_2$') + make_arrow(a1_alt,'$a`_1$',color='Black',text_shift=[-0.55,-0.1]) + make_arrow(a2_alt,'$a`_2$',color='Black') -#Button 3 -bt3_annot = make_arrow(a1,'$a_1$', vec_zero = [-0.5,0], color = 'Blue') + make_arrow(a2,'$a_2$',vec_zero = [-0.5,0], color = 'Blue') -#Button 4 -bt4_annot = make_arrow(a2_c,'$a_2$') + make_arrow(a1,'$a_1$') - -# (0) Button 1, (0, 1-5) Button 2, (0, 6-8) Button 3, (0,1,9,10) -data = [pattern,latticeA,*dash_contour(a1,a2),*dash_contour(a1_alt,a2_alt,color='Black'), - latticeB, *dash_contour(a1,a2,vec_zero=[-0.5,0],color='Blue'), - *dash_contour(a1, a2_c)] - - -updatemenus=list([ - dict( - type="buttons", - direction="down", - active=0, - buttons=list([ - dict(label="Pattern", - method="update", - args=[{"visible": [True, False, False, False, False, False, False, False, False, False, False]}, - {"title": "Pattern", - "annotations": []}]), - dict(label="Lattice A", - method="update", - args=[{"visible": [True, True, True, True, True, True, False, False, False, False, False]}, - {"title": "Lattice A with a Primitive Unit Cell", - "annotations": bt2_annot}]), - dict(label="Lattice B", - method="update", - args=[{"visible": [True, False, False, False, False, False, True, True, True, False, False]}, - {"title": "Lattice B with a Primitive Unit Cell", - "annotations": bt3_annot}]), - dict(label="C.U.Cell", - method="update", - args=[{"visible": [True, True, False, False, False, False, False, False, False, True, True]}, - {"title": "Conventional Unit Cell", - "annotations": bt4_annot}]) - ]), + fig.add_trace( + go.Scatter(visible=False, x=dotLine_a2[0],y=dotLine_a2[1],mode='lines',line_color='red',line_dash='dot' + ), row = 1, col = col ) -] -) + fig.add_trace( + go.Scatter(visible=False, x=lat_points.T[0],y=lat_points.T[1],mode='markers',marker=dict( + color='Black', + size = 20 + ) + ), row = 1, col = col + ) -plot_range = 2 -axis = dict( +# Generate subplots to be used by the slider +N_values = 10 +for i in np.linspace(2.5,3.5,N_values): + subplot(a1*i,a2*i,1) + subplot(b1/i,b2/i,2) + +# Define the default subplot +active = 4 +for i in range(10): + fig.data[active*10+i].visible = True + +steps = [] +for i in range(N_values): + step = dict( + label = 'Lattice Constant', + method="restyle", + args=["visible", [False] * len(fig.data)], + ) + for j in range(10): + step["args"][1][i*10+j] = True # Toggle i'th trace to "visible" + steps.append(step) + +sliders = [dict( + tickcolor = 'White', + font_color = 'White', + currentvalue_font_color = 'Black', + active = active, + name = 'Lattice Constant', + steps = steps +)] + +plot_range = 5 +fig.update_layout( + sliders=sliders, + showlegend = False, + plot_bgcolor = 'rgb(254, 254, 254)', + width = 1000, + height = 500, + xaxis = dict( range=[-plot_range,plot_range], visible = False, showgrid = False, fixedrange = True + ), + yaxis = dict( + range = [-plot_range,plot_range], + visible = False, + showgrid = False, + fixedrange = True ) - -layout = dict( - showlegend = False, - updatemenus=updatemenus, - plot_bgcolor = 'rgb(254, 254, 254)', - width = 600, - height = 600, - xaxis = axis, - yaxis = axis ) - - - -fig=dict(data=data, layout=layout) -py.iplot(fig, filename='lattice_basics') +fig.update_yaxes(range=[-plot_range, plot_range], row=1, col=2) +fig.update_xaxes(range=[-plot_range, plot_range], row=1, col=2,visible=False) +fig.show() ``` -- GitLab