2

I tried to make a plot and save it as svg. When I run the code that makes the plot (I'm using Jupyter), it makes the plot, and everything works fine. However, if I try to save it as svg the program returns ValueError:

ValueError: Transform failed with error code 525: Failed to execute 'insertBefore' on 'Node': The new child element contains the parent.

Here is the part of the code that runs without any errors:

x_values = [2.71152, 1.80768, 0.90384, 0.90384, 0.67788]
y_values = [430.28256, 503.08416, -235.5592, 626.42848, 752.61792]

x_new = [2.71152, 1.80768, 0.90384, 0.67788]
y_new = [430.28256, 503.08416, 626.42848, 752.61792]

a, b, r_val, p_val, slope_err = stats.linregress(x_new, y_new)
yfit = [a * xi + b for xi in x_new]

fig = go.Figure()
fig.add_traces([go.Scatter(name = "Data", 
                           x = x_values, 
                           y = y_values, 
                           mode = "markers+text", 
                           text = ["", r"$y = -144.5 x + 798.5$", "", ""],
                           textposition = "bottom left"), 
              go.Scatter(name = "Fit-Line", 
                           x = x_new, 
                           y = yfit, 
                           mode = "lines")])
fig.update_layout(font = dict(family = "CMU Serif", size = 16),
                  template = "simple_white", 
                  width = 900,
                  xaxis_title = r"$m \cdot (T_f - T_i), \text{ kg K}$",
                  yaxis_title = r"$Q_c, \text{ J}$")
fig.show()

And here is the line that throws the error:

fig.write_image("images/part-2-2-plot.svg")

It also says it "Cannot infer image type from output path '{file}'." However, I checked everything, the "images" directory exists in the folder, but the problem persists. Same problem if I try exporting to .png instead of .svg.

I tried removing parts of the code to see which line messes up everything. Found out that when I remove attributes xaxis_title and yaxis_title from the fig.update_layout() method, it does not throw any errors and exports the plot. Could someone please explain why it behaves like that? Did not seem to have problems with similar plots I made before.

I searched the internet but sadly did not find anything useful.

3
  • I don't get the exact same error, but I'm pretty sure either way the problem is the math in the axes titles. There's this bug with kaleido and mathjax. Kaleido is responsible for saving the images and depends on mathjax for making the latex math syntax work. One solution would be to use a different engine than kaleido, orca. The plotly documentation recommends kaleido and calls orca legacy, but you might not care. Another approach is saving the figure as .html but you might not want that. Commented Apr 13, 2023 at 21:25
  • If either of the above approaches works for you I can show how you can make it work in an answer. Commented Apr 13, 2023 at 21:28
  • @5eb yes, the first approach sounds right. In that project, I decided to remove axes titles, export the SVG, create separate SVGs with the titles using LaTeX and manually attach them to the exported SVG. However, a shorter and more direct way of solving the problem would be really useful in future. Commented Jun 1, 2023 at 19:38

1 Answer 1

0

It seems that there is a conflict between math style axes titles properties (i.e., xaxis_title and yaxis_title) and simple_white template (i.e., error in the source). For example, by changing the template to plotly_dark, you will get the following:

enter image description here

Another piece of evidence is changing the title from math style to a simple text also resolves the error, i.e., by keeping the simple_white template.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.