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.

Azamat
  • 121
  • 2
  • 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. – 5eb Apr 13 '23 at 21:25
  • If either of the above approaches works for you I can show how you can make it work in an answer. – 5eb Apr 13 '23 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. – Azamat Jun 01 '23 at 19:38

0 Answers0