I am using Plotly to generate interactive plots for an HTML report I’m making. My approach is to first generate and save the plot, then source it in to my HTML report using iFrame.
(1) Sample Python code to produce Plotly:
import plotly
import plotly.graph_objs as go
# Generate Plotly plot
sample_plotly = go.Figure()
sample_plotly.add_trace(go.Scatter(x=np.random.randn(3), y=np.random.randn(3), marker_color="black"))
sample_plotly.add_trace(go.Scatter(x=np.random.randn(3), y=np.random.randn(3), marker_color="blue"))
# Save plot
plotly_html = sample_plotly.to_html()
file = open("sample_plotly.html", "w")
file.write(plotly_html)
file.close()
(2) Now we source this Plotly plot into an HTML file using iFrame:
<html>
<head>
</head>
<body>
<h1>See legend in plot</h1>
<details>
<summary>Folded Section</summary>
<p>
<div style = "text-align: left;">
<iframe style="border: none;" src="./sample_plotly.html" dpi="300" width="70%" height="500px"></iframe>
</div>
</p>
</details>
</body>
</html>
How the bug in the plot looks like:
How the plot should look like:
I’ve read some threads before, and I’m quite sure this has something to do with the CSS being set to display:none
. This problem also appears sometimes in tabs, and it’s not only the legend that gets messed up (e.g. axis labels can overlap as well).