Why the colour bar values are not representing the vectors on the plot?

Hi please see the attached image I am trying to use the colour bar to represent the U and V values from the graph but it is not representing it. can you guys guide me how to process it. Below is my current code.
Code:
files = { 'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_PIV\Data_Vector\Run 13-56-31.Adaptive PIV.6tvk0mbp\Export.6tvk0mbp.000000.csv'; ...
'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_B\Data_PIV\Data_Vector\Run 17-19-03.Adaptive PIV.6ul4ynyv\Export.6ul4ynyv.000000.csv'; ...
'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_B\Data_PIV\Data_Vector\Run 17-19-03.Adaptive PIV.6ul4ynyv\Export.6ul4ynyv.000000.csv'; ...
'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_A\Data_PIV\Data_Vector\Run 12-45-51.Adaptive PIV.6tskdx6a\Export.6tskdx6a.000000.csv'};
for i = 1:numel(files)
a = readtable(files{i});
X = a{:,5}/1000;
Y = a{:,6}/1000;
U = a{:,9};
V = a{:,10};
quiver(X, Y, U, V, 10);
xlim([0.1 0.4])
ylim([0.1 0.4])
colorbar('AxisLocation','in')
hold on
end
legend('DataLaserSheet-D','DataLaserSheet-C','DataLaserSheet-B','DataLaserSheet-A')

 Accepted Answer

Quiver plots and line plots do not use the colormap used by colorbar. You have to explicitly set both the axes 'colororder' and 'colormap' properties to the same map.
load('wind','x','y','u','v')
X = x(11:22,11:22,1);
Y = y(11:22,11:22,1);
hold on
quiver(X,Y,u(11:22,11:22,1),v(11:22,11:22,1));
quiver(X,Y,u(11:22,11:22,2),v(11:22,11:22,2));
quiver(X,Y,u(11:22,11:22,3),v(11:22,11:22,3));
quiver(X,Y,u(11:22,11:22,4),v(11:22,11:22,4));
colormap(get(gca,'colororder'))
colorbar
That said, that's really what the legend is for. In this case, the colorbar scale has no clear meaning.
Still, I'm not sure if that's what you're asking for. "I am trying to use the colour bar to represent the U and V values" might mean that you're trying to describe the vector fields using some solid color map (like pcolor() or something). If that's the case, you'll have to actually do that.
figure
hold on
pcolor(X,Y,hypot(u(11:22,11:22,1),v(11:22,11:22,1)));
quiver(X,Y,u(11:22,11:22,1),v(11:22,11:22,1));
shading interp
colormap(gray(256))
colorbar
Note that I only plotted one quiver plot and the pcolor() plot shows only the vector magnitude. Trying to put more things into the same plot should raise the question of how they're expected to be represented clearly.
If you want to represent the vector field (both components) with pcolor(), then you might want to look at this discussion:

5 Comments

Please see the attached file. I am plotting
a = readtables('filename.csv);
X = a{:,5}/1000;
Y = a{:,6}/1000;
U = a{:,9};
V = a{:,10};
quiver(X, Y, U, V, 10);
basically U and V have their values and I want to see the range of them values on the bar. Is it possible ? So far I am just failing trying different things
In the given quiver plot, there is no colored representation of either U or V. The colormap wouldn't refer to anything that's visible. Even if it did, it can only refer to one thing.
a = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/854510/Export.6tvk0mbp.000085.csv');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
X = a{:,5}/1000;
Y = a{:,6}/1000;
U = a{:,9};
V = a{:,10};
ru = [min(U) max(U)] % range of U
ru = 1×2
-9.4214 12.5737
rv = [min(V) max(V)] % range of V
rv = 1×2
-9.4879 13.3122
Say you set up a colorbar and set its extents to match the range of either U, V, or some combination of their ranges.
quiver(X, Y, U, V, 10);
colorbar
%caxis(ru) % set to the range of U
%caxis(rv) % set to the range of V
caxis([min(ru(1),rv(1)) max(ru(2),rv(2))]) % set to the union of ru,rv
colormap(spring) % to avoid confusing colors
The colorbar limits are set, but they don't correspond to anything in the plot. You have to pick something. You can do something with pcolor() like I suggested, but obviously if you plot a pcolor map of U, you can't plot a pcolor map of V without covering up the other one. So you'd ether have to pick one, or plot the magnitude like the example above.
As far as I know, quiver() doesn't support colormapping. There is this thing on the File Exchange:
though I haven't tried it. If that works, you could colormap the quiver plot itself and avoid the use of pcolor(), but you'd still only be able to describe one thing at a time (i.e. magnitude).
Please see the attached file. i gave up on mathlab and tried on python and it picked upt the colour range automatically as well as better representation of the vector field. I wish I can do the same thing with the matlab easily as my project has to be hand in with matlab. I will try with the colour thing!!!
Code is below:
import numpy as np
import math
import matplotlib.pyplot as plt
import pandas as pd
csvPath = r'C:\Users\40085772\Desktop\\'
filename = 'Export.csv'
df = pd.read_csv(csvPath + filename, skiprows=8)
X = df['x (mm)[mm]']
Y = df['y (mm)[mm]']
U = df['U[m/s]']
V = df['V[m/s]']
M = np.hypot(U, V)
plt.figure()
Q = plt.quiver(X, Y, U, V, M)
plt.colorbar()
plt.show()
quiverC2D(X, Y, U, V, 10); that worked as well! cheers thanks alot for your time and effort
I'm glad that tool worked out. I was worried you might have issues with such a large dataset. Getting good visualization out of quiver plots is kind of maddening, as you can see.

Sign in to comment.

More Answers (0)

Asked:

on 5 Jan 2022

Commented:

DGM
on 6 Jan 2022

Community Treasure Hunt

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

Start Hunting!