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.