0

I like to save my plots in a vector format, e.g. pdf or svg. By default, matplotlib saves plotted curves as points connected by straight lines. In SVG language, using the m <path> command. Is there a way to save paths using Bézier Curves? In SVG, that would be using c commands. I found many suggestions to use cubic interpolation, e.g. with scikit, to make the curve look smooth by increasing the number of points. That's not what I want to do. I have enough points in my plots. I just want them to appear perfectly smooth in a vector graphics viewer, which would be the case if the paths were written with smooth nodes, as shown in the screenshot from Inkscape. The top curve has smooth nodes (I used the "Make selected nodes auto-smooth" button in Inkscape) and the bottom one doesn't.

If that isn't possible directly with matplotlib, is there a program that can smooth all the curves in a PDF or SVG file?

Smooth curve example

SU3
  • 5,064
  • 3
  • 35
  • 66
  • Please take a look at this answer: https://stackoverflow.com/questions/62855310/converting-a-list-of-points-to-an-svg-cubic-piecewise-bezier-curve/62857298#62857298 – enxaneta Sep 04 '20 at 05:34
  • 1
    Technical note: SVG has _four_ Bezier curve instructions, `q` and `c`, for quadratic and cubic curves, with `t` and `s` as continuous smooth versions of those, so you _don't_ want `c`, you almost certainly want `s`. The "easiest" way would be to treat your polygonal points as Catmull-Rom coordinates, and then converting each point segment to cubic Bezier form using the rather trivial conversion formula found at the end of https://pomax.github.io/bezierinfo/#catmullconv – Mike 'Pomax' Kamermans Sep 05 '20 at 16:10
  • 1
    On a more programming note: your plots _aren't curves_, they're just points, so one standard approach for folks who want curves is to just interpolate them using something like [scipy's interpolation functions](https://stackoverflow.com/a/31466013/740553) and then do whatever they need to do with the resultant curve data. – Mike 'Pomax' Kamermans Sep 05 '20 at 16:16
  • So, `s`, unline `c`, makes sure that derivatives stay continuous between connected segments of a curve. Thanks for the tip! But I'm not sure what you mean by "your plots aren't curves". We might have different definitions of the word *curve*. As I see it, they are curves defined by the points' coordinates, possibly derivatives at those coordinates, and the interpolation method (`l` for linear, `c` for cubic, `q` for quadratic, etc.). – SU3 Sep 07 '20 at 04:28
  • Oh, and thanks for the link! It's a very nice summary. I'll use it as a reference if I decide to write my own converter. – SU3 Sep 07 '20 at 04:33

0 Answers0