GithubHelp home page GithubHelp logo

nvictus / svgpath2mpl Goto Github PK

View Code? Open in Web Editor NEW
59.0 4.0 12.0 1.49 MB

SVG path parser for matplotlib

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%
matplotlib svg svg-path parser python

svgpath2mpl's Introduction

svgpath2mpl

Github Actions CI

PyPI version

Parse SVG paths into matplotlib Path objects for plotting.

A path in SVG is defined by a 'path' element which contains a d="(path data)" attribute that contains moveto, line, curve (both cubic and quadratic Béziers), arc and closepath instructions. Matplotlib actually supports all of these instructions natively but doesn't provide a parser or fully compatible API.

image

Based on:

  1. svg_parse for matplotlib by Nicolas P. Rougier (BSD license).
  2. svg.path by Lennart Regebro (MIT license).

This module adds the path commands missing from (1), including smooth curves and endpoint-parameterized elliptical arcs.

>>> from svgpath2mpl import parse_path
>>> parse_path('M 100 100 L 300 100')
Path(array([[ 100.,  100.], [ 300.,  100.]]), array([1, 2], dtype=uint8))

See the Jupyter Notebook gallery of examples.

Resources

See the SVG Specification.

See the matplotlib path tutorial and API docs.

License

BSD (3-Clause).

svgpath2mpl's People

Contributors

ickc avatar mapio avatar nvictus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

svgpath2mpl's Issues

matplotlib patches.Arc behavior changed

I believe by mpl 2.0, the underlying implementation for extracting path commands from Arc patches no longer works as expected, causing arc commands to no longer render properly. I was working on a replacement back in March, but filing the issue now.

Missing portions rendering some shapes

Hello.

I'm trying to render some SVGs like: https://fontawesome.com/icons/ice-cream

But I get some strange behavior, as some areas are ignored. It seems the source path has self-intersections, so I wonder if svgpath2mpl handles them.

The path in that SVG is: {'d': 'M368 160h-.94a144 144 0 1 0-286.12 0H80a48 48 0 0 0 0 96h288a48 48 0 0 0 0-96zM195.38 493.69a31.52 31.52 0 0 0 57.24 0L352 288H96z'}.

image

Any insight is appreciated.

"Absolute" Horizontal and Vertical lines still need to use one of existing coordinates

In the case of an 'absolute' horizontal line, you still need to use the existing Y value as the new Y value.
In the case of an 'absolute' vertical line, you still need to use the existing X value as the new X value.

This is different from most of the path elements, where "absolute" means that both the X and Y value should be used.

The following path will not be drawn correctly:

<path fill="#FF8080" stroke="#000000" stroke-width="5" d="M173.25,517V327l132-122l132,122v190"/>

This can be fixed by (for example) modifying parse_path as follows:

...
        elif command == 'H':
            # Horizontal line.
            verts = np.r_[values, 0. if is_relative else current_point[1]]
            codes = COMMANDS[command]

        elif command == 'V':
            # Vertical line.
            verts = np.r_[0. if is_relative else current_point[0], values]
            codes = COMMANDS[command]
...

Incorrect parsing of M x,y x,y x,y ...

According to the SVG 1.1 standard, "If a moveto is followed by multiple pairs of coordinates, the subsequent pairs are treated as implicit lineto commands."

svgpath2mpl incorrectly treats these as multiple moveto commands.

For example: 'm 11911.875,5278.5265 1.875,43.125 -322.5,-0.9375 0.938,-46.875 z'

is converted to:

Path(array([[11911.875 , 5278.5265],
[11913.75 , 5321.6515],
[11591.25 , 5320.714 ],
[11592.188 , 5273.839 ]]), array([1, 1, 1, 1], dtype=uint8))

When it should be:

Path(array([[11911.875 , 5278.5265],
[11913.75 , 5321.6515],
[11591.25 , 5320.714 ],
[11592.188 , 5273.839 ]]), array([1, 2, 2, 2], dtype=uint8))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.