MATLAB | 爱心图例与精致半透明圆角图例

这篇具有很好参考价值的文章主要介绍了MATLAB | 爱心图例与精致半透明圆角图例。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

本文中全部示意图均为本人自制,传播时请注明出处。

写了点小成品函数,比如如何绘制饼图时把图例上的图标变成心形:
MATLAB | 爱心图例与精致半透明圆角图例,matlab,javascript,前端

比如如何自制半透明圆角图例:

MATLAB | 爱心图例与精致半透明圆角图例,matlab,javascript,前端

依旧先讲原理再给出这俩代码哈:


1 原理讲解-图形对象

封闭四边形对象

在之前文章我们已经讲到了legend组件的隐藏属性:

MATLAB | 爱心图例与精致半透明圆角图例,matlab,javascript,前端

这个外框以及大部分情况下图例都是使用LineLoop以及Quadrilateral进行绘制:

这两种基础图形对象都是四个点为一组来绘制图形的,大概原理如下:

VertexData是顶点的坐标,是一个3xn大小的数组,第一行代表x坐标,第2、3行代表y、z坐标。范围为0-1。

每四个点为一组,例如如果有八个顶点,就会四个四个顶点为一组组成图形(1,2,3,4一组,5,6,7,8一组):

MATLAB | 爱心图例与精致半透明圆角图例,matlab,javascript,前端

当然可以将点按顺序排放,或者设置VertexIndices就是顶点顺序来得到想要的四边形形状:

MATLAB | 爱心图例与精致半透明圆角图例,matlab,javascript,前端


其他对象

对比于只能四个点为一组的对象,我们还要介绍更加灵活的对象,首先是LineStrip,这个图形对象会两点一组绘制直线:

MATLAB | 爱心图例与精致半透明圆角图例,matlab,javascript,前端

TriangleStrip就是三个点一组绘制填充三角形的对象,例如可以通过多个三角形填充复杂多边形:

MATLAB | 爱心图例与精致半透明圆角图例,matlab,javascript,前端


2 原理讲解-创建与替换

讲一下怎么构造基础的图形对象,这些对象都是未公开的很底层的函数构造的,例如LineStrip,并不是通过:

  • LineStrip()

来进行构建的,而是通过:

  • matlab.graphics.primitive.world.LineStrip()

构建完对象后,必须设置以下属性才能让隐藏对象显示出来:

  • Layer
  • ColorBinding
  • ColorData
  • VertexData
  • PickableParts

这些属性的设置可以参考原本的图例构成对象,在此不做详述,也可参考本人写的代码。

之后将新构造的对象父类设置成原本组件的Group父类,再将原本组件隐藏即可:

newBoxEdgeHdl.Parent=oriBoxEdgeHdl.Parent;
oriBoxEdgeHdl.Visible='off';

以上就是进行组件替换的全部流程,写了两段示例代码:


3 示例代码

半透明图例

