Zander
Zander

Reputation: 167

Matplotlib in MATLAB?

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?

Upvotes: 0

Views: 9108

Answers (1)

hbaderts
hbaderts

Reputation: 14316

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

Upvotes: 4

Related Questions