如何在 MATLAB 中绘制 3D 图? [英] How to plot a 3D plot in MATLAB?
问题描述
我有三个参数x
、y
和t
.但问题是我的文件结构.
I have three parameters x
,y
and t
. But the problem is my file structure.
我的文件被命名为:
e_x_y.txt
t_x_y.txt
其中 e_x_y.txt
对于 x
和 y
和 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.
我需要在 x vs y vs t
图表上绘制 e_x_y.txt
中的值.
I need to plot the values in e_x_y.txt
on a x vs y vs t
graph.
最好的方法是什么?
我知道 x 和 y 值是什么,所以我不必从文件名中扣除它们.
I know what x and y values are, so I don't have to deduct them from the file names.
为了让事情更清楚,
假设我的文件是:
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
我想绘制以下几点:
(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...
为了做到这一点,您必须绘制一系列 x,y,t
点,并以某种方式表示每个点的误差值 e
.您可以通过更改点的颜色或大小来做到这一点.在此示例中,我将在每个点绘制一个球体,其直径根据误差而变化(直径 1 等于最大预期误差).颜色代表时间.我将使用您添加到问题中的示例数据(格式为 5×4 矩阵,列包含 x
、y
、t
和 e
数据):
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. 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). 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):
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');
这是它的样子:
问题:虽然情节看起来很有趣,但不是很直观.此外,以这种方式绘制大量点会变得杂乱无章,很难看清所有点.
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.
最好制作数据的 3-D 图,因为它可能更容易解释.这里,x 轴代表迭代次数,y 轴代表每个单独的网络:
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:
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)
这会产生更清晰的图:
这篇关于如何在 MATLAB 中绘制 3D 图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!