Skip to content

add docs - using matplotlib from C# via pythonnet #442

Closed

Description

@den-run-ai

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

Activity

  1. JimSEOW commented on Apr 5, 2017

    @JimSEOW

    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;
                    }
                }            
            }
    
  2. yagweb commented on Apr 6, 2017

    @yagweb
    Contributor

    @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.

  3. den-run-ai commented on Apr 6, 2017

    @den-run-ai
    ContributorAuthor

    This will be great enhancement for clrmagic in jupyter notebooks:

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

  4. self-assigned this
    on May 16, 2017
  5. yagweb commented on Jun 3, 2017

    @yagweb
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions