How I can plot 2D figure (x,y) and add 3rd axis (velocity) by variation the color through the path?
13 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Luna
il 2 Gen 2019
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?
Risposta accettata
Star Strider
il 2 Gen 2019
Modificato: Star Strider
il 2 Gen 2019
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:
%20and%20add%203rd%20axis%20(velocity)%20by%20variation%20the%20color%20through%20the%20path%20-%202019%2001%2002.png)
2 Commenti
Più risposte (1)
Adam Danz
il 2 Gen 2019
Modificato: Adam Danz
il 2 Gen 2019
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 Commento
Adam Danz
il 2 Gen 2019
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.
Vedere anche
Categorie
Scopri di più su Axes Appearance in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
