nschloe / tikzplotlib

:bar_chart: Save matplotlib figures as TikZ/PGFplots for smooth integration into LaTeX.
MIT License
2.42k stars 218 forks source link

UnboundLocalError for bar plot with nan data #434

Open andreassch opened 4 years ago

andreassch commented 4 years ago

Converting a bar plot with error bars which includes nan data produces a UnboundLocalError in _path.py. With the following minimal example

import numpy as np
from matplotlib import pyplot as plt
from tikzplotlib import save as tikz_save

data = np.array([1,2,3,np.nan])
err = np.array([1,2,3,np.nan])
plt.bar(range(len(data)), data, yerr=err)
tikz_save("test_bar.tikz")
plt.show()

I get the following exception:

Traceback (most recent call last):
  File "test_bar.py", line 15, in <module>
    tikz_save("test_bar.tikz")
  File ".local/lib/python3.7/site-packages/tikzplotlib/_save.py", line 253, in save
    code = get_tikz_code(*args, filepath=filepath, **kwargs)
  File ".local/lib/python3.7/site-packages/tikzplotlib/_save.py", line 207, in get_tikz_code
    data, content = _recurse(data, figure)
  File ".local/lib/python3.7/site-packages/tikzplotlib/_save.py", line 351, in _recurse
    data, children_content = _recurse(data, child)
  File ".local/lib/python3.7/site-packages/tikzplotlib/_save.py", line 376, in _recurse
    data, cont = _draw_collection(data, child)
  File ".local/lib/python3.7/site-packages/tikzplotlib/_save.py", line 319, in _draw_collection
    return _line2d.draw_linecollection(data, child)
  File ".local/lib/python3.7/site-packages/tikzplotlib/_line2d.py", line 122, in draw_linecollection
    data, path, draw_options=options, simplify=False
  File ".local/lib/python3.7/site-packages/tikzplotlib/_path.py", line 111, in draw_path
    return data, path_command, draw_options, is_area
UnboundLocalError: local variable 'is_area' referenced before assignment

The function shouldn't return an uninitialized variable. I would expect an output plot which has no bar/errorbar at the place of the nan data point. I'm using tikzplotlib 0.9.3 installed via pip and Python 3.7.

andreassch commented 4 years ago

The UnboundLocalError can easily be fixed by inserting

is_area = False

at line 31 in _path.py. However, in the generated pgfplots file I still have to delete the lines corresponding to the nan data points to make it compile with (pdf)latex. Below you find the generated file with the lines in question commented out by me.

% This file was created by tikzplotlib v0.9.3.
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-0.54, xmax=2.54,
xtick style={color=black},
y grid style={white!69.0196078431373!black},
ymin=0, ymax=6.3,
ytick style={color=black}
]
\draw[draw=none,fill=color0] (axis cs:-0.4,0) rectangle (axis cs:0.4,1);
\draw[draw=none,fill=color0] (axis cs:0.6,0) rectangle (axis cs:1.4,2);
\draw[draw=none,fill=color0] (axis cs:1.6,0) rectangle (axis cs:2.4,3);
%\draw[draw=none,fill=color0] (axis cs:2.6,0) rectangle (axis cs:3.4,nan);
\path [draw=black, semithick]
(axis cs:0,0)
--(axis cs:0,2);

\path [draw=black, semithick]
(axis cs:1,0)
--(axis cs:1,4);

\path [draw=black, semithick]
(axis cs:2,0)
--(axis cs:2,6);

%\path [draw=black, semithick]
%;

\end{axis}

\end{tikzpicture}