Interpolation of a scatter plot

22 vues (au cours des 30 derniers jours)
Yannick Tabot Njami
Yannick Tabot Njami le 16 Juin 2019
Hello ;I would be most grateful if someone could give me a helping hand. i have a scatter plot where the the third variable is velocity color coded for each pair of (x,y)I woud like to interpolate this plot to have more points.below is a figure of what i have and my code as well . thanks in advance.
the interpolated points are the red piont of the second figure is having just 9 pionts. i would like to apply that to the first figure which is what i have .
plotTypes = {'mean_velocity','std_velocity','mean_pd', 'std_pd'};
for i = 1: length(plotTypes)
figure
plotType = plotTypes{i};
%subplot(1,length(plotTypes), i);
hold all ;
legendLabels = cell(1,length(plotData));
for j = 1:length(plotData)
xPlot= plotData{j}.data(:, idx.x);
yPlot= plotData{j}.data(:, idx.(plotTypes{i}));
plot(xPlot,yPlot, '-o','color',k(j,:));
legendLabels{j} = ['Y = ', num2str(plotData{j}.y)];
end
legend(legendLabels);
title(plotTypes{i}, 'Interpreter', 'none');
xlabel('x position')
ylabel(plotTypes{i}, 'Interpreter', 'none')
end
%% Generating the 3D color plot
% figure
for i = 1: length(plotTypes)
figure
legendLabels = cell(1,length(plotData));
% subplot(1,length(plotTypes), i);
hold all
for N=1:size(plotData,2)
pz =150;
plotData{1,N}.data();
x=plotData{1,N}.data(:,1);
y= plotData{1,N}.data(:,2);
%if i ==3
% z= plotData{1,N}.data(:,(i+2));
%end% Z(:,N) =plotData{1,N}.data(:,(i+2));
scatter(x,y,pz,plotData{1,N}.data(:,(i+2)),'filled')
legendLabels{N} = ['Y = ', num2str(plotData{N}.y)];
end
%%Interpolation into a gridded data
%F =scatteredInterpolant(x,y,Z);
%[xq,yq]=meshgrid(linspace(20,230,100));
%cq =F(xq,yq);
%h=pcolor(xq,yq,cq)
%legendLabels{N} = ['Y = ', num2str(plotData{N}.y)];
legend(legendLabels);
title(['3D plot of position against ',plotTypes{i}], 'Interpreter', 'none')
xlabel('X(mm)')
ylabel('Y(mm)')
H = colorbar;
ylabel(H, plotTypes{i});
end
  1 commentaire
Akira Agata
Akira Agata le 17 Juin 2019
Seems that interp2 function would be one possible solution.

Connectez-vous pour commenter.

Réponse acceptée

Akira Agata
Akira Agata le 18 Juin 2019
Seems that scatteredInterpolant function would be better. Here is an example.
% Read data file
T = readtable('data.xlsx');
% Make interpolation function F = f(x,y)
F = scatteredInterpolant(T.xcoordinate,T.ycoordinate,T.mean_velocity);
% Make a grid points (20-by-20, for example)
[xmin,xmax] = bounds(T.xcoordinate);
[ymin,ymax] = bounds(T.ycoordinate);
[xGrid,yGrid] = meshgrid(linspace(xmin,xmax,20),linspace(ymin,ymax,20));
% Calculate interpolated value for each grid point
zGrid = F(xGrid,yGrid);
% Visualize the result
figure
scatter(xGrid(:),yGrid(:),[],zGrid(:),'filled')
colorbar
scatter.png
  1 commentaire
Yannick Tabot Njami
Yannick Tabot Njami le 18 Juin 2019
Thank you very much Mr Agata, you solve my problem .i appreciate you !

Connectez-vous pour commenter.

Plus de réponses (1)

KSSV
KSSV le 17 Juin 2019
Read about interp2, griddata. If not working attach your data.
  1 commentaire
