tikzplotlib icon indicating copy to clipboard operation
tikzplotlib copied to clipboard

Suggestion: Add a filter function for artists

Open haji-ali opened this issue 8 years ago • 4 comments
trafficstars

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.

haji-ali avatar Jun 09 '17 16:06 haji-ali

An interesting idea! Is there a simple filter you have in mind?

nschloe avatar Jun 09 '17 17:06 nschloe

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).

haji-ali avatar Jun 09 '17 17:06 haji-ali

I guess that's possible. Do you have a good idea for a small test for this?

nschloe avatar Jun 12 '17 12:06 nschloe

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.

haji-ali avatar Jun 14 '17 14:06 haji-ali