【可视化】基于Matlab实现图表视化相关矩阵,相关值显示为左下角的热图,使用颜色渐变来指示强度,福利

 ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,

代码获取、论文复现及科研仿真合作可私信。

🍎个人主页:Matlab科研工作室

🍊个人信条:格物致知。

更多Matlab完整代码及仿真定制内容点击👇

智能优化算法       神经网络预测       雷达通信      无线传感器        电力系统

信号处理              图像处理               路径规划       元胞自动机        无人机

🔥 内容介绍

在数据分析和可视化中,图表视化是一种非常有用的工具。通过图表视化,我们可以更直观地理解数据之间的关系和趋势。在Matlab中,我们可以通过简单的代码实现图表视化,并且可以使用颜色渐变来显示相关值的强度。

在本文中,我们将介绍如何使用Matlab实现图表视化相关矩阵,并将相关值显示为左下角的热图。首先,让我们来看看如何生成相关矩阵。在Matlab中,我们可以使用corr函数来计算相关矩阵。例如,我们可以使用以下代码生成一个随机的相关矩阵:

data = randn(100, 3); % 生成随机数据
corrMatrix = corr(data); % 计算相关矩阵

一旦我们有了相关矩阵,我们就可以使用图表视化来展示相关值的强度。我们可以使用热图来显示相关值,其中颜色的深浅表示相关值的强度。在Matlab中,我们可以使用heatmap函数来实现热图的可视化。以下是一个简单的示例代码:

heatmap(corrMatrix, 'Colormap', 'jet', 'ColorLimits', [-1, 1]); % 使用jet颜色映射,并设置颜色范围为-1到1

通过这段代码,我们可以将相关矩阵可视化为一个热图,并且使用颜色渐变来指示相关值的强度。这样,我们可以更直观地理解数据之间的关系,并且可以快速发现相关性较强的数据。

除了热图之外,Matlab还提供了许多其他图表视化的工具,如散点图、折线图、柱状图等。通过这些工具,我们可以更全面地分析和理解数据,为我们的工作和决策提供更多的参考和支持。

总之,图表视化是数据分析中非常重要的一环,通过Matlab,我们可以轻松实现相关矩阵的图表视化,并使用颜色渐变来指示相关值的强度。希望本文对你有所帮助,谢谢阅读!

📣 完整代码

