Repository navigation
Expand file tree
/
Copy pathplotly_config.py
More file actions
135 lines (111 loc) · 3.61 KB
/
Copy pathplotly_config.py
File metadata and controls
135 lines (111 loc) · 3.61 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import plotly.tools as tls
import plotly.graph_objects as go
import plotly.io as pio
import os
def style_plot(
fig,
xaxis_title,
yaxis_title,
title,
subtitle="",
font=dict(family="Arial", size=14),
title_size=35,
axis_title_size=30,
tick_size=25,
legend_size=15,
hover_size=20,
xscale='linear',
yscale='linear'
):
"""
Coded and formatted with the help of ChatGPT
Styles a Plotly figure with consistent formatting of axes, titles,
fonts, hover labels, and axis scale (linear or log).
Parameters:
- fig: plotly.graph_objs.Figure
- xaxis_title, yaxis_title, title, subtitle: text labels
- font: dict(global font settings)
- title_size, axis_title_size, tick_size, legend_size, hover_size: int font sizes
- xscale, yscale: 'linear' or 'log'
"""
# Subtitle
full_title = f"{title}<br><sup>{subtitle}</sup>" if subtitle else title
fig.update_layout(
plot_bgcolor='white',
font=font,
title=dict(text=full_title, font=dict(size=title_size), x=0.0, y=0.95),
legend=dict(font=dict(size=legend_size)),
hoverlabel=dict(font_size=hover_size),
)
fig.update_xaxes(
type=xscale,
title=dict(text=xaxis_title, font=dict(size=axis_title_size)),
tickfont=dict(size=tick_size),
mirror=True, showline=True, linecolor='black', gridcolor='lightgrey'
)
fig.update_yaxes(
type=yscale,
title=dict(text=yaxis_title, font=dict(size=axis_title_size)),
tickfont=dict(size=tick_size),
mirror=True, showline=True, linecolor='black', gridcolor='lightgrey'
)
return fig
def save_plotly_figure(
fig,
savedir,
filename,
channel=None,
image_format="png",
scale=4,
include_plotlyjs="cdn",
append = False,
htmloutputfile = None
):
"""
Save a Plotly figure to a standalone HTML file with customizable high-resolution
export options in the built-in download button.
Parameters
----------
fig : plotly.graph_objs.Figure
The Plotly figure to save.
savedir : str
Directory where the HTML file will be written. Will be created if missing.
filename : str
Base name (without .html) of the output file.
channel : int, optional
If provided, the downloadable image filename becomes: filename_ChannelX
image_format : str
Format used by the built-in Plotly download button ("png", "svg", "jpeg", "webp").
scale : int
Multiplier for export resolution. Higher = higher DPI.
Returns
-------
str
The full path of the saved HTML file.
Notes
-----
- The output HTML file contains a live, interactive plot.
- The figure's built-in download button exports a high-resolution image.
- include_plotlyjs="cdn" keeps the file small and loads Plotly JS from the web.
"""
# Configure the camera (download) button
export_name = filename if channel is None else f"{filename}_Channel{channel}"
config = {
"toImageButtonOptions": {
"format": image_format,
"filename": export_name,
"scale": scale
}
}
if not append:
# Ensure directory exists
os.makedirs(savedir, exist_ok=True)
filepath = os.path.join(savedir, f"{filename}.html")
fig.write_html(
filepath,
include_plotlyjs=include_plotlyjs,
full_html=True,
config=config
)
else:
htmloutputfile.write(fig.to_html(full_html=False,config=config,include_plotlyjs=include_plotlyjs))