0

I'm trying to animate a patch in matplotlib. The patch is supposed to be filled incrementally with a stroke of black while using the patch as a clip path. Essentially, I am trying to make these svg animations from the animCJK project in matplotlib. For that I extract the bezier curve info from the svg files for the patches (clip paths) as well as the stroking paths which can be found at the bottom of the svg files.

So what I have currently done is to copy this matplotlib example on clip paths and tried to replace the cirlce patch with my own patch that I create using the svgpath2mpl library. Then I would animate the filling of those patches as in the svg animations and put my own spin on them. The problem is that I get the following error when I set my patch from the svg file as the clip path instead of the circle patch from the matplotlib example:

Traceback (most recent call last):
  File "C:\Users\Chris\Desktop\quick.py", line 15, in <module>
    im.set_clip_path(patch)
  File "C:\Users\Chris\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\artist.py", line 793, in set_clip_path
    self._clippath = TransformedPath(path, transform)
  File "C:\Users\Chris\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\transforms.py", line 2708, in __init__
    _api.check_isinstance(Transform, transform=transform)
  File "C:\Users\Chris\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\_api\__init__.py", line 92, in check_isinstance
    raise TypeError(
TypeError: 'transform' must be an instance of matplotlib.transforms.Transform, not a None

So I need to provide transform=ax.transData to my patch (Path object) so that the clipping works like in the matplotlib example, but I can't seem to figure out how to do that. Can anyone tell me how to get this to work? Here's my code.

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.cbook as cbook
from svgpath2mpl import parse_path

string = "M181 359C174 359 165 363 166 371C167 382 176 389 183 396C198 412 199 435 204 455C222 538 238 623 276 700C300 745 333 786 374 816C385 824 400 826 411 818C418 814 420 805 420 797C421 780 417 762 420 745C422 720 430 696 436 671C437 666 438 657 431 654C424 652 417 658 414 664C399 686 387 710 372 731C370 734 366 735 364 732C347 718 331 702 319 683C303 658 294 629 285 601C276 570 267 538 260 506C254 477 250 448 249 419C249 409 254 399 253 389C251 379 240 375 232 370C216 363 199 359 181 359Z"
patch = parse_path(string)

with cbook.get_sample_data('grace_hopper.jpg') as image_file:
    image = plt.imread(image_file)

fig, ax = plt.subplots()
im = ax.imshow(image)
im.set_clip_path(patch)

ax.axis('off')
plt.show()
J.Doe
  • 224
  • 1
  • 4
  • 19
  • According to [set_clip_path requires patch to be plotted](https://stackoverflow.com/questions/25688573/matplotlib-set-clip-path-requires-patch-to-be-plotted) the transform should be set to `ax.transData`. So, `im.set_clip_path(patch, transform=ax.transData)`. – JohanC Jan 03 '23 at 17:21
  • that worked, thanks. weird that I didn't spot that before it seems so obvious... – J.Doe Jan 03 '23 at 20:51

0 Answers0