function [ defaultColors ] = getdefaultcolors(n)    if(nargin < 1)       n = 0;     end    defaultColors = [0 0.447 0.741; 0.85 0.325 0.098; ...        0.929 0.694 0.125; 0.466 0.674 0.188; 0.301 0.745 0.933; ...        0.494 0.184 0.556; 0.635 0.078 0.184];        other_colors = [0         0.3793    0.1379;                    1.0000    0.0345    0.6897;                    0.1034    1.0000         0;                    0         0.2069    0.1724;                    0.5172    0.1724    1.0000;                    0.9310    1.0000         0;                    0.2069    0.1724    0.1724;                    0         0.1034    0.3448;                    0.7931         0    0.3448;                    1.0000    0.1034    0.9310];     defaultColors = [defaultColors; other_colors];    if(n > size(defaultColors, 2))        clr = distinguishable_colors(n - size(defaultColors, 1), [defaultColors; 0 0 0; 1 1 1], @(x) colorspace('RGB->HSV',x));        defaultColors = [defaultColors; clr];    endend
function[width, height] = measureText(txt, opt, axis)    if(nargin < 3)       axis = gca();     end    hTest = text(axis, 0, 0, txt, opt);    textExt = get(hTest, 'Extent');    delete(hTest);    height = textExt(4);    %Height    width = textExt(3);     %Widthend
%% Prepare the data% Load the example dataset and compute correlation matrixD = load('patients.mat');D.IsFemale = ismember(D.Gender, 'Female');[~, D.HealthStatus] = ismember(D.SelfAssessedHealthStatus, {'Poor', 'Fair', 'Good', 'Excellent'});% Prepare the matrixX = [D.IsFemale, D.Age, D.Weight, D.Height, D.Smoker, ...     D.Systolic, D.Diastolic, D.HealthStatus];axislabels = {'GenderFemale', 'Age', 'Weight', 'Height', ...    'IsSmoker', 'Systolic', 'Diastolic', 'HealthStatus'};C = corr(X);%% Draw the correlogram% Prepare the figure and set the sizefigure(1); clf();set(gcf, 'Position', [0 0 640 480]); movegui('center');% Draw the figurecorrelogram(C, 'AxisLabels', axislabels); set(gca, 'FontSize', 12)
function [] = correlogram(C, varargin)    warning('off', 'MATLAB:polyshape:repairedBySimplify');    p = inputParser;    validX = @(x) validateattributes(x, {'cell', 'numeric', 'logical'}, ...        {'2d', 'nonempty', 'real', '<=', 1, '>=', -1});    validScalar = @(x) validateattributes(x, {'logical', 'numeric'}, ...        {'scalar','nonempty','real','nonnan'});    validColorArray = @(x) validateattributes(x, {'numeric'}, ...        {'2d', 'nonempty', 'real', 'nonnan', 'ncols', 3});    addRequired(p, 'C', validX);    addParameter(p, 'AxisLabels', [], @iscell);    addParameter(p, 'Labels', [], @iscell);    addParameter(p, 'cMin', -1, validScalar);    addParameter(p, 'cMax', 1, validScalar);    addParameter(p, 'colorMin', [0 0 1], validColorArray);    addParameter(p, 'colorZero', [1 1 1]*0.96, validColorArray);    addParameter(p, 'colorMax', [1 0 0], validColorArray);    addParameter(p, 'numDigits', 2, @isnumeric);    addParameter(p, 'boxWidthRatio', 1, validScalar);    addParameter(p, 'boxHeightRatio', 1, validScalar);    addParameter(p, 'BoxLineWidth', 0.5, validScalar);    addParameter(p, 'RadiusRatio', 0.9, validScalar);    addParameter(p, 'Colors', [], @isnumeric);    addParameter(p, 'VariableNames', cell(length(C), 1), @iscell);    addParameter(p, 'ShowDiagonal', false, @islogical);    addParameter(p, 'FontSize', [], validScalar);    addParameter(p, 'Sorting', true, @islogical);    addParameter(p, 'XDir', 'normal', @ischar);    addParameter(p, 'YDir', 'reverse', @ischar);    parse(p, C, varargin{:});    param = p.Results;    checkUsingDefaults = @(p,varname) any(strcmp(p.UsingDefaults,varname));    [nRow, nColumn] = size(C);    if((nRow == nColumn) && param.Sorting)         for i = 1:2            Z = linkage(C, 'average');            D = pdist(C);            si = optimalleaforder(Z, D);            C = C(si, si);            if(~isempty(param.AxisLabels))                param.AxisLabels = param.AxisLabels(si);                param.VariableNames = param.VariableNames(si);            end        end    end        if(checkUsingDefaults(p, 'Labels'))        labels = cell(size(C));        for iRow = 1:nRow            for iColumn = 1:nColumn                cValue = C(iRow, iColumn);                labels{iRow, iColumn} = num2str(cValue, ['%.', num2str(param.numDigits), 'f']);                if(iRow == iColumn); labels{iRow, iColumn} = param.VariableNames{iRow}; end            end        end        param.Labels = labels;    end       if(checkUsingDefaults(p, 'Colors'))        colorMin = param.colorMin;        colorZero = param.colorZero;        colorMax = param.colorMax;        cols = color_spacing_continuous(C(:), [param.cMin; 0; param.cMax], [colorMin; colorZero; colorMax]);        param.Colors = reshape(cols, nRow, nColumn, 3);    end        box_width_ratio = param.boxWidthRatio;    box_height_ratio = param.boxHeightRatio;        row_height = 1 ./ nRow;    col_width = 1./ nColumn;        xcPos = zeros(nColumn, 1);    ycPos = zeros(nRow, 1);        for iRow = 1:nRow        for iColumn = 1:nColumn            row_gap = (1 - box_height_ratio) * 0.5;            col_gap = (1 - box_width_ratio) * 0.5;            x = 0 + (col_gap + iColumn - 1) * col_width;            y = 0 + (row_gap + iRow - 1) * row_height;            w = col_width * (1 - col_gap);            h = row_height * (1 - row_gap);            xcenter = x + w/2;            ycenter = y + h/2;            xcPos(iColumn) = xcenter;            ycPos(iRow) = ycenter;        end    end    isHoldOn = ishold();    if(~isHoldOn)        cla();    end    hold('on');    set(gca, 'XTick', xcPos, 'XTickLabel', param.AxisLabels);    set(gca, 'YTick', ycPos, 'YTickLabel', param.AxisLabels);    xlim([0 1]);    ylim([0 1]);    dim1 = get(gcf, 'Position');    dim2 = get(gca, 'Position');    axis_width = dim1(3) * dim2(3);    axis_height = dim1(4) * dim2(4);    width_to_height = axis_width / axis_height;    alpha = 0.8;    if(width_to_height >= 1)        r_width_mult = 1 / (width_to_height^alpha);        r_height_mult = 1;    else        r_width_mult = 1;        r_height_mult = (width_to_height^alpha);    end    S = struct();    S.FontSize = 10;    [txt_width, txt_height] = measureText('-0.99', S, gca);    scaling = max(txt_width / col_width, txt_height / row_height);    desired_txt_gap = 0.9;    font_size = max(floor(S.FontSize * (1/scaling) * desired_txt_gap), 1);    if(isempty(param.FontSize))        param.FontSize = font_size;    end    txtOptions = struct();    txtOptions.HorizontalAlignment = 'center';    txtOptions.VerticalAlignment = 'middle';    txtOptions.FontSize = param.FontSize;        rec_line_width = param.BoxLineWidth;    for iRow = 1:nRow        for iColumn = 1:nColumn            row_gap = (1 - box_height_ratio) * 0.5;            col_gap = (1 - box_width_ratio) * 0.5;            x = 0 + (col_gap + iColumn - 1) * col_width;            y = 0 + (row_gap + iRow - 1) * row_height;            w = col_width * (1 - col_gap);            h = row_height * (1 - row_gap);            xcenter = x + w/2;            ycenter = y + h/2;            xcPos(iColumn) = xcenter;            ycPos(iRow) = ycenter;            pos = [x y w h];            color = param.Colors(iRow, iColumn, :);                        if(iRow > iColumn)                rectangle('Position',pos, 'FaceColor', color, 'LineWidth', rec_line_width)                txt = param.Labels{iRow, iColumn};                text(xcenter, ycenter, txt, txtOptions);            end            if((iRow == iColumn) && param.ShowDiagonal)                rectangle('Position',pos, 'FaceColor', color, 'LineWidth', rec_line_width)                txt = param.Labels{iRow, iColumn};                text(xcenter, ycenter, txt, txtOptions);            end            if(iRow < iColumn)                cvalue = C(iRow, iColumn);                theta_start = -0.5 * pi;                theta_end = theta_start + (cvalue * 2 * pi);                theta_range = theta_end - theta_start;                                n = 100;                ww = w * param.RadiusRatio * r_width_mult;                hh = h * param.RadiusRatio * r_height_mult;                                theta = [(0:n-1)]*(2*pi)/n;                xx = xcenter + [0.5*ww*cos(theta)];                yy = ycenter + [0.5*hh*sin(theta)];                Pcircle = polyshape(xx,yy);                theta = [(0:n-1) (n-1e-3)]*(theta_range)/n + theta_start;                xx = xcenter + [0 0.5*ww*cos(theta)];                yy = ycenter + [0 0.5*hh*sin(theta)];                P = polyshape(xx,yy);                                plot(Pcircle, 'FaceAlpha', 0, 'LineStyle', '-', 'EdgeColor', [1 1 1] * 0.55);                plot(P, 'FaceAlpha', 1, 'FaceColor', color);            end                    end    end    set(gca, 'XDir', param.XDir);    set(gca, 'YDir', param.YDir)    if(~isHoldOn); hold('off'); endend
function [C] = color_spacing_continuous( values, breaks, colors)    indices = zeros(size(values));    for i = 1:length(breaks)       if(i ~= length(breaks))           q = values >= breaks(i);       else           q = values > breaks(i);       end       indices(q) = indices(q) + 1;    end    ratio = zeros(size(values));    ranges = breaks(2:end) - breaks(1:(end)-1);    for i = 1:(length(breaks)-1)        ind = indices == i;        ratio(ind) = (values(ind) - breaks(i)) / (ranges(i));            end    C = (1-ratio).*colors(indices, :) + ratio.*colors(indices+1, :);end

