Pythonnet: add docs - using matplotlib from C# via pythonnet

Created on 27 Mar 2017  路  4Comments  路  Source: pythonnet/pythonnet

Last time I tried few years ago using matplotlib from pythonnet this was not possible.

These 2 packages use IPC to call matplotlib from C#:

https://github.com/IRC-SPHERE/PythonPlotter
https://github.com/ITGlobal/MatplotlibCS

I tried again today and at least Tk backend is working great with pythonnet!

> #r "C:\Python\Anaconda3_64b\Lib\site-packages\Python.Runtime.dll";
> using Python.Runtime;
> PythonEngine.Initialize();
> dynamic mpl = Py.Import("matplotlib");
> mpl.use("TkAgg")
> dynamic plt = Py.Import("matplotlib.pyplot");
> plt.plot(new List<float> { 1, 2, 3 });
> plt.show()

image

documentation enhancement good first issue

Most helpful comment

I write a demo to show how to use matplotlib via pythonnet at here.

It used the pyscope function, and covered all the functions of PythonInterface.

@JimSEOW You might be interested in this.

All 4 comments

In PyPlot

The ploting is done through buffer and MemoryStream(buffer).
It would be great if we could get this method to work for integrating MatPlotLib in Python for .NET

        public void UpdatePlot()
        {
            using (Py.GIL())
            {
                if (Local.HasKey("fig"))
                {
                    Python.PushLocal(Local);
                    Python.RunString(
                        "fig.set_size_inches(__figwidth__, __figheight__)" + Environment.NewLine +
                        "fig.tight_layout()" + Environment.NewLine +
                        "buf = io.BytesIO()" + Environment.NewLine +
                        "fig.savefig(buf, dpi=__dpi__, format='png')" + Environment.NewLine +
                        "buf_out = np.array(buf.getbuffer(), dtype = np.uint8)"
                    );
                    byte[] buf = Python.Get("buf_out");
                    Python.RunString(
                        "del buf_out\n" +
                        "del buf"
                    );
                    Local = Python.PopLocal();
                    Stream stream = new MemoryStream(buf);
                    Image image = Image.FromStream(stream);
                    PictureBox.Image = image;
                }
            }            
        }

@JimSEOW Currently I don't use MatplotLib in C#, but I was thinking about an implementation of MatplotLib embedding as an example for pyscope, which will cover the functions of PythonInterface and PyPlot you just mentioned.

This code provides one good idea and itself can be greatly optimized. Other suggestions are also welcomed and thank you.

This will be great enhancement for clrmagic in jupyter notebooks:

https://groups.google.com/forum/m/#!topic/jupyter/Wy7LnDF4O_Q

I write a demo to show how to use matplotlib via pythonnet at here.

It used the pyscope function, and covered all the functions of PythonInterface.

@JimSEOW You might be interested in this.

Was this page helpful?
0 / 5 - 0 ratings