How to plot data with >2 dimensions?

I have a variable of size 3x5x10000 and I want to plot this. The idea is that there are 5 trees which grow over 10000 timesteps in 3 different scenarios.
How could I get this in a plot which shows the growth of 5 trees over 10000 timesteps in 1 plot, and have 3 plots in total for each different scenario? The plot function can seemingly only use data with 2 dimensions.

回答(2 个)

Rik
Rik 2020-10-12
Apart from plot3 (as Alan suggested), you may also consider using subplot to divide the different scenarios and using hold to plot the multiple trees in a single axes (or the reverse of course). The fact that your variable is 3D doesn't mean you need to actually plot your data in 3 dimensions.

2 个评论

When I use subplot and plot, I put:
time = 1:n; % Here n is the amount of timesteps, so 10000 in this case
subplot(1,3,1) % Use a 1x3 plot and define whats on #1 with plot
plot(time,dh(1,:),'b') % plot time vs the variable dh, 1st row and all columns
This gives me the error that the vectors are not the same length, time = 1:10000 and my dh looks like this:
val(:,:,1) =
2.2321 2.6510 2.9316 3.1485 3.3277
2.2321 2.6510 2.9316 3.1485 3.3277
2.2321 2.6510 2.9316 3.1485 3.3277
val(:,:,2) =
2.2322 2.6511 2.9317 3.1486 3.3278
2.2322 2.6511 2.9317 3.1486 3.3278
2.2322 2.6511 2.9317 3.1486 3.3278
% This small 3x5 repeats itself n times, so 10000 times.
How do I get row 1, column 1, for every of these 10000 in the plot?
When I try to use
plot(1,1,:)
It says that the data cannot have more than 2 dimensions.
The end goal is to have the rows separated per subplot, and have every column as a different line per subplot.
Use dh(1,1,:) instead, that will select the first element from each 3x5 block. If plot still complains, use squeeze to reduce the dimensionality of your array.
dh=rand(3,5,10000);
time=1:size(dh,3);
n=0;
for scenario=1:size(dh,1)
ax=subplot(1,3,scenario);
cla(ax);%only for debugging: clear axes contents
hold(ax,'on');
for tree=1:size(dh,2)
name=sprintf('tree %d',tree);
plot(time,squeeze(dh(scenario,tree,:)),'DisplayName',name,'Parent',ax);
end
hold(ax,'off');
title(sprintf('scenario %d',scenario))
legend(ax)
end

请先登录,再进行评论。

类别

标签

提问:

2020-10-12

编辑:

Rik
2020-10-12

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by