⛳️ 运行结果

🔗 参考文献

🎈 部分理论引用网络文献,若有侵权联系博主删除
🎁  关注我领取海量matlab电子书和数学建模资料

👇  私信完整代码、论文复现、期刊合作、论文辅导及科研仿真定制

1 各类智能优化算法改进及应用
生产调度、经济调度、装配线调度、充电优化、车间调度、发车优化、水库调度、三维装箱、物流选址、货位优化、公交排班优化、充电桩布局优化、车间布局优化、集装箱船配载优化、水泵组合优化、解医疗资源分配优化、设施布局优化、可视域基站和无人机选址优化
2 机器学习和深度学习方面
卷积神经网络(CNN)、LSTM、支持向量机(SVM)、最小二乘支持向量机(LSSVM)、极限学习机(ELM)、核极限学习机(KELM)、BP、RBF、宽度学习、DBN、RF、RBF、DELM、XGBOOST、TCN实现风电预测、光伏预测、电池寿命预测、辐射源识别、交通流预测、负荷预测、股价预测、PM2.5浓度预测、电池健康状态预测、水体光学参数反演、NLOS信号识别、地铁停车精准预测、变压器故障诊断
2.图像处理方面
图像识别、图像分割、图像检测、图像隐藏、图像配准、图像拼接、图像融合、图像增强、图像压缩感知
3 路径规划方面
旅行商问题(TSP)、车辆路径问题(VRP、MVRP、CVRP、VRPTW等)、无人机三维路径规划、无人机协同、无人机编队、机器人路径规划、栅格地图路径规划、多式联运运输问题、车辆协同无人机路径规划、天线线性阵列分布优化、车间布局优化
4 无人机应用方面
无人机路径规划、无人机控制、无人机编队、无人机协同、无人机任务分配、无人机安全通信轨迹在线优化
5 无线传感器定位及布局方面
传感器部署优化、通信协议优化、路由优化、目标定位优化、Dv-Hop定位优化、Leach协议优化、WSN覆盖优化、组播优化、RSSI定位优化
6 信号处理方面
信号识别、信号加密、信号去噪、信号增强、雷达信号处理、信号水印嵌入提取、肌电信号、脑电信号、信号配时优化
7 电力系统方面
微电网优化、无功优化、配电网重构、储能配置
8 元胞自动机方面
交通流 人群疏散 病毒扩散 晶体生长
9 雷达方面
卡尔曼滤波跟踪、航迹关联、航迹融合

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值