File size: 1,536 Bytes
79bd9ac | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | """Plotly helpers: consistent, theme-aware figures + CSV export for the logbook."""
import os
import numpy as np
import pandas as pd
import plotly.graph_objects as go
PALETTE = ["#4C78A8", "#F58518", "#54A24B", "#E45756", "#72B7B2", "#B279A2"]
def new_fig(title, xtitle, ytitle):
fig = go.Figure()
fig.update_layout(
title=title, xaxis_title=xtitle, yaxis_title=ytitle,
template="plotly_white", width=760, height=480,
legend=dict(borderwidth=1, bordercolor="#ccc"),
margin=dict(l=60, r=20, t=60, b=50),
)
return fig
def add_theory_sim(fig, x, theory, sim, sim_std=None, color=None, name=""):
"""Solid line = theory, markers = simulation (paper's convention)."""
color = color or PALETTE[0]
fig.add_trace(go.Scatter(x=x, y=theory, mode="lines", line=dict(color=color, width=2.5),
name=f"{name} theory"))
err = dict(type="data", array=sim_std, visible=True) if sim_std is not None else None
fig.add_trace(go.Scatter(x=x, y=sim, mode="markers", marker=dict(color=color, size=8,
symbol="circle", line=dict(color="white", width=1)),
error_y=err, name=f"{name} sim"))
def save(fig, df, outdir, stem):
os.makedirs(outdir, exist_ok=True)
html = os.path.join(outdir, stem + ".html")
csv = os.path.join(outdir, stem + ".csv")
fig.write_html(html, include_plotlyjs="cdn")
df.to_csv(csv, index=False)
print(f" wrote {html}\n wrote {csv}")
return html, csv
|