How to plot several sets (in different colours) with stem3-plotting function?

조회 수: 11 (최근 30일)
So I have several line array A which has columns [x, y, val1, val2]. For example, I want to have a single stem3-plot, where val1s are red and val2s are blue.
stem3(A(:, 1), A(:, 2), A(:, 3), 'red') % Will plot vals1 in red.
But how to get then vals2 in colour blue to the same stem3-plot?
Tried something similar which works with 2D-stem: http://www.mathworks.se/help/matlab/ref/stem.html ("Plot Multiple Data Series"), but it didn't work
stem3(A(:, 1), A(:, 2), [A(:, 3), A(:, 4)]); "Error using stem3. The length of X must match the number of columns of Z."
stem3(A(:, 1), A(:, 2), [A(:, 3); A(:, 4)]); "Error using stem3. X and Y must be same length as Z or the lengths of X and Y must match the size of Z."

채택된 답변

AJ von Alt
AJ von Alt 2014년 1월 20일
편집: AJ von Alt 2014년 1월 20일
You can use hold to overlay multiple plots on one set of axes. The following code demonstrates the concept using stem3 to plot the datasets.
% Random inputs
A = randn(20,3);
B = randn(20,3);
figure;
% Create a 3-D stem plot for dataset A
stem3( A(:,1) , A(:,2) , A(:,3) ,'blue')
hold on; % Activate hold
% Create a 3-D stem plot for dataset B
stem3( B(:,1) , B(:,2) , B(:,3) ,'red')
hold off %turn off hold
legend('A','B')

추가 답변 (2개)

Bruno Pop-Stefanov
Bruno Pop-Stefanov 2014년 1월 20일
If you want to plot two series in the same axes in red and in blue, use hold on like in the following:
figure;
stem3(A(:, 1), A(:, 2), A(:, 3), 'r')
hold on
stem3(A(:, 1), A(:, 2), A(:, 4), 'b')
hold off

Jouni Lohikoski
Jouni Lohikoski 2014년 1월 20일
Thanks you both. It ("hold") should be mentioned in the stem3 help page.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by