0

I know Matplotlib is a library of Python. However, I am curious if anyone knows a way to connect MATLAB to Python to use this library of Python in MATLAB?

3
  • 4
    Matplotlib is modeled after MATLAB’s graphics functionality, and honestly it’s not as good. Why would you want to use the inferior clone? Commented Jun 2, 2018 at 5:37
  • 2
    Matplotlib is not a MATLAB clone. It did start off as beeing very closely related to MATLAB, but has evolved a lot since then. Commented Jun 2, 2018 at 10:51
  • @CrisLuengo The plotting part of my algorithm is already implemented in python. I asked because I did not want to convert this chunk of code to MATLAB. Otherwise, I quite agree with you guys. Commented Jun 2, 2018 at 14:11

1 Answer 1

4

The Matplotlib library provides the pyplot module, which contains functions which closely resemble the MATLAB plotting syntax and functionality. If you mainly use these functions, then it should be very easy to port the Matplotlib code to native MATLAB.

If you require some functionality of Matplotlib which is not available in MATLAB, then you can of course use Matplotlib like any other Python library from within MATLAB:

First, verify that the correct Python version is used from within MATLAB, as described in the documentation. Basically, you call

pyversion

and verify that this is the path to the desired version of Python. If it is not, set it to the correct path using, again, the pyversion function.

Then, you can easily use NumPy and Matplotlib to generate some sample data and plot it by calling

x = py.numpy.arange(25);
y = py.numpy.square(x) + py.numpy.random.rand(25)
py.matplotlib.pyplot.plot(x, y)
py.matplotlib.pyplot.title('Sample Data')
py.matplotlib.pyplot.xlabel('x')
py.matplotlib.pyplot.ylabel('y')
py.matplotlib.pyplot.savefig('figure1.png')

which will result in a figure as shown below.

Sample image of Matplotlib generated from MATLAB

Sign up to request clarification or add additional context in comments.

2 Comments

It was a clone, many years ago. But that hasn't been the case in a long time.
@TheBlackCat I didn't mean to understate Matplotlib's functionality and the contributions of its authors. I changed the first paragraph of my answer to make it more clear that Matplotlib is a tool of its own and not a 1:1 clone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.