python有很多专门的绘图包,如果能在matlab里面调用,可以非常便捷地绘制美观图形,并用于论文。以matplotlib为例,讲一下怎么实现。

注意事项:查看matlab版本,然后安装对应的Python虚拟环境,需要注意的使用matlab调用matplotlib包前一定要安装PyQt5。这个包目前最高支持到3.8。我的配置是matlab 2021b,配置了python3.8环境,pip安装了PyQt5。

初次使用需要配置python环境

pyversion('D:\Anaconda3\envs\py38\python.exe')

将python包导入matlab有两种方式,直接import的形式和import as,第二种形式常见于简写,比如画图可以将matplotlib.pyplot这个包简化为plt,同样的也有办法做到。

第一种,

import py.matplotlib.pyplot.*

py.matplotlib.pyplot.figure('draw')

py.matplotlib.pyplot.plot(x)

py.matplotlib.pyplot.show()

第二种,

plt=py.importlib.import_module("matplotlib.pyplot")

plt.figure()

plt.plot(x,x+y)

plt.show()

调用过程中关键字的传入使用pyargs函数即可

% https://ww2.mathworks.cn/help/matlab/ref/pyargs.html

% https://matplotlib.org/stable/gallery/misc/table_demo.html#sphx-glr-gallery-misc-table-demo-py

%https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.bar.html

plt=py.importlib.import_module("matplotlib.pyplot")

plt.figure('draw2')

kwv=pyargs('width',0.4,'bottom',0,'align','center');%创建关键字

plt.bar(x-0.2,y(1,:),kwv)

plt.show()

保存图像

sfkwv=pyargs('dpi',600,'bbox_inches','tight','pad_inches',0);

plt.savefig('test.png',sfkwv)

 修改一些风格需要使用update函数

mpl=py.importlib.import_module('matplotlib')
%中英文混合标题, 修改默认风格
plotsytle = py.dict(pyargs('axes.unicode_minus','False','font.sans-serif','SimSun','mathtext.fontset','stix','font.size',8,'legend.fontsize',int32(8),'legend.frameon','False'));
update(mpl.rcParams,plotsytle)%'lines.linewidth', 1, 'lines.linestyle', '--',

设置图像尺寸

%设置图像尺寸
fig1=plt.figure('draw2')
WH=[8,6]*0.39370;%厘米转英寸
plt.gcf().set_size_inches(pyargs('w',WH(1),'h',WH(2)))%设置画图尺寸

 完整实例

% 手动配置python版本,第一次运行需要配置,后续可能就不需要了
% pyversion('D:\Anaconda3\envs\py38\python.exe')
clc;clear;close all;
x=1:10;
y=1:10;
mpl=py.importlib.import_module('matplotlib')
%中英文混合标题, 修改默认风格
plotsytle = py.dict(pyargs('axes.unicode_minus','False','font.sans-serif','SimSun','mathtext.fontset','stix','font.size',8,'legend.fontsize',int32(8),'legend.frameon','False'));
update(mpl.rcParams,plotsytle)%'lines.linewidth', 1, 'lines.linestyle', '--',
plt=py.importlib.import_module("matplotlib.pyplot");
%设置图像尺寸
fig1=plt.figure('draw2')
WH=[8,6]*0.39370;%厘米转英寸
plt.gcf().set_size_inches(pyargs('w',WH(1),'h',WH(2)))%设置画图尺寸
plt.plot(x-0.2,y);%,pyargs('label','data1')
kwv=pyargs('width',0.4,'bottom',0,'align','center');%创建关键字,'label','data2'
plt.bar(x+0.2,y,kwv);
plt.xlabel("例子: 能量随时间变化$\mathrm{Change\ in\ energy\ over\ time}$")
plt.ylabel('$\mathrm{y\ }$轴')
legend_font=py.dict(pyargs('family','Times New Roman'));%设置字体
plt.gca().legend({'data1','data2'},pyargs('loc','best','prop',legend_font));
sfkwv=pyargs('dpi',600,'bbox_inches','tight','pad_inches',0);
plt.savefig('test.png',sfkwv)
plt.show();

其他的,在matlab中使用python dict变量 

创建 Python dict 变量

创建一个 dict 变量以传递给 Python 函数。pyargs 函数创建关键字参数。
studentID = py.dict(pyargs('Robert',357,'Mary',229,'Jack',391))
或者,创建一个 MATLAB 结构体并将其转换为 dict 变量。
S = struct('Robert',357,'Mary',229,'Jack',391);
studentID = py.dict(S)

 将dict参数传递给python方法

%Python dict 类有一个 update 方法。要运行以下代码,请创建包含患者和检测结果的 dict 变量。

patient = py.dict(pyargs('name', 'John Doe', ...
'test1', [], ...
'test2', [220.0, 210.0, 205.0], ...
'test3', [180.0, 178.0, 177.5]));
%将患者姓名转换为 MATLAB 字符串。

string(patient{'name'})

%使用 update 方法更新并显示 test1 的结果。

update(patient,py.dict(pyargs('test1',[79.0, 75.0, 73.0])))
P = struct(patient);
disp(['test1 results for '+string(patient{'name'})+": "+num2str(double(P.test1))])

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