@@ -1004,7 +1004,10 @@ def _plot_result2D(data, theory, resid, view, use_data, limits=None, label='theo
10041004 import plotly .graph_objects as go
10051005 from plotly .subplots import make_subplots
10061006
1007+ plot_number = 0
10071008 def add_trace (trace , row , col , label ):
1009+ nonlocal plot_number
1010+ plot_number += 1
10081011 colorbar = dict (
10091012 title = dict (text = label , side = 'right' ),
10101013 yanchor = "bottom" ,
@@ -1013,15 +1016,32 @@ def add_trace(trace, row, col, label):
10131016 if num_plots == 1 :
10141017 trace .update (colorbar = colorbar )
10151018 fig .add_trace (trace )
1016- fig . update_xaxes ( scaleanchor = f"y { row } " , scaleratio = 1 )
1017- fig .update_yaxes (scaleanchor = f"x { col } " , scaleratio = 1 )
1019+ # Use square pixels
1020+ fig .update_xaxes (scaleanchor = "y " , scaleratio = 1 )
10181021 else :
1019- trace .update (colorbar = dict (x = col / 2 - 0.05 , y = 1 - (row / 2 - 0.05 ), len = 0.45 , ** colorbar ))
1022+ if col == 1 : # data colorbar is full height; x position is tied to column width
1023+ cbarpos = dict (x = 0.6 , y = 0.05 , len = 0.95 )
1024+ else : # theory and residual colorbars are half height
1025+ cbarpos = dict (x = 1.0 , y = 1 - (row / 2 - 0.05 ), len = 0.45 )
1026+ trace .update (colorbar = {** cbarpos , ** colorbar })
10201027 fig .add_trace (trace , row = row , col = col )
1021- fig .update_xaxes (row = row , col = col , scaleanchor = f"y{ row } " , scaleratio = 1 )
1022- fig .update_yaxes (row = row , col = col , scaleanchor = f"x{ col } " , scaleratio = 1 )
1028+ # Use square pixels
1029+ # scaleanchor y{n} refers to the nth axes (1-origin) on the list of axes.
1030+ fig .update_xaxes (row = row , col = col , scaleanchor = f"y{ plot_number } " , scaleratio = 1 )
10231031
1024- fig = make_subplots (rows = 2 , cols = 2 ) if num_plots > 1 else go .Figure ()
1032+ if num_plots > 1 :
1033+ # Layout with data on left and theory + residuals stacked on the right.
1034+ # Data gets most of the width.
1035+ fig = make_subplots (
1036+ rows = 2 , cols = 2 ,
1037+ column_widths = [0.7 , 0.3 ],
1038+ horizontal_spacing = 0.15 ,
1039+ row_heights = [0.5 , 0.5 ],
1040+ specs = [[{'rowspan' : 2 }, {}], [None , {}]],
1041+ )
1042+ else :
1043+ # Single plot takes up the whole space
1044+ fig = go .Figure ()
10251045 if use_data :
10261046 trace = _plot_2d_signal (data , active_data , view = view , limits = limits , backend = backend )
10271047 add_trace (trace , 1 , 1 , data_label )
@@ -1120,29 +1140,21 @@ def _plot_2d_signal(data, signal, limits=None, view=None, label=None, backend='m
11201140 plt .imshow (masked_z ,
11211141 interpolation = 'nearest' , aspect = 'equal' , origin = 'lower' ,
11221142 extent = [xmin , xmax , ymin , ymax ],
1123- vmin = limits [0 ], vmax = limits [1 ])
1143+ vmin = limits [0 ], vmax = limits [1 ], label = label )
11241144 plt .xlabel ("q_x/Å" )
11251145 plt .ylabel ("q_y/Å" )
11261146
1127- # When plotting in bumps webview with mpld3 the chisq value is printed
1128- # above the right hand corner of the data graph. Move the labels for the
1129- # graphs to the left hand side to avoid collision. Unfortunately, mpld3
1130- # doesn't understand title with loc="left", so do the label as a text string
1131- # on the axes.
1132- if 0 :
1133- #plt.title(label, loc="left")
1134- plt .title (label )
1135- else :
1136- ax = plt .gca ()
1137- transform = ax .transAxes
1138- h_ex = 30 # assume we are 50 lines tall, so that 2/30 ~ 0.08
1139- text_offset = 0.5 / h_ex # 1/2 ex above and below the text
1140- x , y = text_offset , 1 + text_offset
1141- ha , va = "left" , "bottom"
1142- ax .text (x , y , label , transform = transform , va = va , ha = ha )
1143-
1144- #h = plt.colorbar(location='right')
1145- #h.set_label(label)
1147+ # Put the label on the top left of the plot.
1148+ _mpl_2d_label (plt .gca (), label )
1149+
1150+ def _mpl_2d_label (ax , title ):
1151+ transform = ax .transAxes
1152+ h_ex = 30 # assume we are 50 lines tall, so that 2/30 ~ 0.08
1153+ text_offset = 0.5 / h_ex # 1/2 ex above and below the text
1154+ x , y = text_offset , 1 - text_offset
1155+ ha , va = "left" , "top"
1156+ t = ax .text (x , y , title , transform = transform , va = va , ha = ha )
1157+ t .set_bbox (dict (facecolor = 'white' , edgecolor = 'white' , alpha = 0.7 ))
11461158
11471159
11481160# === The following is modified from sas.sasgui.plottools.PlotPanel
0 commit comments