Remove the jaggedness

Viewed 45

I'm trying to plot a Spirograph™ curve

In [65]: from matplotlib.pyplot import gca, subplots
    ...: from numpy import exp, lcm, linspace, pi
    ...: 
    ...: def prepare_for_sg():
    ...:     fig, ax = subplots()
    ...:     ax.set_aspect(1)
    ...:     ax.axis(False)
    ...:     return fig, ax
    ...: 
    ...: def plot_sg(R, r, d, Φ=0.0, φ=0.0, ax=None):
    ...:     if ax is None : ax = gca()
    ...:     Ω = 2*pi ;  ω = -Ω*R/r
    ...:     n = lcm(r, R)
    ...:     t = linspace(0, n, n*360+1)
    ...:     xy = (R-r)*np.exp(1j*Ω*(t-Φ)) + d*np.exp(1j*ω*(t-φ))
    ...:     line, = ax.plot(xy.real, xy.imag)
    ...:     line.set_antialiased(True)
    ...: 
    ...: prepare_for_sg() ; plot_sg(56, 42, 13)

In [66]: !xmag

and below it's what I get, plus an xmag window zooming on a detail of the curve — there is no excess of antialiasing.

What should I do to remove the jaggedness?


EDIT
Having changed my glasses, I recognize that there is a very little bit of antialiasing (thanks @Thomas).
With respect to Thomas's hint, reducing the density of points, I reduced the number of points by a factor of 12 (from 360 per lobe to 30) but I haven't seen any improvement on the smoothness of the curve (there is the same tiny little bit of antialiasing), on the contrary there is a sensible degradation of the curve rendering as a whole.

What should I do to increase the aggressiveness of antialiasing?

enter image description here

2 Answers

matplotlib renders the graph for your screen, so in the end, this is like a bitmap. There will always be some form aliasing in this case.

I assume you want a graph without aliasing to process it further, maybe printing it with high detail? Then you could plt.savefig("output.pdf"), which would save it as PDF with vector graphics.

try to modify figure size and linewidth

    line, = ax.plot(xy.real, xy.imag,linewidth=5.0) #line width
    fig = plt.gcf()
    fig.set_size_inches(30, 30) #set figure size

example

Related