thomas-saigre / tikzplotly

Export plotly figures as TikZ/PGFplots for integration in LaTeX
https://pypi.org/project/tikzplotly/
MIT License
16 stars 0 forks source link

Traces with empty data cause an IndexError #17

Open Viech opened 1 week ago

Viech commented 1 week ago

Sometimes, procedurally, you might add a trace whose data is empty; this causes the following exception.

Traceback (most recent call last):
  File "…", line 232, in <module>
    tikzplotly.save(path, fig)
  File "…/.local/lib/python3.12/site-packages/tikzplotly/_save.py", line 123, in save
    code = get_tikz_code(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "…/.local/lib/python3.12/site-packages/tikzplotly/_save.py", line 59, in get_tikz_code
    data_str.append( draw_scatter2d(data_name_macro, trace, y_name, axis, colors_set) )
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "…/.local/lib/python3.12/site-packages/tikzplotly/_scatter.py", line 37, in draw_scatter2d
    if data_type(scatter.x[0]) == "date":
                 ~~~~~~~~~^^^
IndexError: tuple index out of range
thomas-saigre commented 1 week ago

Hi, thanks for spotting this error :) I did not manage to reproduce it, could you please provide a Minimal (Non)-Working Example leading to the error ?

thomas-saigre commented 1 day ago

@Viech The following code, where I manually set y data to an empty list

import plotly.express as px
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16])4
fig.data[0].y = None

import tikzplotly
tikzplotly.save("test.tex", fig)

raises the following code, which is not exactly what you got:

Error log ```py TypeError Traceback (most recent call last) in ----> 1 tikzplotly.save("tmp.tex", fig) ~/.local/lib/python3.10/site-packages/tikzplotly/_save.py in save(filepath, *args, **kwargs) 121 Additional arguments are passed to the backend. 122 """ --> 123 code = get_tikz_code(*args, **kwargs) 124 directory = Path(filepath).parent 125 if not directory.exists(): ~/.local/lib/python3.10/site-packages/tikzplotly/_save.py in get_tikz_code(fig, tikz_options, axis_options, include_disclamer, img_name) 80 81 if len(data_container.data) > 0: ---> 82 code += data_container.exportData() 83 code += "\n" 84 ~/.local/lib/python3.10/site-packages/tikzplotly/_dataContainer.py in exportData(self) 82 export_string += f"{sanitize_text(data.name)} {' '.join([sanitize_text(label) for label in data.y_label])}\n" 83 for i in range(len(data.x)): ---> 84 export_string += f"{data.x[i]} {' '.join([str(y[i]) for y in data.y_data])}\n" 85 86 export_string += "}" + sanitize_text(data.macro_name) + "\n" ~/.local/lib/python3.10/site-packages/tikzplotly/_dataContainer.py in (.0) 82 export_string += f"{sanitize_text(data.name)} {' '.join([sanitize_text(label) for label in data.y_label])}\n" 83 for i in range(len(data.x)): ---> 84 export_string += f"{data.x[i]} {' '.join([str(y[i]) for y in data.y_data])}\n" 85 86 export_string += "}" + sanitize_text(data.macro_name) + "\n" TypeError: 'NoneType' object is not subscriptable ```

But still, there is some bug that I need to fix ;)