[Matplotlib-devel] Internals of smooth plotting in matplotlib

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[Matplotlib-devel] Internals of smooth plotting in matplotlib

priimak
Hi All.

I am writing plotting library in Scala (https://github.com/priimak/splot https://splot-web.github.io/) but I am having difficulty doing smooth plotting when I have a lot of data points. Consider a following use case. Let say along the x-axis I have array of points from 0 to 0.3 with step 0.001 and on y-axis each corresponding point is computed from some function, let say sin(x). Now I want to plot smooth plot using these points. If I just plot corresponding pixels I might have wholes in the image if I zoom in, so that it is not good. If I just poly line (there is such function in java Graphics2D object) based on these points I have another problem. I have to convert each point into onscreen pixel coordinates (I can use floor(..) or rounding to the nearest int when doing it). And that results in jagged step like line that  you can see here


That does not happen in matplotlib when let say using fig.plot(...) function. Output is always smooth which is what I want as well. Is there a particular place in code where I can see how that is done?

--
Dmitri Priimak

_______________________________________________
Matplotlib-devel mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-devel
Reply | Threaded
Open this post in threaded view
|

Re: Internals of smooth plotting in matplotlib

matplotlib - devel mailing list
Now I want to plot smooth plot using these points. If I just plot corresponding pixels I might have wholes in the image if I zoom in,

You really don’t want to do that.

so that it is not good. If I just poly line (there is such function in java Graphics2D object) based on these points I have another problem. I have to convert each point into onscreen pixel coordinates (I can use floor(..)

I would use round(), but still, drawing in pixel coordinates will result in somewhat jagged lines. 

The way around that is to use “antialiased” drawing. I would be shocked if there wasn’t a Java lib for that. 

Most MPL back-ends use the antigrain library (AGG) which is s particularly good renderer, but any antialiasing renderer should be pretty good.

You also mention zooming — make sure you zoom before converting to pixel coords as well.

HTH,

-CHB



or rounding to the nearest int when doing it). And that results in jagged step like line that  you can see here


That does not happen in matplotlib when let say using fig.plot(...) function. Output is always smooth which is what I want as well. Is there a particular place in code where I can see how that is done?

--
Dmitri Priimak
_______________________________________________
Matplotlib-devel mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-devel

_______________________________________________
Matplotlib-devel mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-devel
Reply | Threaded
Open this post in threaded view
|

Re: Internals of smooth plotting in matplotlib

priimak


On Mon, May 6, 2019 at 10:10 AM Chris Barker - NOAA Federal <[hidden email]> wrote:
Now I want to plot smooth plot using these points. If I just plot corresponding pixels I might have wholes in the image if I zoom in,

You really don’t want to do that.

so that it is not good. If I just poly line (there is such function in java Graphics2D object) based on these points I have another problem. I have to convert each point into onscreen pixel coordinates (I can use floor(..)

I would use round(), but still, drawing in pixel coordinates will result in somewhat jagged lines. 

The way around that is to use “antialiased” drawing. I would be shocked if there wasn’t a Java lib for that. 

There is but it does not seem to work very well. Certainly not as good as AGG back end.


Most MPL back-ends use the antigrain library (AGG) which is s particularly good renderer, but any antialiasing renderer should be pretty good.

I see that now. I don't really want to depend on AGG. Perhaps I can re-implement it in Java/Scala.
 

You also mention zooming — make sure you zoom before converting to pixel coords as well.

Yes, I am doing just that.


HTH,

-CHB



or rounding to the nearest int when doing it). And that results in jagged step like line that  you can see here


That does not happen in matplotlib when let say using fig.plot(...) function. Output is always smooth which is what I want as well. Is there a particular place in code where I can see how that is done?

--
Dmitri Priimak
_______________________________________________
Matplotlib-devel mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-devel

_______________________________________________
Matplotlib-devel mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-devel
Reply | Threaded
Open this post in threaded view
|

Re: Internals of smooth plotting in matplotlib

matplotlib - devel mailing list
> There is but it does not seem to work very well. Certainly not as good as AGG back end.

Part of the problem may be that you have to specify coordinates in
integers, forcing a rounding even before you draw.

One kludgy option is to draw at double resolution, and then rescale the image.

> I see that now. I don't really want to depend on AGG. Perhaps I can re-implement it in Java/Scala.

I’d look for s better Java rendering lib...

-CHB
_______________________________________________
Matplotlib-devel mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-devel