点击上方蓝字和“好玩的MATLAB”一起快乐玩耍吧!
4c0sqgapngr6408494013.jpg
好玩的matlab
带你解锁不一样的matlab新玩法
今天介绍一下山脊图,喜欢此推文的小伙伴们记得点赞+关注+分享!【尊重作者劳动成果,转载请注明推文链接和公众号名】上周去逛了一下商店看见一个笔架
atoj4doizxc6408494113.png
这突然让我想起,这个不就像科研绘图的山脊图嘛,于是我打算用 MATLAB 复刻一个。
ksjlpb3z1fh6408494213.png
效果图
bbogftjx2rm6408494313.png
lwbebl2uae26408494414.gif
山脊图介绍山脊图(Ridge Plot),也被称为Joy Plot,是一种用于可视化数据分布的图表,特别是用于显示多个组的分布情况。在这种图表中,每个组的数据分布都通过平滑的密度曲线来表示,这些曲线沿着垂直轴堆叠,形成类似山脊的视觉效果。山脊图是核密度估计(KDE)的一个应用,它提供了比传统的条形图或直方图更平滑的数据分布视图。
用途:山脊图主要用于展示和比较不同组或类别内数据的分布情况。它特别适合于以下情境:
比较多个分布:当需要展示和比较多组数据的分布形状时,山脊图能够直观地展示出分布之间的差异。
展现趋势变化:在时间序列数据中,山脊图可以用来展示数据随时间的变化趋势。
优化空间利用:通过堆叠的方式,山脊图能在有限的空间内展示大量的分布信息。
缺点:
可读性:对于不熟悉这种图表的观众来说,山脊图可能难以理解和解读。
叠加问题:当曲线重叠较多时,可能会导致图表的某些部分难以区分。
数据量限制:对于数据量非常大的数据集,山脊图可能不那么有效,因为过多的曲线会使图表变得杂乱。
应用场景:
时间序列分析:在金融、经济、气象等领域,用于展示某个变量随时间的变化趋势。
社会科学:比如在选举数据分析中,展示不同候选人或党派在不同地区或人群中的支持率分布。
生物医学:用于展示不同实验组或治疗方法下的生物统计数据分布。
绘图教程线条山脊图data = randn(100,12)+[ 20 22 24 26 28 31 30 28 26 24 22 20];lgLable= {'January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December'};colors=makeColorMap([0,0,0;1,1,1],12);figure('Position',[476 246 511 620])p=0.15;yTick=(1:size(data, 2))*p;for i = 1:size(data, 2) [f, x] = ksdensity(data(:, i)); f=f+i*p; pHandle=plot(x, f, 'LineWidth', 1.5,'color',colors(i,:)); hold on; yline(yTick(i),'-.','LineWidth',1,'Color',pHandle.Color,'HandleVisibility','off')endgrid off;box off;legend(lgLable,'box','on','Color',[1,1,1]*0.95,'EdgeColor','none');set(gca, 'YTick',yTick , 'YTickLabel',lgLable,'LineWidth', 1.5, 'XMinorTick', 'on', 'TickDir', 'out', ... 'fontname','Times new Roman','color',[1,1,1]*0.9);xlabel('XXXX-Value');ylabel('YYYY-Value');title('Ridge Plot');hold off;
1vrcojsfxym6408494514.png
填充山脊图data = randn(100,12)+[ 20 22 24 26 28 31 30 28 26 24 22 20];lgLable= {'January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December'};% colors=makeColorMap(winter,size(data, 2));colors=makeColorMap(jet,size(data, 2));figure('Position',[476 246 511 620])p =0.2;hold on;for i = size(data, 2):-1:1 [f, x] = ksdensity(data(:, i)); fShifted = f + i * p; pHandle = plot(x, fShifted,'color',colors(i,:),'LineWidth', 1.5,'HandleVisibility', 'off'); yline(i * p, '-.', 'LineWidth', 1, 'Color', colors(i,:),'HandleVisibility', 'off') Xfill = [x, fliplr(x)]; Yfill = [fShifted, ones(1, length(x)) * i * p]; fill(Xfill, Yfill, pHandle.Color, 'EdgeColor', 'none', 'FaceAlpha', 0.3);endyTick = (1:size(data, 2)) * p;set(gca, 'YTick', yTick, ... 'YTickLabel', lgLable, ... 'LineWidth', 1.5, 'XMinorTick', 'on', 'TickDir', 'out', ... 'fontname','Times new Roman');xlabel('XXXX-Value');ylabel('YYYY-Value');title('Ridge Plot');ax = gca;hold off;?
busl2k1mzkh6408494614.png
3D填充山脊图data = randn(100,12)+[ 20 22 24 26 28 31 30 28 26 24 22 20];lgLabel= {'January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December'};p = 0.2;xTick=(1:size(data, 2))*p;figure('Position',[476 246 511 620])hold on;colors=makeColorMap(hsv,size(data, 2));legendHandles = zeros(size(data, 2), 1);for i = 1:size(data, 2) [f, y] = ksdensity(data(:, i)); xShifted = i * p; plot3(xShifted * ones(size(f)), y, f, 'LineWidth', 1.5, 'Color', colors(i,:), 'HandleVisibility', 'off'); Yfill = [y, fliplr(y)]; Xfill = [xShifted * ones(1, length(y)), xShifted * ones(1, length(y))]; Zfill = [f, zeros(size(f))]; legendHandles(i) = fill3(Xfill, Yfill, Zfill, colors(i,:), 'EdgeColor', 'none', 'FaceAlpha', 0.3); endgrid off;box on;legend(lgLabel,'box','on','Color',[1,1,1]*0.95,'EdgeColor','none','FontName','Times New Roman','location','best');set(gca, 'XTick',xTick, 'XTickLabel',lgLabel,'FontName','Times New Roman','linewidth',1)xlabel('XXXX-Value');ylabel('YYYY-Value');zlabel('ZZZ-Value');title('Ridge Plot');view([-83.4000 71.6830])hold off;
pwmrgr2dt4f6408494714.png
不同核山脊图、添加边际散点密度data = randn(100,3)+[ 20 24 28];colors=makeColorMap(jet,size(data, 2));figure('Position',[476 246 511 620])p=0;hold on;yTick=(1:size(data, 2))*p;lineLength = 0.02; % 竖线的长度for i = size(data, 2):-1:1 [f, x] = ksdensity(data(:, i)); fShifted = f + i * p; pHandle = plot(x, fShifted, 'color', colors(i,:), 'LineWidth', 1.5, 'HandleVisibility', 'off'); yline(i * p, '-.', 'LineWidth', 1, 'Color', colors(i,:), 'HandleVisibility', 'off'); Xfill = [x, fliplr(x)]; Yfill = [fShifted, ones(1, length(x)) * i * p]; fill(Xfill, Yfill, pHandle.Color, 'EdgeColor', 'none', 'FaceAlpha', 0.3); for j = 1:length(data(:, i)) line([data(j, i), data(j, i)], [i * p, i * p + lineLength], 'Color', pHandle.Color, 'LineWidth', 1); endendgrid off;box off;ax=gca;set(ax,'LineWidth', 1.5, 'XMinorTick', 'on', 'TickDir', 'out', ... 'fontname','Times new Roman','color',[1,1,1]*0.9);ax.YLim=[0,1];ax.XLim=[15,32];xlabel('XXXX-Value');ylabel('YYYY-Value');title('Ridge Plot');hold off;
cdqr4xuhnft6408494814.png
横坐标方向、全局渐变的散点图
data = randn(100,12)+[ 20 22 24 26 28 31 30 28 26 24 22 20];lgLabel={'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'};p = 0.2;numColors = 256;% 创建一个从紫色到红色再到黄色的颜色映射colorMap =makeColorMap([0.1490 0.0353 0.5608;0.7305 0.2500 0.4922;0.9569 0.9490 0.1765],numColors);% 计算全局的x轴范围xMin = min(data, [], 'all');xMax = max(data, [], 'all');figure('Position',[476 246 511 620])hold on;for i = size(data, 2):-1:1 [f(i,:), x] = ksdensity(data(:, i), 'Kernel', 'triangle'); fShifted = f(i,:) + i * p; % 偏移每个分布 plot(x, fShifted, 'LineWidth', 1.5, 'color', 'k', 'HandleVisibility', 'off'); yline(i * p, '-.', 'LineWidth', 1, 'HandleVisibility', 'off'); for j = 1:length(x)-1 Xfill = [x(j), x(j+1), x(j+1), x(j)]; Yfill = [i * p, i * p, fShifted(j+1), fShifted(j)]; % 计算当前点的颜色比例 colorRatio = (x(j) - xMin) / (xMax - xMin); colorIdx = min(numColors, max(1, round(colorRatio * (numColors - 1)) + 1)); fill(Xfill, Yfill, colorMap(colorIdx, :), 'EdgeColor', 'none', 'FaceAlpha', 0.9); endend% 设置轴标签和标题yTick = (1:size(data, 2)) * p;set(gca, 'YTick', yTick, ... 'YTickLabel', lgLabel, ... 'LineWidth', 1.5, 'XMinorTick', 'on', 'TickDir', 'out', ... 'fontname','Times new Roman');xlabel('XXXX-Value');ylabel('YYYY-Value');title('Ridge Plot');% 设置颜色条colormap(colorMap);cBar = colorbar;cBar.TickDirection = 'out';cBar.LineWidth = 1;cBar.TickLength = 0.02;cBar.FontName = 'Times new Roman';caxis([xMin,xMax]);hold off;
d5hsmrphttj6408494914.png
横坐标方向、单核渐变图data = randn(100,12) + [20 22 24 26 28 31 30 28 26 24 22 20];lgLabel = {'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'};numColors = 256; % 颜色数量colors = jet(numColors); % 使用jet颜色映射figure('Position',[476 246 511 620])p = 0.2;hold on;for i = size(data, 2):-1:1 [f, x] = ksdensity(data(:, i)); fShifted = f + i * p; plot(x, fShifted, 'LineWidth', 1.5, 'HandleVisibility', 'off'); yline(i * p, '-.', 'LineWidth', 1, 'HandleVisibility', 'off'); colorGradient = linspace(1, numColors, length(x)); for j = 1:length(x)-1 Xfill = [x(j), x(j+1), x(j+1), x(j)]; Yfill = [i * p, i * p, fShifted(j+1), fShifted(j)]; fill(Xfill, Yfill, colors(round(colorGradient(j)), :), 'EdgeColor', 'none', 'FaceAlpha', 0.7); endendyTick = (1:size(data, 2)) * p;set(gca, 'YTick', yTick, 'YTickLabel', lgLabel, ... 'LineWidth', 1.5, 'XMinorTick', 'on', 'TickDir', 'out', ... 'fontname', 'Times new Roman');xlabel('XXXX-Value');ylabel('YYYY-Value');title('Ridge Plot');colormap(colors);cBar = colorbar;cBar.TickDirection = 'out';cBar.LineWidth = 1;cBar.TickLength = 0.02;cBar.FontName = 'Times new Roman';hold off;
1qfjo5y2nrz6408495014.png
每个山脊图设置不同颜色,且添加边际散点图data = randn(100,12)+[ 20 22 24 26 28 31 30 28 26 24 22 20];lgLable = {'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'};figure('Position', [476 246 511 620])p = 0.3;lineLength = 0.04; % 竖线的长度colors=makeColorMap(hsv,size(data, 2));hold on;for i = size(data, 2):-1:1 [f, x] = ksdensity(data(:, i)); fShifted = f + i * p; pHandle = plot(x, fShifted, 'color', colors(i,:), 'LineWidth', 1.5, 'HandleVisibility', 'off'); yline(i * p, '-.', 'LineWidth', 1, 'Color', colors(i,:), 'HandleVisibility', 'off'); Xfill = [x, fliplr(x)]; Yfill = [fShifted, ones(1, length(x)) * i * p]; fill(Xfill, Yfill, pHandle.Color, 'EdgeColor', 'none', 'FaceAlpha', 0.3); for j = 1:length(data(:, i)) line([data(j, i), data(j, i)], [i * p, i * p + lineLength], 'Color', 'k', 'LineWidth', 1); endendyTick = (1:size(data, 2)) * p;set(gca, 'YTick', yTick, ... 'YTickLabel', lgLable, ... 'LineWidth', 1.5, 'XMinorTick', 'on', 'TickDir', 'out', ... 'fontname', 'Times new Roman');xlabel('XXXX-Value');ylabel('YYYY-Value');title('Ridge Plot');
piebuugc4v26408495115.png
每个山脊图设置不同颜色单核横向颜色渐变data = randn(100,12)+[ 20 22 24 26 28 31 30 28 26 24 22 20];lgLabel = {'January', 'February', 'March', 'April', 'May', 'June', ... 'July', 'August', 'September', 'October', 'November', 'December'};p = 0.2;numColors = 256;% 创建12个不同的颜色映射,每个月一个colorMaps = cell(1, 12);colorList={parula,turbo,hsv,hot,cool,spring,summer,autumn,winter,gray,bone,copper,pink,jet,lines,colorcube,prism,flag};for i = 1:12 colorMaps{i} = makeColorMap(colorList{i}, numColors);end% 计算全局的x轴范围xMin = min(data, [], 'all');xMax = max(data, [], 'all');figure('Position', [476 246 511 620])hold on;for i = size(data, 2):-1:1 [f(i,:), x] = ksdensity(data(:, i), 'Kernel', 'triangle'); fShifted = f(i,:) + i * p; % 偏移每个分布 plot(x, fShifted, 'LineWidth', 1.5, 'color', 'k', 'HandleVisibility', 'off'); yline(i * p, '-.', 'LineWidth', 1, 'HandleVisibility', 'off'); currentColorMap = colorMaps{i}; for j = 1:length(x)-1 Xfill = [x(j), x(j+1), x(j+1), x(j)]; Yfill = [i * p, i * p, fShifted(j+1), fShifted(j)]; colorRatio = (x(j) - xMin) / (xMax - xMin); colorIdx = min(numColors, max(1, round(colorRatio * (numColors - 1)) + 1)); fill(Xfill, Yfill, currentColorMap(colorIdx, :), 'EdgeColor', 'none', 'FaceAlpha', 0.9); endendyTick = (1:size(data, 2)) * p;set(gca, 'YTick', yTick, ... 'YTickLabel', lgLabel, ... 'LineWidth', 1.5, 'XMinorTick', 'on', 'TickDir', 'out', ... 'fontname', 'Times new Roman');xlabel('XXXX-Value');ylabel('YYYY-Value');title('Ridge Plot');hold off;
zfss5pq5dng6408495215.png
山脊图的 Y值映射颜色变化
data = randn(100,12) + [20 22 24 26 28 31 30 28 26 24 22 20];lgLabel = {'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'};numColors = 100; % 颜色数量colors =makeColorMap(jet, numColors);figure('Position',[476 246 511 620])p = 0.2;hold on;for i = size(data, 2):-1:1 [f, x] = ksdensity(data(:, i)); fShifted = f + i * p; plot(x, fShifted, 'LineWidth', 1.5, 'HandleVisibility', 'off'); yline(i * p, '-.', 'LineWidth', 1, 'HandleVisibility', 'off'); % 计算整个分布的最小和最大纵坐标 minY = min(fShifted); maxY = max(fShifted); for j = 1:length(x)-1 Xfill = [x(j), x(j+1), x(j+1), x(j)]; Yfill = [i * p, i * p, fShifted(j+1), fShifted(j)]; avgY = mean(Yfill(3:4)); colorIdx = round(((avgY - minY) / (maxY - minY)) * (numColors - 1)) + 1; fill(Xfill, Yfill, colors(colorIdx, :), 'EdgeColor', 'none', 'FaceAlpha', 0.6); endendyTick = (1:size(data, 2)) * p;set(gca, 'YTick', yTick, 'YTickLabel', lgLabel, ... 'LineWidth', 1.5, 'XMinorTick', 'on', 'TickDir', 'out', ... 'fontname', 'Times new Roman');xlabel('XXXX-Value');ylabel('YYYY-Value');title('Ridge Plot');colormap(colors);cBar = colorbar;cBar.TickDirection = 'out';cBar.LineWidth = 1;cBar.TickLength = 0.02;cBar.FontName = 'Times new Roman';hold off;
kt1nzr5xlps6408495315.png
四分位点
data = randn(100,12) + [20 22 24 26 28 31 30 28 26 24 22 20];lgLabel = {'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'};colors = makeColorMap(jet, size(data, 2));figure('Position', [476 246 511 620])p = 0.2;hold on;for i = size(data, 2):-1:1 [f, x] = ksdensity(data(:, i)); fShifted = f + i * p; pHandle = plot(x, fShifted, 'color', colors(i,:), 'LineWidth', 1.5, 'HandleVisibility', 'off'); yline(i * p, '-.', 'LineWidth', 1, 'Color', colors(i,:), 'HandleVisibility', 'off') Xfill = [x, fliplr(x)]; Yfill = [fShifted, ones(1, length(x)) * i * p]; fill(Xfill, Yfill, pHandle.Color, 'EdgeColor', 'none', 'FaceAlpha', 0.3); % 计算并标记四分位点 quartiles = quantile(data(:, i), [0.25 0.50 0.75]); for q = quartiles [~, idx] = min(abs(x-q)); plot(q, fShifted(idx), 'o', 'MarkerFaceColor', pHandle.Color, 'MarkerEdgeColor', 'k'); endendyTick = (1:size(data, 2)) * p;set(gca, 'YTick', yTick, ... 'YTickLabel', lgLabel, ... 'LineWidth', 1.5, 'XMinorTick', 'on', 'TickDir', 'out', ... 'fontname', 'Times new Roman');xlabel('XXXX-Value');ylabel('YYYY-Value');title('Ridge Plot');hold off;
lrs2wekqgat6408495415.png
data = randn(100,12) + [20 22 24 26 28 31 30 28 26 24 22 20];lgLabel = {'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'};colors = makeColorMap(jet, size(data, 2));figure('Position', [476 246 511 620])p = 0.2;hold on;for i = size(data, 2):-1:1 [f, x] = ksdensity(data(:, i)); fShifted = f + i * p; pHandle = plot(x, fShifted, 'color', colors(i,:), 'LineWidth', 1.5, 'HandleVisibility', 'off'); yline(i * p, '-.', 'LineWidth', 1, 'Color', colors(i,:), 'HandleVisibility', 'off'); Xfill = [x, fliplr(x)]; Yfill = [fShifted, ones(1, length(x)) * i * p]; fill(Xfill, Yfill, pHandle.Color, 'EdgeColor', 'none', 'FaceAlpha', 0.3); % 计算并标记四分位点,并绘制竖线 quartiles = quantile(data(:, i), [0.25 0.50 0.75]); for q = quartiles [~, idx] = min(abs(x-q)); plot(q, fShifted(idx), 'o', 'MarkerFaceColor', pHandle.Color, 'MarkerEdgeColor', 'k'); line([q q], [fShifted(idx) i * p], 'Color', pHandle.Color, 'LineStyle', '-.','LineWidth', 1); endendyTick = (1:size(data, 2)) * p;set(gca, 'YTick', yTick, ... 'YTickLabel', lgLabel, ... 'LineWidth', 1.5, 'XMinorTick', 'on', 'TickDir', 'out', ... 'fontname', 'Times new Roman');xlabel('XXXX-Value');ylabel('YYYY-Value');title('Ridge Plot');hold off;
oxo4jmzm4ce6408495515.png
其中makeColorMap函数代码之前「一张图搞定绘图配色问题」有详细介绍。
- -THE END- -
源码下载:gitee下载:https://gitee.com/iDMatlab/ridgePlot
wduzshz0lbz6408495616.png
QQ 群下载:
oioplhoowyu6408495716.png
fileexchange 下载:
u0op3cce3uk6408495816.png
参考资料:
【1】https://it.sohu.com/a/704677145_121123706
dpzliexlggi6408495916.gif
送书活动
包邮赠送 「北京大学出版社」赞助《3D科研绘图与学术图表绘制从入门到精通》
专业级3D科研绘图与学术图表绘制指南:以“设计基本概念+软件底层原理+案例实际操作”的思路进行全方位讲解,涵盖化学、材料学、生物医学等领域的科学可视化的艺术化表达
实例丰富:我们涵盖各类绘图软件与工具,让你能够自如运用不同技术绘制出高质量的图表。内容全面:全流程讲解3D科研绘图与学术图表绘制的方法,有效填补了现有同类型参考书的空白。经验总结:作者多年一线研发实战经验全面归纳整理,毫无保留分享技术要领。大咖力荐:多位大型科技公司技术高管和高校相关领域教研专家推荐。全彩印刷:图表案例精彩呈现,带来良好的阅读体验,方便理解和学习。
了解更多
【抽奖方式及满足条件】:
1.关注「好玩的MATLAB 」公众号和视频号
2.给本文点【赞】+【在看】;
3.留言区评论点赞最多的前2名。
4.本活动只针对从未获过奖的同学,之前获过奖的小伙伴,不用参加。
同时满足上述4个条件的读者朋友,包邮赠送一本.
【开奖时间】:2023年11月15日中午12点
【领奖方式】:在开奖时加小编私人微信:idmatlab
扫一扫加管理员微信
|