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?
1 Answer
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.

python. I asked because I did not want to convert this chunk of code to MATLAB. Otherwise, I quite agree with you guys.