function SPrettyLegend(lgd)
% Semitransparent rounded rectangle legend
% Copyright (c) 2023, Zhaoxu Liu / slandarer
% -------------------------------------------------------------------------
% Zhaoxu Liu / slandarer (2023). pretty legend 
% (https://www.mathworks.com/matlabcentral/fileexchange/132128-pretty-legend), 
% MATLAB Central File Exchange. 检索来源 2023/7/9.
% =========================================================================
if nargin<1
    ax = gca;
    lgd = get(ax,'Legend');
end
pause(1e-6)
Ratio = .1;
t1 = linspace(0,pi/2,4); t1 = t1([1,2,2,3,3,4]);
t2 = linspace(pi/2,pi,4); t2 = t2([1,2,2,3,3,4]);
t3 = linspace(pi,3*pi/2,4); t3 = t3([1,2,2,3,3,4]);
t4 = linspace(3*pi/2,2*pi,4); t4 = t4([1,2,2,3,3,4]);
XX = [1,1,1-Ratio+cos(t1).*Ratio,1-Ratio,Ratio,Ratio+cos(t2).*Ratio,...
    0,0,Ratio+cos(t3).*Ratio,Ratio,1-Ratio,1-Ratio+cos(t4).*Ratio];
YY = [Ratio,1-Ratio,1-Ratio+sin(t1).*Ratio,1,1,1-Ratio+sin(t2).*Ratio,...
    1-Ratio,Ratio,Ratio+sin(t3).*Ratio,0,0,Ratio+sin(t4).*Ratio];
% 圆角边框(border-radius)
oriBoxEdgeHdl = lgd.BoxEdge;
newBoxEdgeHdl = matlab.graphics.primitive.world.LineStrip();
newBoxEdgeHdl.AlignVertexCenters = 'off';
newBoxEdgeHdl.Layer = 'front';
newBoxEdgeHdl.ColorBinding = 'object';
newBoxEdgeHdl.LineWidth = 1;
newBoxEdgeHdl.LineJoin = 'miter';
newBoxEdgeHdl.WideLineRenderingHint = 'software';
newBoxEdgeHdl.ColorData = uint8([38;38;38;0]);
newBoxEdgeHdl.VertexData = single([XX;YY;XX.*0]);
newBoxEdgeHdl.Parent=oriBoxEdgeHdl.Parent;
oriBoxEdgeHdl.Visible='off';
% 半透明圆角背景(Semitransparent rounded background)
oriBoxFaceHdl = lgd.BoxFace;
newBoxFaceHdl = matlab.graphics.primitive.world.TriangleStrip();
Ind = [1:(length(XX)-1);ones(1,length(XX)-1).*(length(XX)+1);2:length(XX)];
Ind = Ind(:).';
newBoxFaceHdl.PickableParts = 'all';
newBoxFaceHdl.Layer = 'back';
newBoxFaceHdl.ColorBinding = 'object';
newBoxFaceHdl.ColorType = 'truecoloralpha';
newBoxFaceHdl.ColorData = uint8(255*[1;1;1;.6]);
newBoxFaceHdl.VertexData = single([XX,.5;YY,.5;XX.*0,0]);
newBoxFaceHdl.VertexIndices = uint32(Ind);
newBoxFaceHdl.Parent = oriBoxFaceHdl.Parent;
oriBoxFaceHdl.Visible = 'off';
end

使用示例

clc; clear; close all
rng(12)
% 生成随机点(Generate random points)
mu = [2 3; 6 7; 8 9];
S  = cat(3,[1 0; 0 2],[1 0; 0 2],[1 0; 0 1]);
r1 = abs(mvnrnd(mu(1,:),S(:,:,1),100));
r2 = abs(mvnrnd(mu(2,:),S(:,:,2),100));
r3 = abs(mvnrnd(mu(3,:),S(:,:,3),100));
% 绘制散点图(Draw scatter chart)
hold on
propCell = {'LineWidth',1.2,'MarkerEdgeColor',[.3,.3,.3],'SizeData',60};
scatter(r1(:,1),r1(:,2),'filled','CData',[0.40 0.76 0.60],propCell{:});
scatter(r2(:,1),r2(:,2),'filled','CData',[0.99 0.55 0.38],propCell{:});
scatter(r3(:,1),r3(:,2),'filled','CData',[0.55 0.63 0.80],propCell{:});
% 增添图例(Draw legend)
lgd = legend('scatter1','scatter2','scatter3');
lgd.Location = 'northwest';
lgd.FontSize = 14;
% 坐标区域基础修饰(Axes basic decoration)
ax=gca; grid on
ax.FontName   = 'Cambria';
ax.Color      = [0.9,0.9,0.9];
ax.Box        = 'off';
ax.TickDir    = 'out';
ax.GridColor  = [1 1 1];
ax.GridAlpha  = 1;
ax.LineWidth  = 1;
ax.XColor     = [0.2,0.2,0.2];
ax.YColor     = [0.2,0.2,0.2];
ax.TickLength = [0.015 0.025];
% 隐藏轴线(Hide XY-Ruler)
pause(1e-6)
ax.XRuler.Axle.LineStyle = 'none';
ax.YRuler.Axle.LineStyle = 'none';

SPrettyLegend(lgd)

MATLAB | 爱心图例与精致半透明圆角图例,matlab,javascript,前端


心形图例(饼图专属)

function pie2HeartLegend(lgd)
% Heart shaped legend for pie chart
% Copyright (c) 2023, Zhaoxu Liu / slandarer
% =========================================================================
if nargin<1
    ax = gca;
    lgd = get(ax,'Legend');
end
pause(1e-6)
% 心形曲线(Heart curve)
x = -1:1/100:1;
y1 = 0.6 * abs(x) .^ 0.5 + ((1 - x .^ 2) / 2) .^ 0.5;
y2 = 0.6 * abs(x) .^ 0.5 - ((1 - x .^ 2) / 2) .^ 0.5;
XX = [x, flip(x),x(1)]./3.4+.5;
YY = ([y1, y2,y1(1)]-.2)./2+.5;
Ind = [1:(length(XX)-1);2:length(XX)];
Ind = Ind(:).';
% 获取图例图标(Get Legend Icon)
lgdEntryChild = lgd.EntryContainer.NodeChildren;
iconSet = arrayfun(@(lgdEntryChild)lgdEntryChild.Icon.Transform.Children.Children,lgdEntryChild,UniformOutput=false);
% 基础边框句柄(Base Border Handle)
newEdgeHdl = matlab.graphics.primitive.world.LineStrip();
newEdgeHdl.AlignVertexCenters = 'off';
newEdgeHdl.Layer = 'front';
newEdgeHdl.ColorBinding = 'object';
newEdgeHdl.LineWidth = .8;
newEdgeHdl.LineJoin = 'miter';
newEdgeHdl.WideLineRenderingHint = 'software';
newEdgeHdl.ColorData = uint8([38;38;38;0]);
newEdgeHdl.VertexData = single([XX;YY;XX.*0]);
newEdgeHdl.VertexIndices = uint32(Ind);
% 基础多边形面句柄(Base Patch Handle)
newFaceHdl = matlab.graphics.primitive.world.TriangleStrip();
Ind = [1:(length(XX)-1);ones(1,length(XX)-1).*(length(XX)+1);2:length(XX)];
Ind = Ind(:).';
newFaceHdl.PickableParts = 'all';
newFaceHdl.Layer = 'middle';
newFaceHdl.ColorBinding = 'object';
newFaceHdl.ColorType = 'truecoloralpha';
newFaceHdl.ColorData = uint8(255*[1;1;1;.6]);
newFaceHdl.VertexData = single([XX,.5;YY,.5;XX.*0,0]);
newFaceHdl.VertexIndices = uint32(Ind);
% 替换图例图标(Replace Legend Icon)
for i = 1:length(iconSet)
    oriEdgeHdl = iconSet{i}(1);
    tNewEdgeHdl = copy(newEdgeHdl);
    tNewEdgeHdl.ColorData = oriEdgeHdl.ColorData;
    tNewEdgeHdl.Parent = oriEdgeHdl.Parent;
    oriEdgeHdl.Visible = 'off';

    oriFaceHdl = iconSet{i}(2);
    tNewFaceHdl = copy(newFaceHdl);
    tNewFaceHdl.ColorData = oriFaceHdl.ColorData;
    tNewFaceHdl.Parent = oriFaceHdl.Parent;
    oriFaceHdl.Visible = 'off';
end
end

使用示例

clc; clear; close all
% 生成随机点(Generate random points)
X = [1 3 0.5 2.5 2];
pieHdl = pie(X);

% 修饰饼状图(Decorate pie chart)
colorList=[0.4941    0.5490    0.4118
    0.9059    0.6510    0.3333
    0.8980    0.6157    0.4980
    0.8902    0.5137    0.4667
    0.4275    0.2824    0.2784];
for i = 1:2:length(pieHdl)
    pieHdl(i).FaceColor=colorList((i+1)/2,:);
    pieHdl(i).EdgeColor=colorList((i+1)/2,:);
    pieHdl(i).LineWidth=1;
    pieHdl(i).FaceAlpha=.6;
end
for i = 2:2:length(pieHdl)
    pieHdl(i).FontSize=13;
    pieHdl(i).FontName='Times New Roman';
end
lgd=legend('FontSize',13,'FontName','Times New Roman','TextColor',[1,1,1].*.3);

pie2HeartLegend(lgd)

MATLAB | 爱心图例与精致半透明圆角图例,matlab,javascript,前端


以上已是本文做出的全部探索,本人应该是全网第一位给出修改图例可行方法的人,通过上述方法,阴影柱状图图例等特殊图例的绘制也变成了可能,期待大家的二次创作哈文章来源地址https://www.toymoban.com/news/detail-540152.html

到了这里,关于MATLAB | 爱心图例与精致半透明圆角图例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Matlab中图例的位置(图例放在图的上方、下方、左方、右方、图外面)等

    默认的位置在NorthEast 变为North 设置 位置 North inside plot box near top South inside bottom East inside right West inside left NorthEast inside top right (default for 2-D plots) NorthWest inside top left SouthEast inside bottom right SouthWest’ inside bottom left NorthOutside outside plot box near top SouthOutside outside bottom EastOutside ou

    2024年02月12日
    浏览(35)
  • Matlab设置figure中标题/图例英文不同字体

    标题中既包含英文又包含中文,如果设置字体为Times New Roman,中文就会不显示,如果设置成宋体,英文字母也会不好看 修改方法为:分别在英文和中文处加入fontname{Times new roman}、fontname{宋体} 结果

    2024年02月03日
    浏览(28)
  • Matlab读取csv绘制多条曲线,多颜色及图例设置

    数据示例如下,其中A列为x轴,B、C、D为对应y值,并分别绘制曲线  示例代码如下  绘制曲线结果如下  其中,plot(x,C,\\\'Color\\\',[1 0.6 0.07],\\\'LineWidth\\\', 1)可根据各颜色的RGB值进行设置,将其值/255进行归一化输入即可。 如本例中C曲线使用颜色为“镉黄”,RGB值为[255 153 18],对其进行归

    2024年02月11日
    浏览(40)
  • Matlab中画柱状图详细教程bar函数使用方法(二维附matlab代码)柱状图创建/位置/颜色/图例

    bar(y) bar(x,y) bar(___,width) bar(___,style) bar(___,color) bar(___,Name,Value) bar(ax,___) b = bar(___) bar(y) 创建一个柱状图/条形图,y 中的每个元素对应一个柱状/条形。如果 y 是矩阵,则 bar 根据 y 中的行对柱状/条形分组。 bar(x,y) 在 x 指定的位置绘制柱状/条形。 bar(___,width) 设置柱状/条形的相对

    2023年04月24日
    浏览(28)
  • Matlab中爱心的四种画法(附代码)

     什么Σ(っ°Д°;)っ?居然可以 用matlab画出漂亮的爱心图案 ? 方法一~ ~(~▽~~)成品图如下~  如果要等比例改变爱心的大小,可以指定一个scale: 这样一来,爱心就等比例地变为原来的2倍大啦~(面积为4倍) 下面是 第二种画法 ,与第一种大同小异,但画出来的爱心略有不同

    2023年04月25日
    浏览(35)
  • 【CSS】透明背景的圆角渐变边框实现方案

    css的渐变边框可以用下面方式实现 css的圆角边框可以用下面方式实现 那想要实现一个圆角的渐变边框呢,可能会以为,两个都用上不就可以了,但事实是,这两个属性并不兼容,所以要实现一个圆角的渐变边框,就得需要曲线救国的方法了 最终效果图    

    2024年02月13日
    浏览(52)
  • 将Matlab图窗中的可视化保存为背景透明的矢量图

    将matlab绘制的结果复制为矢量图时,去除背景的操作如下: 先打开/绘制图形窗口(不要关闭) 在命令行终端输入 axis off 关闭坐标系 继续在命令行终端分别输入: ax = gca; copygraphics(ax,\\\'ContentType\\\',\\\'vector\\\',\\\'BackgroundColor\\\',\\\'none\\\'); 此时,背景透明的矢量图就保存在系统 剪贴板 上了,详

    2024年01月21日
    浏览(32)
  • matlab中画有重影的机器人运动过程【给另一个机器人设置透明度】

    1、前言如题 2、参考连接如下 How to plot two moving robot in the same figure and change one of them transparency? - MATLAB Answers - MATLAB Central (mathworks.cn)3、代码:【找到figure中对应对象并设置属性】 4、结果【同样的轨迹,实影的先运动几个步数,虚影后续运动】    

    2024年02月17日
    浏览(32)
  • 音乐流派分类:探索利用Matlab,Django,JavaScript和Python实现85%准确率的机器学习方法

    音乐,这种涵盖了历史,文化,艺术和心理学的丰富多彩的媒体形式,一直以来都是人类社会生活的重要组成部分。通过时间的推移,音乐的风格和形式不断演变,形成了我们今天所熟知的各种音乐流派。音乐流派的分类,一直以来都是个颇具争议的主题,其主观性和模糊性

    2024年02月09日
    浏览(60)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包