Yannick Tabot Njami
Yannick Tabot Njami le 17 Juin 2019
%
Bellow is my entire code And attached you would fine my data as well .plotData{1,4}. has 7 rows due to error that why at the centere of the color plot there is a missing point there . The rest are all 8 rows. just a hint.
%%This Script selects file of of specific format(.txt) and
%% mport them each for data analysis.
%% the Analyzed data are then plotted as per requirement
clear all ;cla;
%% processing the parent folder
myfolder ='C:\Users\TABOT\Desktop\Windkanal_Data\Yannick\Yannick_2019_01_03';
%% checking to snsure that folder actually exist
if~isdir(myfolder)
Error_message =sprintf('Error,Folder not found:\n %s',myfolder);
end
%% getting list of all files with File pattern .txt
filepattern =fullfile(myfolder,'*.txt');
txtfiles=dir(filepattern);
%% sorting out the zero measurement and calculating Time sequence ,samples and mean voltage
zerofile =fullfile(myfolder,'20190301_00_20_20.txt');
offsetdata =importdata(zerofile);
zerovoltage =offsetdata.data(:,2);
mean_Uo =mean(zerovoltage);
%% ignoring the off set file after being used
txtfiles(1)=[];
%% preallocating memeory
mean_pd = zeros(length(txtfiles),1);
mean_velocity =zeros(length(txtfiles),1);
std_pd=zeros(length(txtfiles),1);
std_velocity =zeros(length(txtfiles),1);
velocity =zeros(length(txtfiles),1);
mean_voltage =zeros(length(txtfiles),1);
pd =zeros(length(zerovoltage),1);
Non_Avg_table = zeros(length(zerovoltage),5);
velocities =zeros(length(zerovoltage),1);
all_nonaveragetable(1,64) =struct('table',[],'x_position',[],'y_position',[]);
%% looping over each file in the directory txtfiles
for k =1 : length(txtfiles)
% getting the name of each file base on its x and y coordinates
% and importing the content of the file current file
fn_parts =split(txtfiles(k).name,{'_','.'});
coor_x =str2double(fn_parts{3});
coor_y =str2double(fn_parts{4});
filecontents =importdata(fullfile(txtfiles(k).folder,txtfiles(k).name));
%% referring to voltage and samples of each file consisting of a struture and sample value
voltage =filecontents.data(:,2);
samples =filecontents.data(:,1);
time_sequence =samples/1000;
for ii=1: length(voltage)
%% Looping over the voltage and calculating pd and velocities respectively
pd(ii)=abs( 474.6*(voltage(ii)-mean_Uo));
velocities(ii)=sqrt(2*pd(ii)/1.2);
end
%% calculating the various mean and standard deviation of the respective parameters
mean_pd(k) = mean(pd);
velocity(k) = sqrt(2*mean_pd(k)/1.2);
mean_voltage(k) = mean(voltage);
mean_velocity(k) = mean(velocities);
std_velocity(k) =std(velocities);
std_pd(k)=std(pd);
%% Generating table of non-average values and forming a structure of 1x 64
%% of 3 fields
Non_Avg_table= table(samples,time_sequence,voltage ,velocities,pd);
all_nonaveragetable(k).table= Non_Avg_table;
%%getting the coordinate of each point
all_nonaveragetable(k).x_position=coor_x;
all_nonaveragetable(k).y_position=coor_y;
end
%% Extracting the x and y Coordinate and transposing
xcoordinate =(extractfield(all_nonaveragetable,'x_position'))';
ycoordinate =(extractfield(all_nonaveragetable,'y_position'))';
%% Generating the table of average values
Average_Table = table(xcoordinate,ycoordinate,mean_velocity,std_velocity, mean_pd ,std_pd);
[data,txt] =xlsread('data.xlsx');
idx = struct('x',1, 'y',2, 'mean_velocity', 3, 'std_velocity', 4, 'mean_pd', 5, 'std_pd', 6);
%% Gathering and sorting data
plotTypes = {'mean_velocity','std_velocity','mean_pd', 'std_pd'};
% stepval = 30;
yValues = [20 50 80 110 140 170 200 230];
indextoFind =cell(1,length(yValues));
plotData = cell(1,length(yValues));
for i= 1:length(yValues)
foundIndex = find(data(:, idx.y) == yValues(i));
tempData = sortrows(data(foundIndex,:)); %% Get all data and sort row-wise for each yValues
plotData{i} = struct('y', yValues(i), 'data',tempData) ;%% here store data to be plotted
end
%% Plot Data
k =[1 ,0 ,0 ;0, 1, 0 ;0 ,1 ,.5;.5 .5 .5 ;.1 ,.1, .25; 1 ,1, 0; 0, 0,1;0 ,0.5 ,1];
% figure
for i = 1: length(plotTypes)
figure
plotType = plotTypes{i};
% subplot(1,length(plotTypes), i);
hold all ;
legendLabels = cell(1,length(plotData));
for j = 1:length(plotData)
xPlot= plotData{j}.data(:, idx.x);
yPlot= plotData{j}.data(:, idx.(plotTypes{i}));
plot(xPlot,yPlot, '-o','color',k(j,:));
legendLabels{j} = ['Y = ', num2str(plotData{j}.y)];
end
legend(legendLabels);
title(plotTypes{i}, 'Interpreter', 'none');
xlabel('x position')
ylabel(plotTypes{i}, 'Interpreter', 'none')
end
%% Generating the 3D color plot
% figure
for i = 1: length(plotTypes)
figure
legendLabels = cell(1,length(plotData));
% subplot(1,length(plotTypes), i);
hold all
for N=1:size(plotData,2)
pz =150;
plotData{1,N}.data();
x=plotData{1,N}.data(:,1);
y= plotData{1,N}.data(:,2);
% if i ==3
% z= plotData{1,N}.data(:,(i+2));
%
% end% Z(:,N) =plotData{1,N}.data(:,(i+2));
% %
scatter(x,y,pz,plotData{1,N}.data(:,(i+2)),'filled')
legendLabels{N} = ['Y = ', num2str(plotData{N}.y)];
end
%%Interpolation into a gridded data
% F =scatteredInterpolant(x,y,Z);
%
% [xq,yq]=meshgrid(linspace(20,230,100));
% cq =F(xq,yq);
% % h=pcolor(xq,yq,cq)
%
% legendLabels{N} = ['Y = ', num2str(plotData{N}.y)];
legend(legendLabels);
title(['3D plot of position against ',plotTypes{i}], 'Interpreter', 'none')
xlabel('X(mm)')
ylabel('Y(mm)')
H = colorbar;
ylabel(H, plotTypes{i});
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by