How I can plot 2D figure (x,y) and add 3rd axis (velocity) by variation the color through the path?

조회 수: 67 (최근 30일)
I am trying to plot a 2D figure (x,y) and add 3rd axis (velocity) by variation the color through the path. I did code and add related colorbar. In fact I would like to show each point by corresponding velocity value by changing color that colorbar already shows. However, I don't have any idea how I can vary the velocity. My solution that I already used not show the exact velocity values. I also attached the data. Any idea? Thanks in advance
Elnaz
close all
clear all
clc
%%
load data3
x =data (:,1); %X
y =data (:,2); %Y
z =data (:,3); %Mean velocity
z_min=min(min(z));
z_max=max(max(z));
x_min=min(min(x));
x_max=max(max(x));
y_min=min(min(y));
y_max=max(max(y));
dt=(6.95:0.05:19.5);
figure % new figure
title ('Trajectory of Particles')
grid on
xlabel('X (m)')
yyaxis right
plot(x,y,'k--o','LineWidth',2,'MarkerEdgeColor','r','MarkerFaceColor','none');
ylabel('Y(m)')
yyaxis left
plot(x,z,'w');
ylabel('Mean Velocity(m/sec)')
c = colorbar;
c.Label.String = 'Mean Velocity(m/sec)';
caxis([z_min z_max]);
  댓글 수: 2
Luna
Luna 2019년 1월 2일
that kind of colorbar mostly used in contour/surface plots which have x,y,z data.
You can maybe use meshgrid for that but I really didn't understand what exactly you want to plot. Do you have any sample figure or image as a desired result?
Elnaz P
Elnaz P 2019년 1월 2일
Please see the attached photo, this is a plot that I did. 2D path and I would like to see each point by corresponding velocity value in color. For instance, first point (x1,y1) the velocity is 100 so this point should be in blue. Second one (x2,y2), the velocity is 600 so second point should be in yellow.
Example.jpg

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2019년 1월 2일
편집: Star Strider 2019년 1월 2일
One possibility is to change the second plot call to scatter, since the scatter functions allow color-coded markers.
Try this:
figure % new figure
title ('Trajectory of Particles')
grid on
xlabel('X (m)')
yyaxis right
plot(x,y,'k--o','LineWidth',2,'MarkerEdgeColor','r','MarkerFaceColor','none');
ylabel('Y(m)')
yyaxis left
scatter(x,y, 40, z, 'filled'); % <— CHANGE THE SECOND ‘plot’ CALLL TO ‘scatter’
ylabel('Mean Velocity(m/sec)')
c = colorbar;
c.Label.String = 'Mean Velocity(m/sec)';
caxis([z_min z_max]);
Here, the ‘z’ value colors the scatter markers. You will probably have to experiment with this to get the result you want. See the documentation on the scatter (link) function for more information on it.
EDIT —
Adding the plot:
How I can plot 2D figure (x,y) and add 3rd axis (velocity) by variation the color through the path - 2019 01 02.png

추가 답변 (1개)

Adam Danz
Adam Danz 2019년 1월 2일
편집: Adam Danz 2019년 1월 2일
This solution uses scatter() to add color to each marker based on the values in 'v'. At the beginning I create random fake data. You can determine how many color bins you'd like by changing 'nBins'. The higher the number, the more precise your color assigments will be.
% create fake data
x = 1:20;
y = rand(size(x));
v = rand(size(x))*90; % random velocity values
% number of color bins?
nBins = 100;
% define color map
% for a list of color maps: https://www.mathworks.com/help/matlab/ref/colormap.html#buc3wsn-6
cols = hot(nBins);
% determine color index of each point
[~, colorEdges, colorIdx] = histcounts(v, nBins);
% create figure
figure
axh = axes;
hold(axh, 'on')
% plot data with empty markers
plot(axh, x, y, 'k-o')
% add color
scatter(x,y,35, cols(colorIdx,:), 'filled')
% add colorbar
colormap(cols)
cb = colorbar('peer', axh);
caxis([colorEdges(1), colorEdges(end)])
  댓글 수: 1
Adam Danz
Adam Danz 2019년 1월 2일
This solution is an extension of Star Strider's suggestion which I saw after adding the solution. What differs is my use of histcounts() to assign the color values based on the velocity values.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by