tikzplotlib
tikzplotlib copied to clipboard
Suggestion: Add a filter function for artists
When using matplotlib2tikz, sometimes some objects are produced which are invisible in a plot but somehow visible in the pgfplot (usually paths with 'fill opacity=0'). I understand that it is difficult to account for all cases, but I think a nice and easy feature would be for matplotlib2tikz to call a user supplied function that filters unwanted objects.
An interesting idea! Is there a simple filter you have in mind?
I was thinking of providing a function that accepts an object as an argument and returns True when the object should be added to the output, and False otherwise.
If provided, matplotlib2tikz would then call the function when iterating over the children in _recurse in save.py and only add the object if True is returned.
Then, as the user, I would return False if the fillstyle of a line is None or when the object is a PathCollection (since I know I didn't create these objects).
I guess that's possible. Do you have a good idea for a small test for this?
Sure. This code produces a few unnecessary paths
plt.clf()
l1, = plt.plot(1, 1, '-k')
plt.savefig('/tmp/tmp.pdf', bbox_inches='tight')
from matplotlib2tikz import save as tikz_save
tikz_save("/tmp/tmp.tex", plt.gcf())
These paths are created by savefig but they are visible when saved in tikz. So I was thinking of something along the lines of
tikz_save("/tmp/tmp.tex", plt.gcf(),
filter=lambda obj: not isinstance(obj, mpl.collections.PathCollection))
to filter these paths out.