Skip to content

Commit bdbf066

Browse files
author
Paul Kienzle
committed
fix 2D plots and enable plotly in webview
1 parent 88347c9 commit bdbf066

2 files changed

Lines changed: 42 additions & 38 deletions

File tree

sasmodels/compare.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ def plot_models(opts, result, limits=None, setnum=0):
971971
if have_base:
972972
if have_comp:
973973
plt.subplot(221)
974-
plot_theory(base_data, base_value, label=base_name, view=view, use_data=use_data, limits=limits)
974+
plot_theory(base_data, base_value, label=base_name, view=view, use_data=use_data, limits=limits, backend='matplotlib')
975975
if setnum > 0:
976976
plt.legend([f"Set {k+1}" for k in range(setnum+1)], loc='best')
977977
plt.title("%s t=%.2f ms"%(base.model.info.name, base_time))
@@ -985,8 +985,8 @@ def plot_models(opts, result, limits=None, setnum=0):
985985
if have_base:
986986
plt.subplot(223)
987987
if not opts['is2d'] and have_base:
988-
plot_theory(comp_data, base_value, label=base_name, view=view, use_data=use_data, limits=limits)
989-
plot_theory(comp_data, comp_value, label=comp_name, view=view, use_data=use_data, limits=limits)
988+
plot_theory(comp_data, base_value, label=base_name, view=view, use_data=use_data, limits=limits, backend='matplotlib')
989+
plot_theory(comp_data, comp_value, label=comp_name, view=view, use_data=use_data, limits=limits, backend='matplotlib')
990990
plt.title("%s t=%.2f ms"%(comp.model.info.name, comp_time))
991991
#plt.gca().tick_params(labelbottom=False, labelleft=False)
992992
#plt.gca().set_yticks([])
@@ -1008,7 +1008,7 @@ def plot_models(opts, result, limits=None, setnum=0):
10081008
# Note: base_data only since base and comp have same q values (though
10091009
# perhaps different resolution), and we are plotting the difference
10101010
# at each q
1011-
plot_theory(base_data, err, view=errview, label=errstr, use_data=use_data)
1011+
plot_theory(base_data, err, view=errview, label=errstr, use_data=use_data, backend='matplotlib')
10121012
plt.xscale('log' if view == 'log' and not opts['is2d'] else 'linear')
10131013
plt.title("max %s = %.3g"%(errstr, abs(err).max()))
10141014
if opts['is2d']:
@@ -1018,14 +1018,6 @@ def plot_models(opts, result, limits=None, setnum=0):
10181018
else:
10191019
plt.ylabel(errstr)
10201020

1021-
#cbar_title = errstr if errview=="linear" else "log "+errstr
1022-
# if is2D:
1023-
# h = plt.colorbar()
1024-
# h.ax.set_title(cbar_title)
1025-
# fig = plt.gcf()
1026-
# extra_title = ' '+opts['title'] if opts['title'] else ''
1027-
# fig.suptitle(":".join(opts['name']) + extra_title)
1028-
10291021
if have_base and have_comp and opts['show_hist']:
10301022
plt.figure()
10311023
v = relerr

sasmodels/data.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)