简体   繁体   English

如何在MATLAB中绘制3D图?

[英]How to plot a 3D plot in MATLAB?

I have three parameters x , y and t . 我有三个参数xyt But the problem is my file structure. 但问题是我的文件结构。

My files are named as: 我的文件命名为:

e_x_y.txt
t_x_y.txt

where e_x_y.txt has the error for particular values of x and y and t_x_y.txt has corresponding time values. 其中e_x_y.txt具有针对xy特定值的错误,并且t_x_y.txt具有对应的时间值。

I need to plot the values in e_x_y.txt on a x vs y vs t graph. 我需要在x vs y vs t图上绘制e_x_y.txt的值。

What is the best way to do that? 最好的方法是什么?

I know what x and y values are, so I don't have to deduct them from the file names. 我知道x和y值是什么,所以我不必从文件名中扣除它们。


To make things more clear, 为了使事情更清楚,

suppose my files are: 假设我的文件是:

e_4_5.txt
45
54
t_4_5.txt
2.0
6.0

e_7_8.txt
32
98
121
t_7_8.txt
2
9
1.0

I want to plot the following points: 我想绘制以下几点:

(4,5,2.0) = 45
(4,5,6.0) = 54
(7,8,2.0) = 32 
(7,8,9.0) = 98
(7,8,1.0) = 121

The type of plot you are trying to make may be difficult to visualize well. 您尝试制作的绘图类型可能难以很好地可视化。 I can give you two suggestions: one is what you want , and one is what you should probably do instead... 我可以给你两个建议:一个是你想要的 ,一个是你应该做的事情......

Plotting 4-D data: 绘制四维数据:

In order to do this, you will have to plot a series of x,y,t points and somehow represent the error value e at each point. 为此,您必须绘制一系列x,y,t点,并以某种方式表示每个点的误差值e You could do this by changing the color or size of the point. 您可以通过更改点的颜色或大小来完成此操作。 In this example, I'll plot a sphere at each point with a diameter that varies based on the error (a diameter of 1 equates to the maximum expected error). 在这个例子中,我将在每个点绘制一个球体,其直径根据误差而变化(直径为1等于最大预期误差)。 The color represents the time. 颜色代表时间。 I'll be using the sample data you added to the question (formatted as a 5-by-4 matrix with the columns containing the x , y , t , and e data): 我将使用您添加到问题中的样本数据(格式为5 x 4矩阵,其中包含xyte数据的列):

data = [4 5 2 45; 4 5 6 54; 7 8 2 32; 7 8 9 98; 7 8 1 121];
[x, y, z] = sphere;  % Coordinate data for sphere
MAX_ERROR = 121;     % Maximum expected error
for i = 1:size(data, 1)
  c = 0.5*data(i, 4)/MAX_ERROR;  % Scale factor for sphere
  X = x.*c+data(i, 1);           % New X coordinates for sphere
  Y = y.*c+data(i, 2);           % New Y coordinates for sphere
  Z = z.*c+data(i, 3);           % New Z coordinates for sphere
  surface(X, Y, Z, 'EdgeColor', 'none');  % Plot sphere
  hold on
end
grid on
axis equal
view(-27, 16);
xlabel('x');
ylabel('y');
zlabel('t');

And here's what it would look like: 这就是它的样子:

在此输入图像描述

The problem: Although the plot looks kind of interesting, it's not very intuitive. 问题:尽管情节看起来很有趣,但它不是很直观。 Also, plotting lots of points in this way will get cluttered and it will be hard to see them all well. 而且,以这种方式绘制很多点将会变得混乱,很难看到它们都很好。

More intuitive 3-D plot: 更直观的三维图:

It may be better to instead make a 3-D plot of the data, since it may be easier to interpret. 最好是制作数据的三维图,因为它可能更容易解释。 Here, the x-axis represents the iteration number and the y-axis represents each individual network: 这里,x轴表示迭代次数,y轴表示每个单独的网络:

plot3(1:2, [1 1], [2 45; 6 54]);           % Plot data for network 4-5
hold on
plot3(1:3, [2 2 2], [2 32; 9 98; 1 121]);  % Plot data for network 7-8
xlabel('iteration number');
set(gca, 'YTick', [1 2], 'YTickLabel', {'network 4-5', 'network 7-8'})
grid on
legend('time', 'error')
view(-18, 30)

This produces a much clearer plot: 这会产生更清晰的情节:

在此输入图像描述

Even though I am not convinced this the best way to visualize the data, here's a simple way to do it as you asked. 虽然我不相信这是可视化数据的最佳方式,但这是一种简单的方法,可以按照您的要求进行。 You can plot the 3D points in a simple scatter plot, and map the size OR the color to the values of the fourth dimension error . 您可以在简单的散点图中绘制3D点,并将大小或颜色映射到第四维度error的值。 Something like: 就像是:

x = randi(20, [10 1]);
y = randi(20, [10 1]);
t = randi(10, [10 1]);
e = randi(200, [10 1]);

% map `e` to color
figure(1)
scatter3(x, y, t, 200, e, 'filled')
xlabel('x'), ylabel('y'), zlabel('t')
colormap(hot), colorbar

% map `e` to size
figure(2)
scatter3(x, y, t, e, 'filled')
xlabel('x'), ylabel('y'), zlabel('t')

颜色尺寸

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM