如何使用MATLAB绘制ggplot风格图片(散点图及折线图)
写了一个用来修饰MATLAB AXES的函数,目前只支持散点图及折线图,之后可能会再出曲面修饰函数或者柱状图修饰函数之类的一系列修饰器,效果如下:
0使用效果
1参数说明
程序主要有以下几个参数:
- ax | 程序的第一个参数,即要作用的AXES区域,如果设置为[]则自动获取gca作为ax。
- AxesTheme | 坐标区域风格,可设置为:‘gray’/‘economist’/‘wsj’/‘own1’
- ColorOrder | 图形对象颜色序列,可设置为:‘default’/‘none’/‘npg’/‘lancet’/‘starterk’/‘Set1’/‘Set2’/‘Set3’/‘Dark2’/‘own1’
- LegendStyle | 图例样式,可设置为:‘ggplot’/‘own1’
- EdgeStyle | 轮廓样式,可设置为:‘none’/‘gray’/‘white’/‘ori’
2基本使用
假设你编写了如下程序:
t=0:0.1:3*pi;
plot(t,sin(t))
hold on
plot(t,cos(t./2))
plot(t,t)lgd=legend('y=sin(t)','y=cos(t/2)','y=t');
lgd.Location='northwest';
title(lgd,'Func','FontSize',12)
则绘制图像如下:
若在程序最后加上一行:
t=0:0.1:3*pi;
plot(t,sin(t))
hold on
plot(t,cos(t./2))
plot(t,t)lgd=legend('y=sin(t)','y=cos(t/2)','y=t');
lgd.Location='northwest';
title(lgd,'Func','FontSize',12)% 修饰用代码
ggplotAxes2D([]);
则结果如下:
这里啥参数都没设,用的就都是默认值。
若将最后一句改为:
ggplotAxes2D([],'ColorOrder','Set2');
就能使用Set2风格的配色:
3AXES及图例风格
AxesTheme : gray
LegendStyle : ggplot
ggplotAxes2D([],'AxesTheme','gray','LegendStyle','ggplot','ColorOrder','Set2');
我们发现绘图效果不变,说明这俩是默认值
AxesTheme : economist
LegendStyle : ggplot
ggplotAxes2D([],'AxesTheme','economist','LegendStyle','ggplot','ColorOrder','Set2');
注,若通过该按钮保存则可把背景颜色一同保存:
AxesTheme : wsj
LegendStyle : ggplot
ggplotAxes2D([],'AxesTheme','wsj','LegendStyle','ggplot','ColorOrder','Set2');
AxesTheme : own1
LegendStyle : own1
ggplotAxes2D([],'AxesTheme','own1','LegendStyle','own1','ColorOrder','Set2');
4边缘风格
散点图边缘风格
假设编写以下散点图代码:
mu = [2 3];
SIGMA = [1 0; 0 2];
r = mvnrnd(mu,SIGMA,100);
scatter(r(:,1),r(:,2),'filled');
hold on;
mu = [6 7];
SIGMA = [ 1 0; 0 2];
r2 = mvnrnd(mu,SIGMA,100);
scatter(r2(:,1),r2(:,2),'filled')
mu = [8 9];
SIGMA = [ 1 0; 0 1];
r3 = mvnrnd(mu,SIGMA,100);
scatter(r3(:,1),r3(:,2),'filled')
lgd=legend('scatter1','scatter2','scatter3');
lgd.Location='northwest';
运行效果如下:
我们在用修饰器时加入EdgeStyle属性:
EdgeStyle : white
ggplotAxes2D([],'ColorOrder','Set2','EdgeStyle','white');
EdgeStyle : gray
ggplotAxes2D([],'ColorOrder','Set2','EdgeStyle','gray');
EdgeStyle : ori
ori即为边缘使用图形对象原色,但是亮度降低。
ggplotAxes2D([],'ColorOrder','Set2','EdgeStyle','ori');
折线边缘风格
假设我们编写了如下代码:
t=0:0.35:3*pi;
plot(t,sin(t),'Marker','d')
hold on
plot(t,cos(t./2),'Marker','o')
plot(t,t,'Marker','^')lgd=legend('y=sin(t)','y=cos(t/2)','y=t');
lgd.Location='northwest';
title(lgd,'Func','FontSize',12)
我们也可对其进行一系列修饰,例如:
ggplotAxes2D([],'AxesTheme','wsj','ColorOrder','Set2','EdgeStyle','gray');
ggplotAxes2D([],'AxesTheme','own1','ColorOrder','own1','EdgeStyle','gray','LegendStyle','own1');
5完整代码
function ax=ggplotAxes2D(varargin)
%
% @author:slandarer
%
% 参数说明:
% -----------------------------------------------------
% AxesTheme | 坐标区域风格 | 'gray'/'economist'/'wsj'/'own1'
% ColorOrder | 图形对象颜色序列 | 'default'/'none'/'npg'/'lancet'/'starterk'
% 'Set1'/'Set2'/'Set3'/'Dark2'/'own1'
% LegendStyle | 图例样式 | 'ggplot'/'own1'
% EdgeStyle | 轮廓样式 | 'none'/'gray'/'white'/'ori'% ax.Legend.UserData.NewBkg 图例新背景
% ax.Legend.UserData.NewTitle 图例新标题
% ax.UserData.NewYTick(i) Y轴新标签% 获取要处理的坐标区域=====================================================
if strcmp(get(varargin{1},'type'),'axes' )ax=varargin{1};
elseax=gca;
end
hold(ax,'on')% default==================================================================
theme.AxesTheme='gray';
theme.ColorOrder='default';
theme.LegendStyle='ggplot';
theme.EdgeStyle='none';%从可变长度变量中提取有用信息==============================================
for i=1:length(varargin)tempVar=varargin{i};if strcmp(tempVar,'AxesTheme')||strcmp(tempVar,'axesTheme')||strcmp(tempVar,'axestheme')theme.AxesTheme=varargin{i+1};endif strcmp(tempVar,'ColorOrder')||strcmp(tempVar,'colorOrder')||strcmp(tempVar,'colororder')theme.ColorOrder=varargin{i+1};endif strcmp(tempVar,'LegendStyle')||strcmp(tempVar,'legendStyle')||strcmp(tempVar,'legendstyle')theme.LegendStyle=varargin{i+1};endif strcmp(tempVar,'EdgeStyle')||strcmp(tempVar,'edgeStyle')||strcmp(tempVar,'edgestyle')theme.EdgeStyle=varargin{i+1};end
end% 配色方案
switch theme.ColorOrdercase 'none'case 'default'ax.ColorOrder=[0.9900 0.4500 0.45000.8500 0.5600 00.6400 0.6500 00.2200 0.7100 00 0.7500 0.49000 0.7500 0.77000 0.6900 0.96000.5800 0.5600 1.00000.9100 0.4200 0.95001.0000 0.3800 0.7400]; case 'npg'ax.ColorOrder=[0.9000 0.2900 0.21000.3000 0.7300 0.84000 0.6300 0.53000.2400 0.3300 0.53000.9500 0.6100 0.50000.5200 0.5700 0.71000.5700 0.8200 0.76000.8600 0 00.4900 0.3800 0.28000.6900 0.6100 0.5200];case 'lancet'ax.ColorOrder=[ 0 0.2700 0.55000.9300 0 00.2600 0.7100 0.25000 0.6000 0.71000.5700 0.3700 0.62000.9900 0.6900 0.57000.6800 0 0.16000.6800 0.7100 0.71000.1100 0.1000 0.1000];case 'starterk'ax.ColorOrder=[0.8000 0.0500 00.3600 0.5300 0.85000.5200 0.7400 01.0000 0.8000 00.4900 0.5300 0.56000 0.7100 0.89000 0.6900 0.4000];case 'Set1'ax.ColorOrder=[0.8900 0.1000 0.11000.2200 0.4900 0.72000.3000 0.6900 0.29000.6000 0.3100 0.64001.0000 0.5000 01.0000 1.0000 0.20000.6500 0.3400 0.16000.9700 0.5100 0.75000.6000 0.6000 0.6000];case 'Set2'ax.ColorOrder=[0.4000 0.7600 0.65000.9900 0.5500 0.38000.5500 0.6300 0.80000.9100 0.5400 0.76000.6500 0.8500 0.33001.0000 0.8500 0.18000.9000 0.7700 0.58000.7000 0.7000 0.7000];case 'Set3'ax.ColorOrder=[0.5500 0.8300 0.78001.0000 1.0000 0.70000.7500 0.7300 0.85000.9800 0.5000 0.45000.5000 0.6900 0.83000.9900 0.7100 0.38000.7000 0.8700 0.41000.9900 0.8000 0.90000.8500 0.8500 0.85000.7400 0.5000 0.74000.8000 0.9200 0.77000.8300 0.8300 0.8300];case 'Dark2'ax.ColorOrder=[0.1100 0.6200 0.47000.8500 0.3700 0.01000.4600 0.4400 0.70000.9100 0.1600 0.54000.4000 0.6500 0.12000.9000 0.6700 0.01000.6500 0.4600 0.11000.4000 0.4000 0.4000];case 'own1'ax.ColorOrder=[0.8500 0.7100 0.80000.3700 0.4400 0.66000.7500 0.6900 0.83000.3700 0.2200 0.52000.8400 0.2500 0.55000.7200 0.5200 0.52000.6100 0.3800 0.60000.0400 0.1400 0.28001.0000 0.5800 0.35000.9500 0.8900 0.7500];
end% 部分plot scatter修饰
if false
childrenNum=length(ax.Children);
for i=1:childrenNumswitch theme.EdgeStylecase 'none'EdgeColor=[];case 'gray'EdgeColor=[0.3 0.3 0.3];case 'white'EdgeColor=[0.96 0.96 0.96];case 'ori'EdgeColor=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:).*0.5;endswitch get(ax.Children(i),'type')case 'line'ax.Children(i).LineWidth=1.8;if ~isempty(EdgeColor)ax.Children(i).LineWidth=1.5;ax.Children(i).MarkerEdgeColor=EdgeColor;ax.Children(i).MarkerSize=8;ax.Children(i).MarkerFaceColor=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:);endcase 'scatter'if ~isempty(EdgeColor)ax.Children(i).MarkerFaceColor=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:);ax.Children(i).LineWidth=1.5;ax.Children(i).MarkerEdgeColor=EdgeColor;ax.Children(i).SizeData=60;endend
end
endif true
childrenNum=length(ax.Children);
lineSet=[];
scatterSet=[];
n=1;
for i=childrenNum:-1:1if strcmp(get(ax.Children(i),'type'),'scatter')scatterSet{n}=ax.Children(i);n=n+1;end
end
n=1;
for i=childrenNum:-1:1if strcmp(get(ax.Children(i),'type'),'line')lineSet{n}=ax.Children(i);n=n+1;end
end
for i=1:length(scatterSet)switch theme.EdgeStylecase 'none'EdgeColor=[];case 'gray'EdgeColor=[0.3 0.3 0.3];case 'white'EdgeColor=[0.96 0.96 0.96];case 'ori'EdgeColor=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:).*0.5;endscatterSet{i}.MarkerEdgeColor=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:);if ~isempty(EdgeColor)scatterSet{i}.LineWidth=1.5;scatterSet{i}.MarkerEdgeColor=EdgeColor;scatterSet{i}.SizeData=60;scatterSet{i}.MarkerFaceColor=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:);end
end
for i=1:length(lineSet)switch theme.EdgeStylecase 'none'EdgeColor=[];case 'gray'EdgeColor=[0.3 0.3 0.3];case 'white'EdgeColor=[0.96 0.96 0.96];case 'ori'EdgeColor=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:).*0.5;endlineSet{i}.LineWidth=1.8;lineSet{i}.Color=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:);if ~isempty(EdgeColor)lineSet{i}.LineWidth=1.5;lineSet{i}.MarkerEdgeColor=EdgeColor;lineSet{i}.MarkerSize=8;lineSet{i}.MarkerFaceColor=ax.ColorOrder(mod(i-1,size(ax.ColorOrder,1))+1,:);end
end
end% legend 风格化
if ~isempty(ax.Legend)
switch theme.LegendStylecase 'ggplot'ax.Legend.FontSize=11;ax.Legend.Title.FontSize=14;ax.Legend.AutoUpdate='off';if ~isempty(regexpi(ax.Legend.Location,'out'))ax.Legend.Box='off';lgdPos=ax.Legend.Position;xyMin=[(lgdPos(1)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),...(lgdPos(2)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)];xyMax=[(lgdPos(1)+lgdPos(3)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),...(lgdPos(2)+lgdPos(4)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)];ax.Legend.Title.Visible='off';xyMin(1),xyMax(2)ax.Legend.UserData.NewTitle=text(ax,xyMin(1),xyMax(2),[' ',ax.Legend.Title.String],...'FontSize',14,'VerticalAlignment','top','FontWeight','bold');elseax.Legend.Box='off';lgdPos=ax.Legend.Position;xyMin=[(lgdPos(1)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),...(lgdPos(2)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)];xyMax=[(lgdPos(1)+lgdPos(3)-ax.Position(1))/ax.Position(3)*(ax.XLim(2)-ax.XLim(1))+ax.XLim(1),...(lgdPos(2)+lgdPos(4)-ax.Position(2))/ax.Position(4)*(ax.YLim(2)-ax.YLim(1))+ax.YLim(1)];xDiff=(xyMax(1)-xyMin(1));yDiff=(xyMax(2)-xyMin(2));ax.Legend.UserData.NewBkg=rectangle(ax,'Position',[xyMin,xDiff,yDiff],'Curvature',0.2,...'LineWidth',1.2,'EdgeColor',[0.39 0.41 0.39],'FaceColor',[1 1 1 .2]);%ax.Legend.Title.FontSize=14;ax.Legend.Title.Visible='off';ax.Legend.UserData.NewTitle=text(ax,xyMin(1),xyMax(2),[' ',ax.Legend.Title.String],...'FontSize',14,'VerticalAlignment','top','FontWeight','bold');endcase 'own1'ax.Legend.Color=[0.9412 0.9412 0.9412];ax.Legend.LineWidth=0.8;ax.Legend.FontSize=11;
end
end% axes风格化
switch theme.AxesThemecase 'gray'ax.Parent.Color=[1 1 1];ax.Color=[0.9,0.9,0.9];ax.Box='off';grid(ax,'on');ax.TickDir='out';ax.GridColor=[1 1 1];ax.GridAlpha=1;ax.LineWidth=1.2;ax.XColor=[0.33,0.33,0.33];ax.YColor=[0.33,0.33,0.33];ax.TickLength=[0.015 0.025];plot(ax,[ax.XLim(2),ax.XLim(1),ax.XLim(1),ax.XLim(2),ax.XLim(2)],...[ax.YLim(2),ax.YLim(2),ax.YLim(1),ax.YLim(1),ax.YLim(2)],...'Color',[1 1 1],'LineWidth',2)case 'economist'ax.Parent.Color=[0.8400 0.8900 0.9200];ax.Color=[0.8400 0.8900 0.9200];ax.Parent.InvertHardcopy='off';ax.Box='off';ax.YGrid='on';ax.GridColor=[1 1 1];ax.GridAlpha=1;ax.LineWidth=1.2;ax.XColor=[0.33,0.33,0.33];ax.YColor='none';ax.TickLength=[0.015 0.025];for i=1:length(ax.YTick)ax.UserData.NewYTick(i)=...text(ax,ax.XLim(1)-ax.TickLength(1)/(ax.Position(3))*(ax.XLim(2)-ax.XLim(1)),...ax.YTick(i),ax.YTickLabel{i},'HorizontalAlignment','right','Color',[0.33,0.33,0.33]);endcase 'wsj'ax.Parent.Color=[0.9700 0.9500 0.8900];ax.Color=[0.9700 0.9500 0.8900];ax.Parent.InvertHardcopy='off';ax.Box='off';ax.YGrid='on';ax.GridAlpha=1;ax.LineWidth=0.8;ax.YColor='none';ax.GridLineStyle=':';ax.TickLength=[0.015 0.025];for i=1:length(ax.YTick)ax.UserData.NewYTick(i)=...text(ax,ax.XLim(1)-ax.TickLength(1)/(ax.Position(3))*(ax.XLim(2)-ax.XLim(1)),...ax.YTick(i),ax.YTickLabel{i},'HorizontalAlignment','right','Color',[0.33,0.33,0.33]);endcase 'own1'grid(ax,'on');ax.GridLineStyle='--';ax.LineWidth = 1;
end
end
6注
- 如果对AXES形状不满意,请调整到合适的形状后再命令行调用函数
- scatter和plot可以同时修饰例如:
如何使用MATLAB绘制ggplot风格图片(散点图及折线图)相关推荐
- Matplotlib【学习大礼包】数据可视化基础 掌获绘图基础语法与常用参数、设置pyplot的动态rc参数、 绘制散点图、折线图、直方图、饼图
文章目录 如何查看完整版!!(代码+图片) 第3章 Matplotlib 数据可视化基础 3.1 掌获绘图基础语法与常用参数 3.1.1 掌获pyplot基础语法 1.创建画布与创建子图 2.添加画布 ...
- python画散点图程序-Python散点图与折线图绘制过程解析
这篇文章主要介绍了Python散点图与折线图绘制过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 在数据分析的过程中,经常需要将数据可视化,目 ...
- Python之matplotlib:利用matplotlib绘制八象空间三维图案例(知识点包括散点图、折线图、标注文字、图例、三维坐标)之详细攻略
Python之matplotlib:利用matplotlib绘制八象空间三维图案例(知识点包括散点图.折线图.标注文字.图例.三维坐标)之详细攻略 目录
- 用matlab对称振子E面方向图,利用matlab绘制电基本振子e面方向图和空间立体方向图.doc...
利用matlab绘制电基本振子e面方向图和空间立体方向图.doc 微波技术与天线实验报告学院:信电学院班级:通信2班姓名:学号:2014年4月29日1利用matlab绘制电基本振子E面方向图和空间立体 ...
- MATLAB编程(4)——MATLAB绘制二维高斯函数的三维图
本篇博文记录使用MATLAB绘制二维高斯函数的三维图. 用到的MATLAB函数--mesh()(绘制三维线框图)和surf()(绘制三维表面图). MATLAB命令窗口输入>> doc 函 ...
- CorePlot_1.5.1 绘制散点图(折线图、曲线图、直方图)
前言 CorePlot提供了散点图(CPTScatterPlot)的绘制,包括:折线图.曲线图.直方图.如下图所示: CorePlot框架自身提供了线条样式.文本样式及填充色的设置.CPTMu ...
- 3.Matplotlib数据可视化基础(上)(pyplot、rc参数、散点图、折线图)
笔记说明:本文是我的学习笔记,大部分内容整理自 黄红梅,张良均等.Python数据分析与应用[M].北京:人民邮电出版社,2018:52-77. 还有部分片断知识来自网络搜索补充. 可视化这块的内容我 ...
- Matplotlib基础02:散点图、折线图与柱状图
Matplotlib基础02:散点图.折线图与柱状图 散点图 散点图Scatter: 是数据点在直角坐标系中的分布图,可以让我们对数据分析规律,得到数据变化的趋势,进而进行数据分组. API:scat ...
- R绘图 vs Python绘图(散点图、折线图、直方图、条形图、箱线图、饼图、热力图、蜘蛛图)
写在前面:为啥不用excel绘制这些图,用PoweBI,帆软BI等可视化软件来绘图,不是更方便吗?的确,这些工具都很方便,但同时,它们显得很呆,不够灵活,更为致命的是,它们绘制出的图形,分辨率不够,用 ...
- POI处理Excel,条形图,散点图,折线图
POI处理Excel,条形图,散点图,折线图 一.引入Maven依赖 二.条形图实现 2.1实现效果展示 2.2 代码实现如下: 三.散点图实现-----对应Excel中的XY散点图 3.1初始数据准 ...
最新文章
- 从起源到未来,一文看懂70年的人工智能简史
- Testing Windows Live Writer 2
- opencv+python读写视频流
- 在ASP.NET Core微服务架构下使用数据库切分和扩展
- Linux进程全解3——进程概念、进程ID、多进程调度原理
- 项目管理学习总结(2)——需求收集和管理
- 3-cd 命令总结
- uni-app image组件当显示不出图片时显示默认图片
- 深鸿会深大小组学习笔记:第一周,从零开发鸿蒙小游戏2048app(上)
- GSCC呼叫中心系统
- Mac上制作Mac os10.12.6启动盘的多次尝试
- 【无人机】【2017.12】基于AGENT的防御群建模分析
- 双端影视APP后台搭建及前端安装编译大概流程
- C++Primer笔记-A003-decltype使用
- 追风筝的人 第九章
- 实时人体姿态估计!Efficient-HRNet:更快!更强!!
- Stay Hungry, Stay Foolish — 求知若饥,虚心若愚!
- 服务器维护 志愿填报时间顺延,因系统维护耽误考生填报,西藏高考志愿填报截止时间顺延两日...
- EditText设置只允许输入文字、拼音、数字
- 飞书机器人小助手@特定人