当前位置: 首页 > news >正文

MATLAB | MATLAB中绘图的奇淫技巧合集

一些离大谱的绘图小技巧,部分内容来自https://undocumentedmatlab.com/

更改3D坐标区轴位置

对于hAxes=gca

  • hAxes.XRuler.FirstCrossoverValue
    X轴在Y轴上的位置
  • hAxes.XRuler.SecondCrossoverValue
    X轴在Z轴上的位置
  • hAxes.YRuler.FirstCrossoverValue
    Y轴在X轴上的位置
  • hAxes.YRuler.SecondCrossoverValue
    Y轴在Z轴上的位置
  • hAxes.ZRuler.FirstCrossoverValue
    Z轴在X轴上的位置
  • hAxes.ZRuler.SecondCrossoverValue
    Z轴在Y轴上的位置

一个实例:

N = 49;
X = linspace(-10,10,N);
Z = peaks(N);
mesh(X,X,Z);

hAxes=gca;
hAxes.LineWidth=1.5;
hAxes.XRuler.FirstCrossoverValue  = 0; % X轴在Y轴上的位置 
hAxes.YRuler.FirstCrossoverValue  = 0; % Y轴在X轴上的位置 
hAxes.ZRuler.FirstCrossoverValue  = 0; % Z轴在X轴上的位置 
hAxes.ZRuler.SecondCrossoverValue = 0; % Z轴在Y轴上的位置 

无穷基线

基线不仅可以设置为0或者其他有限数,甚至可以设置为正负无穷:

x=0:.1:8;
y=sin(x)-x;
 
area(x,y,'FaceAlpha',.5,'BaseValue',-inf) 

x=1900:10:2000;
y=[75 91 105 123.5 131 150 179 203 226 249 281.5];
bar(x,y,'BaseValue',inf)

修改坐标区域背景

我们知道可以通过设置

set(gca,'Color',[1,0,0])

类似的形式设置背景颜色,但这只是纯色,那么有啥办法把背景色换成渐变色?

t=0.2:0.01:3*pi;

hold on
plot(t,cos(t)./(1+t),'LineWidth',4)
plot(t,sin(t)./(1+t),'LineWidth',4)
plot(t,cos(t+pi/2)./(1+t+pi/2),'LineWidth',4)
plot(t,cos(t+pi)./(1+t+pi),'LineWidth',4)
legend


ax=gca;pause(1e-16);% Backdrop建立需要一定时间因此pause一下很重要
% 四列分别为四个角的颜色
% 使用4xN大小颜色矩阵
% 四行分别是R,G,B,和透明度
colorData = uint8([255, 150, 200, 100; ... 
                   255, 100,  50, 200; ...
                     0,  50, 100, 150; ...
                   102, 150, 200,  50]);
set(ax.Backdrop.Face, 'ColorBinding','interpolated','ColorData',colorData);

修改背景为图片

t=0.2:0.01:3*pi;

hold on
plot(t,cos(t)./(1+t),'LineWidth',4)
plot(t,sin(t)./(1+t),'LineWidth',4)
plot(t,cos(t+pi/2)./(1+t+pi/2),'LineWidth',4)
plot(t,cos(t+pi)./(1+t+pi),'LineWidth',4)
legend


axis tight; 
img=imread('test.png'); 
h=image(xlim,ylim,flipud(img)); 
uistack(h,'bottom')  

循环颜色和线形

ax=gca;hold on
m=magic(9);

ax.ColorOrder=lines(3);
ax.LineStyleOrder={'-','--o',':s'};

hold on
for i=1:9
  plot(m(i,:),'LineWidth',1.2)
  ax.LineStyleOrderIndex = ax.ColorOrderIndex;
end
legend 

渐变marker

x=1:30;y=sin(x); 
hLine=plot(x,y,'o-','MarkerSize',10,'Color',[0,0,0]);

pause(1e-16)% MarkerHandle建立需要一定时间因此pause一下很重要
hMarkers=hLine.MarkerHandle;
markerNum=size(hMarkers.VertexData,2);

