{"id":17212024,"url":"https://github.com/whitead/skunk","last_synced_at":"2025-08-21T06:30:40.056Z","repository":{"id":43301976,"uuid":"403128673","full_name":"whitead/skunk","owner":"whitead","description":"Insert SVG images into matplotlib elements. Can be used to also compose matplotlib plots by nesting them. ","archived":false,"fork":false,"pushed_at":"2023-11-27T17:17:35.000Z","size":50,"stargazers_count":74,"open_issues_count":3,"forks_count":7,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-12-10T07:11:22.912Z","etag":null,"topics":["matplotlib","python","svg"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/whitead.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-09-04T18:25:54.000Z","updated_at":"2024-11-25T21:36:53.000Z","dependencies_parsed_at":"2023-11-27T18:30:39.096Z","dependency_job_id":"0f6cd8fb-fe44-4fb1-afa8-6fd5ae3d82ca","html_url":"https://github.com/whitead/skunk","commit_stats":{"total_commits":27,"total_committers":1,"mean_commits":27.0,"dds":0.0,"last_synced_commit":"1f34ba535e3e5205877d22a39539eb2ec9328600"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whitead%2Fskunk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whitead%2Fskunk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whitead%2Fskunk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whitead%2Fskunk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/whitead","download_url":"https://codeload.github.com/whitead/skunk/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230494921,"owners_count":18235046,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["matplotlib","python","svg"],"created_at":"2024-10-15T02:59:03.705Z","updated_at":"2024-12-19T20:08:06.865Z","avatar_url":"https://github.com/whitead.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# skunk [![build](https://github.com/whitead/skunk/actions/workflows/tests.yml/badge.svg)](https://whitead.github.io/skunk/)[![PyPI version](https://badge.fury.io/py/skunk.svg)](https://badge.fury.io/py/skunk)\n\n \u003cimg src=\"https://raw.githubusercontent.com/whitead/skunk/main/tests/skunk.svg\"\u003e\n\nInsert SVG images into matplotlib elements. Can be used to also compose matplotlib plots by nesting them.\n\n```sh\npip install skunk\n```\n\n## Jupyter Notebooks\n\nTo show generated SVGs in Jupyter Notebooks:\n\n```py\nskunk.display(svg)\n```\n\n## Overwrite Subplot\n\n```py\nimport skunk\nimport numpy as np\nimport os\nimport matplotlib.pyplot as plt\n\nfig, axs = plt.subplots(ncols=2)\n\nx = np.linspace(0, 2 * np.pi)\naxs[0].plot(x, np.sin(x))\n\n# important line where we set ID\nskunk.connect(axs[1], 'sk')\n\nplt.tight_layout()\n\n# Overwrite using file path to my svg\n# Can also use a string that contains the SVG\nsvg = skunk.insert(\n    {\n        'sk': 'skunk.svg'\n    })\n\n# write to file\nwith open('replaced.svg', 'w') as f:\n    f.write(svg)\n# or in jupyter notebook\nskunk.display(svg)\n```\n\n### Output\n\n![image](https://user-images.githubusercontent.com/908389/132105794-f178b4c1-3378-46b9-81d8-18f8e2435a83.png)\n\n\n## SVG in Annotation\n\nRead about [annotation boxes first](https://matplotlib.org/stable/gallery/text_labels_and_annotations/demo_annotation_box.html)\n\n```py\nimport skunk\nfrom matplotlib.offsetbox import AnnotationBbox\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfig, ax = plt.subplots()\n\nx = np.linspace(0, 2 * np.pi)\nax.plot(x, np.sin(x))\n\n# new code: using skunk box with id sk1\nbox = skunk.Box(50, 50, 'sk1')\nab = AnnotationBbox(box, (np.pi / 2, 1),\n                    xybox=(-5, -100),\n                    xycoords='data',\n                    boxcoords='offset points',\n                    arrowprops=dict(arrowstyle=\"-\u003e\"))\nax.add_artist(ab)\n\n# sknunk box with id sk2\nbox = skunk.Box(50, 50, 'sk2')\nab = AnnotationBbox(box, (3 * np.pi / 2, -1),\n                    xybox=(-5, 100),\n                    xycoords='data',\n                    boxcoords='offset points',\n                    arrowprops=dict(arrowstyle=\"-\u003e\"))\n\nax.add_artist(ab)\n\n# insert current figure into itself at sk1\n# insert svg file in sk2\nsvg = skunk.insert(\n    {\n        'sk1': skunk.pltsvg(),\n        'sk2': 'skunk.svg'\n    })\n\n# write to file\nwith open('replaced2.svg', 'w') as f:\n    f.write(svg)\n# or in jupyter notebook\nskunk.display(svg)\n```\n\n### Output\n\n![image](https://user-images.githubusercontent.com/908389/132105868-f0e4ae23-3ebf-4630-b230-8279d5791169.png)\n\n## SVG to Replace Image\n\nSometimes you may want a raster image to appear if not using an SVG. This can be done with an `ImageBox`.\nThe example above is identical, except we replace the `skunk.Box` with a `skunk.ImageBox` that has the same\narguments (after first) as [`OffsetImage`](https://matplotlib.org/stable/api/offsetbox_api.html#matplotlib.offsetbox.OffsetImage)\n\n```py\n# use image box, so can have PNG when not in SVG\nwith open('skunk.png', 'rb') as file:\n    skunk_img = plt.imread(file)\nbox = skunk.ImageBox('sk2', skunk_img, zoom=0.1)\n```\n\n### Output\n\nYou can see that the inner image contains the raster now instead of the blue rectangle. This example is overly fancy, normally you won't be *recursively* nesting plots so the raster image will only appear if you're not using SVG.\n\n![image](https://user-images.githubusercontent.com/908389/133010015-a1713504-33b6-4c26-960d-6da50b5a9561.png)\n\n## Save to PDF\n\nI prefer [`cairosvg`](https://github.com/Kozea/CairoSVG):\n\n```py\nimport cairosvg\ncairosvg.svg2pdf(bytestring=svg, write_to='image.pdf')\n```\n\n## Layout a set of SVGs\n\nSometimes you just want to slap a bunch of SVGs together into a grid. You can do that with this method:\n\n```py\nsvg = skunk.layout_svgs(svgs)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhitead%2Fskunk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwhitead%2Fskunk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhitead%2Fskunk/lists"}