Matlab 三维散点图绘制及数据标注 - 网络空间负载配置可视化
% 设置文件名和sheet名
file = '混合表.xls';
sheet1 = 'Sheet1';
sheet2 = 'Sheet2';
% 读取第一个sheet内的坐标信息
[num1, txt1, raw1] = xlsread(file, sheet1);
x1 = num1(:, 1);
y1 = num1(:, 2);
z1 = num1(:, 3);
names1 = txt1(:, 4); % 假设第四列是点的名称
% 读取第二个sheet内的坐标信息
[num2, txt2, raw2] = xlsread(file, sheet2);
x2 = num2(:, 1);
y2 = num2(:, 2);
z2 = num2(:, 3);
names2 = txt2(:, 4); % 假设第四列是点的名称
% 绘制三维散点图
figure;
scatter3(x1, y1, z1, 'r', 'filled');
hold on;
scatter3(x2, y2, z2, 'b', 'filled');
xlabel('X');
ylabel('Y');
zlabel('Z');
title('网络空间负载配置图');
legend(sheet1, sheet2);
grid on;
% 绘制相同x和y坐标的点之间的虚线连接
for i = 1:length(x1)
idx = find(x2 == x1(i) & y2 == y1(i));
if ~isempty(idx)
plot3([x1(i), x2(idx)], [y1(i), y2(idx)], [z1(i), z2(idx)], '--k');
% 添加文本标签
text((x1(i)+x2(idx))/2, (y1(i)+y2(idx))/2, (z1(i)+z2(idx))/2, num2str(matrix1(i, idx)), 'Color', 'k', 'FontSize', 8, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle');
end
end
% 添加文本标签
for i = 1:length(x1)
text(x1(i), y1(i), z1(i), names1{i}, 'Color', 'red', 'FontSize', 8);
end
for i = 1:length(x2)
text(x2(i), y2(i), z2(i), names2{i}, 'Color', 'blue', 'FontSize', 8);
end
% 读取矩阵2的数据
matrix2 = xlsread('物理网络.xlsx');
% 连接z轴不为零的点
for i = 1:length(x2)
for j = 1:length(x2)
if matrix2(j, i) ~= 0
plot3([x2(i), x2(j)], [y2(i), y2(j)], [z2(i), z2(j)], '-b');
end
end
end
% 读取矩阵1的数据
matrix1 = xlsread('逻辑网络.xlsx');
% 连接z轴为零的点
for k = 1:length(x1)
for l = 1:length(x1)
if matrix1(k, l) ~= 0
plot3([x1(k), x1(l)], [y1(k), y1(l)], [z1(k), z1(l)], '-r');
end
end
end
在上述代码中,我在绘制虚线连接的同时添加了文本标签。使用text函数将矩阵数据标注在对应的连线中间位置,并通过设置HorizontalAlignment和VerticalAlignment参数来调整文本的对齐方式。
请确保矩阵数据文件的格式正确,并且矩阵中的数据与代码中的索引和变量对应正确。根据你的实际数据和需求,对文件路径和索引进行适当的调整。
原文地址: https://www.cveoy.top/t/topic/F8f 著作权归作者所有。请勿转载和采集!