% 这里用的pink配色,可以换成其他colormap
colorData=uint8([(pink(markerNum).*255)';255.*ones(1,markerNum)]);
set(hMarkers,'FaceColorBinding','interpolated','FaceColorData',colorData)

渐变折线图

x=1:.1:10;y=sin(x); 
hLine=plot(x,y,'-','LineWidth',30);

colorNum=length(hLine.XData);

% 这里用的pink配色,可以换成其他colormap
% 最后面透明度用的200可调整
colorData=uint8([(pink(colorNum).*255)';200.*ones(1,colorNum)]);
pause(1e-16)% MarkerHandle建立需要一定时间因此pause一下很重要
set(hLine.Edge,'ColorBinding','interpolated', 'ColorData',colorData)

半透明及渐变图例

t=0.2:0.01:3*pi;

hold on
plot(t,cos(t)./(1+t),'LineWidth',4)
plot(t,sin(t)./(1+t),'LineWidth',4)
plot(t,cos(t+pi/2)./(1+t+pi/2),'LineWidth',4)
plot(t,cos(t+pi)./(1+t+pi),'LineWidth',4)
hLegend=legend();
% 设置图例为半透明
pause(1e-16)
set(hLegend.BoxFace,'ColorType','truecoloralpha','ColorData',uint8(255*[1;1;1;.5])); 

set(gca,'Color',[0,0,.18]);

当然也可以花里胡哨:

t=0.2:0.01:3*pi;
hold on
plot(t,cos(t)./(1+t),'LineWidth',4)
plot(t,sin(t)./(1+t),'LineWidth',4)
plot(t,cos(t+pi/2)./(1+t+pi/2),'LineWidth',4)
plot(t,cos(t+pi)./(1+t+pi),'LineWidth',4)
hLegend=legend();

% 设置图例为渐变色
pause(1e-16)
colorData = uint8([255, 150, 200, 100; ... 
                   255, 100,  50, 200; ...
                     0,  50, 100, 150; ...
                   102, 150, 200,  50]);
set(hLegend.BoxFace,'ColorBinding','interpolated','ColorData',colorData)

特殊marker

众所周知,在版本R2020b版本中,横线及竖线marker以及可以直接使用:

t=0:0.1:3*pi;
hold on
plot(t,cos(t),'-_')
plot(t,t./8,'-|')

而在老版本,这俩marker可以这样调用出来:

t=0:0.1:3*pi;
hold on
hLine1=plot(t,cos(t),'-o');
hLine2=plot(t,t./8,'-o');

pause(1e-8)
set(hLine1.MarkerHandle,'Style','hbar')
set(hLine2.MarkerHandle,'Style','vbar')

等高线图保留小数位数

[X,Y,Z] = peaks;


[c,h]=contourf(X,Y,Z,5,'ShowText','on');
h.LevelList=round(h.LevelList,3);% 保留三位小数
clabel(c,h)

绘制结果:

原始绘图:

根据等高线颜色设置文本颜色

[X,Y,Z] = peaks;
[C,hContour] = contour(X,Y,Z, 'ShowText','on', 'LevelStep',1, 'LineWidth',1.5);


updateContours(hContour);
% 因为标签位置是自动更新的属性,会刷新掉颜色,因此添加listener检测
addlistener(hContour, 'MarkedClean', @(h,e)updateContours(hContour));
function updateContours(hContour)
    drawnow  
    levels = hContour.LevelList;
    labels = hContour.TextPrims; % 获取标签基础对象  
    lines  = hContour.EdgePrims; % 获取边缘基础对象 
    for idx = 1 : numel(labels)
        labelValue = str2double(labels(idx).String);
        lineIdx = find(abs(levels-labelValue)<10*eps, 1);  % 找到对应层级
        labels(idx).ColorData = lines(lineIdx).ColorData;  % 修改标签颜色
        labels(idx).Font.Size = 11;
    end
    drawnow
end


相关文章:

  • 指针笔试题解析(4)
  • [Spring Boot 3] 整合NoSQL与构建RESTful服务
  • 基于非线规划算法的船舶能量调度
  • 二、PL/SQL 编程基础
  • NGINX源码之:ngx_open_cached_file
  • 【路径规划-机器人栅格地图】基于蚁群算法求解大规模栅格地图路径规划及避障附Matlab代码
  • SpringCache的介绍和使用
  • java-php-python-ssm艾灸减肥管理网站计算机毕业设计
  • 力扣每日一题2022-09-23中等题:设计链表
  • 内存数据库简介-内存数据库性能排行
  • 【云原生】Hadoop on k8s 环境部署
  • 不同编码格式(Unicode、多字节字符)vs环境下使用printf、scanf应注意事项
  • 使用Express获取jquery数据 使用模块化 共享自己的数据库
  • Python算法:决策树分类
  • IDEA插件开发-学习
  • “寒冬”下的金三银四跳槽季来了,帮你客观分析一下局面
  • el-input获取焦点 input输入框为空时高亮 el-input值非法时
  • java多线程
  • MySQL Access denied for user 'root'@'localhost' 解决方法
  • mysql 数据库四种事务隔离级别
  • PHP那些事儿
  • Python学习笔记 字符串拼接
  • spring cloud gateway 源码解析(4)跨域问题处理
  • SwizzleMethod 黑魔法
  • Vue--数据传输
  • 简析gRPC client 连接管理
  • 聊聊hikari连接池的leakDetectionThreshold
  • 聊聊springcloud的EurekaClientAutoConfiguration
  • 前端js -- this指向总结。
  • 如何学习JavaEE,项目又该如何做?
  • 数组大概知多少
  • 小程序、APP Store 需要的 SSL 证书是个什么东西?
  • 协程
  • FaaS 的简单实践
  • mysql 慢查询分析工具:pt-query-digest 在mac 上的安装使用 ...
  • ​sqlite3 --- SQLite 数据库 DB-API 2.0 接口模块​
  • #if和#ifdef区别
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (c语言版)滑动窗口 给定一个字符串,只包含字母和数字,按要求找出字符串中的最长(连续)子串的长度
  • (done) ROC曲线 和 AUC值 分别是什么?
  • (PHP)设置修改 Apache 文件根目录 (Document Root)(转帖)
  • (待修改)PyG安装步骤
  • (附源码)springboot码头作业管理系统 毕业设计 341654
  • (附源码)ssm考试题库管理系统 毕业设计 069043
  • (附源码)计算机毕业设计ssm电影分享网站
  • (经验分享)作为一名普通本科计算机专业学生,我大学四年到底走了多少弯路
  • (九)One-Wire总线-DS18B20
  • (三)elasticsearch 源码之启动流程分析
  • (算法二)滑动窗口
  • .bat批处理(十一):替换字符串中包含百分号%的子串
  • .Net Core和.Net Standard直观理解
  • .NET Remoting Basic(10)-创建不同宿主的客户端与服务器端
  • .Net 访问电子邮箱-LumiSoft.Net,好用
  • .net6Api后台+uniapp导出Excel
  • .NET多线程执行函数