DelphiVCL4Python icon indicating copy to clipboard operation
DelphiVCL4Python copied to clipboard

How can I show matplotlib plots seamlessly in the GUI window?

Open zwm88 opened this issue 3 years ago • 5 comments

I'm wondering how I can draw matplotlib plots to a Canvas? I guess I can save plots in a memory image, then draw this image to the canvas in TPaintBox.OnPaint method. But there will be no interaction. And I don't know if I maximize the Window, whether the image will look ugly.

Could anyone provide a matplotlib backend that uses DelphiVCL or DelphiFMX?

zwm88 avatar Apr 09 '22 09:04 zwm88

Hi @zwm88,

I encorage you to start a new discussion in our discussions page ❤️

lmbelo avatar Apr 10 '22 16:04 lmbelo

I guess that would be a feature request, the backend, something similar to https://github.com/beeware/toga-chart Qt and Kivy etc. all have the matplotlib backend. @lmbelo

zwm88 avatar Apr 10 '22 19:04 zwm88

@zwm88 It would be really good. I will investigate it a little more. If you have started something, we can take it to the discussion and put some work.

lmbelo avatar Apr 12 '22 12:04 lmbelo

I made it for now using TImage, but a backend will be good for interactive mode. Here is my code:

        Height = self.Image1.Height
        Width  = self.Image1.Width

        fig, ax = plt.subplots(figsize = (Width / self.PixelsPerInch, Height / self.PixelsPerInch), dpi = self.PixelsPerInch)

        ax.plot(x, y, linewidth=3.0)
        plt.legend(['A', 'B', 'C'], loc='upper left')

        plt.savefig('test.png', format='png')
        self.Image1.Picture.LoadFromFile('test.png')
        plt.close()

Seems that TStream is not supported by the DelphiVCL, and I don't know how to set the Image's picture using an image in memory, so I have to use the intermediate file, which is a bit slow.

zwm88 avatar Apr 13 '22 07:04 zwm88

Yes, this would be a great addon feature. I too got a similar thought few days back and was thinking on ways to do so. As @zwm88 pointed, we shall try to take the plot object and provide it as image by avoiding intermediate step. As Python4Delphi is bi-directional, we can even think of taking of providing the matplotlib as backend for plotting through a component.

Priyatham10 avatar Apr 13 '22 10:04 Priyatham10