nschloe / tikzplotlib

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

`clean_figure` not working due to dead code if lines temporarily leave visible box #589

Open Naikless opened 1 year ago

Naikless commented 1 year ago

The following code throws NotImplementedError: There is data outside of the box. Don't know how to handle during cleaning. Please check if x/ylim is to tight:

import matplotlib.pyplot as plt
from tikzplotlib import clean_figure

x = range(1001)
y = [1]*1001
y[500] = 100

fig = plt.figure()
plt.plot(x,y)
plt.xlim(400, 600)
plt.ylim(0,10)

clean_figure(fig)

The reason lies in _move_points_closer that supposedly once was thought to map the points outside the visible domain to points on the edge of that domain. However, it seems this was never implemented and this function only returns the data unchanged if there are no points outside the visible area or throws the above error otherwise.

In my actual use case I have a very long timesignal of which I only want to plot a short fraction. Cutting this out is already done by _prune_outside_box. However, the signal also has some peaks that lie outside the domain which trigger the above error.

Since _move_points_closer currently doesn't do anything anyway, it seems exaggerated to me to completely abort the figure cleaning process just because some values are still outside the axis area. I would suggest to change this into a warning instead.