필터 지우기
필터 지우기

How to fix the intervals of colorbar?

조회 수: 9 (최근 30일)
aka_manah
aka_manah 2023년 1월 11일
댓글: Cris LaPierre 2023년 1월 11일
Hello everyone,
I want to make a movie of points that moves into 2D space and those points have a value T that changes.
So, I have 3 matrices:
1) Xr(:,:) , Yr(:,:) that has the x,y vaules of each point at each time (rows represent points and columns represent time).
2) Tr(:,:) that has the Temperature of each point at each time (rows represent points and columns represent time).
I want to plot (with scatter) the point and make a colorbar that has static intervals for all times (not only the min, max value of colobar).
Here is my code:
%%
clc;clear;
%% define variables %%
l=5; % distance between source and wall
[x1,y1]=meshgrid(-1:0.1:1, -0:0.1:2);
u=zeros(size(x1,1),size(x1,2));
v=zeros(size(x1,1),size(x1,2));
%%
fid = fopen('initialParticlePotition.txt') ;
PP = textscan(fid,'%f %f %f ','HeaderLines',1) ;
PP = cell2mat(PP);
fclose(fid);
fid = fopen('resultsMat.txt') ;
r = textscan(fid,'%f %f %f %f %f','HeaderLines',1) ;
r = cell2mat(r);
fclose(fid);
fid = fopen('Parameters.txt') ;
Parameters = textscan(fid,'%f %f %f %f %f','HeaderLines',1) ;
Parameters = cell2mat(Parameters);
fclose(fid);
NTIMETOT=Parameters(1);
NPTOT= Parameters(2);
dt=Parameters(3);
ntimeW=Parameters(4);
q=Parameters(5);
Xr=zeros(NPTOT,ntimeW);
Yr=zeros(NPTOT,ntimeW);
dr=zeros(NPTOT,ntimeW);
Tr=zeros(NPTOT,ntimeW);
k=0;
for i=1:size(r,1)
Xr(i-k*NPTOT,k+1)=r(i,2);
Yr(i-k*NPTOT,k+1)=r(i,3);
dr(i-k*NPTOT,k+1)=r(i,4);
Tr(i-k*NPTOT,k+1)=r(i,5);
if (i==(k+1)*NPTOT)
k=k+1;
end
end
%%
for i=1:size(x1,1)
for j=1:size(x1,2)
u(i,j)=q/(2*pi)*x1(i,j)/(x1(i,j)^2+(y1(i,j)-l)^2)+q/(2*pi)*x1(i,j)/(x1(i,j)^2+(y1(i,j)+l)^2);
v(i,j)=q/(2*pi)*(y1(i,j)-l)/(x1(i,j)^2+(y1(i,j)-l)^2)+q/(2*pi)*(y1(i,j)+l)/(x1(i,j)^2+(y1(i,j)+l)^2);
end
end
figure;
i=0;
count=1;
for k=1:1:(ntimeW)
%Wipe the slate clean so we are plotting with a black figure
clf
i=i+1;
%Extract the data at the current time
t_k= (k)*dt*100;
%Plot the behind
c2= colorbar;
c2.Label.String = 'T';
c2.Limits=[min(Tr(:,:),[],'omitnan') max(Tr(:,:),[],'omitnan')];
hold on
axis equal
xlim([-1 1])
ylim([0 2])
quiver(x1,y1,u,v,'k')
xlabel(' X $[m]$ ','interpreter','latex','FontSize',15)
ylabel(' Y $[m]$ ','interpreter','latex','FontSize',15)
%Plot the current location of the particles
scatter(Xr(:,k),Yr(:,k),15,Tr(:,k),'filled','o') % plot all the particles at time k
%patches of particles
%force Matlab to draw the image at this point
movieVector(i)=getframe(gcf); % tha dwsei ena matrix poy perigrafei to image
end
%%Step 5: Save the Movie
myWriter= VideoWriter('curve');%,'MPEG-4'
myWriter.FrameRate=20;
%Open the VideoWriter object, write the movie, and close the file
open(myWriter);
writeVideo(myWriter,movieVector);
close(myWriter);
if you run it you will see that the colormap limits stay the same, but the intervals change accordinly to the value T of the points.
I want the colormap to be same on each time step and the color of the points to change according to the colormap and not the other way around.
Thank you in advance,
A hard working student.

답변 (1개)

Cris LaPierre
Cris LaPierre 2023년 1월 11일
I think you want to look into clim. I'd also suggest moving anything does not need to be redrawn everytime outside the for loop. Here's an example that displays the results here (no movie)
%%
clc;clear;
%% define variables %%
l=5; % distance between source and wall
[x1,y1]=meshgrid(-1:0.1:1, -0:0.1:2);
u=zeros(size(x1,1),size(x1,2));
v=zeros(size(x1,1),size(x1,2));
%%
fid = fopen('initialParticlePotition.txt') ;
PP = textscan(fid,'%f %f %f ','HeaderLines',1) ;
PP = cell2mat(PP);
fclose(fid);
fid = fopen('resultsMat.txt') ;
r = textscan(fid,'%f %f %f %f %f','HeaderLines',1) ;
r = cell2mat(r);
fclose(fid);
fid = fopen('Parameters.txt') ;
Parameters = textscan(fid,'%f %f %f %f %f','HeaderLines',1) ;
Parameters = cell2mat(Parameters);
fclose(fid);
NTIMETOT=Parameters(1);
NPTOT= Parameters(2);
dt=Parameters(3);
ntimeW=Parameters(4);
q=Parameters(5);
Xr=zeros(NPTOT,ntimeW);
Yr=zeros(NPTOT,ntimeW);
dr=zeros(NPTOT,ntimeW);
Tr=zeros(NPTOT,ntimeW);
k=0;
for i=1:size(r,1)
Xr(i-k*NPTOT,k+1)=r(i,2);
Yr(i-k*NPTOT,k+1)=r(i,3);
dr(i-k*NPTOT,k+1)=r(i,4);
Tr(i-k*NPTOT,k+1)=r(i,5);
if (i==(k+1)*NPTOT)
k=k+1;
end
end
%%
for i=1:size(x1,1)
for j=1:size(x1,2)
u(i,j)=q/(2*pi)*x1(i,j)/(x1(i,j)^2+(y1(i,j)-l)^2)+q/(2*pi)*x1(i,j)/(x1(i,j)^2+(y1(i,j)+l)^2);
v(i,j)=q/(2*pi)*(y1(i,j)-l)/(x1(i,j)^2+(y1(i,j)-l)^2)+q/(2*pi)*(y1(i,j)+l)/(x1(i,j)^2+(y1(i,j)+l)^2);
end
end
figure;
i=0;
count=1;
c2= colorbar;
c2.Label.String = 'T';
c2.Limits=[min(Tr(:,:),[],'omitnan') max(Tr(:,:),[],'omitnan')];
clim([80 170])
axis equal
xlim([-1 1])
ylim([0 2])
xlabel(' X $[m]$ ','interpreter','latex','FontSize',15)
ylabel(' Y $[m]$ ','interpreter','latex','FontSize',15)
for k=1:1:(ntimeW)
%Extract the data at the current time
t_k= (k)*dt*100;
%Plot the behind
hold on
quiver(x1,y1,u,v,'k')
%Plot the current location of the particles
scatter(Xr(:,k),Yr(:,k),15,Tr(:,k),'filled','o') % plot all the particles at time k
end
  댓글 수: 2
aka_manah
aka_manah 2023년 1월 11일
편집: aka_manah 2023년 1월 11일
Thanks for the response,
I want in each timestep to plot only the position of point in the current iteration, not all the position of the point, inorder to make a animation.
My problem is that i want a static colorbar that corresponse to all the T values of all point of all times (same as the colormap in your photo).
Cris LaPierre
Cris LaPierre 2023년 1월 11일
I understand, but animations do not show here, so I chose a way to represent it statically. You will have to modify the code to do what you want.
My code for this would look more like this. Note that I also changed the colormap, as yellow gets hard to see.
[x1,y1]=meshgrid(-1:0.1:1, -0:0.1:2);
u=zeros(size(x1,1),size(x1,2));
v=zeros(size(x1,1),size(x1,2));
%%
PP = readmatrix('initialParticlePotition.txt') ;
r = readmatrix('resultsMat.txt') ;
Parameters = readmatrix('Parameters.txt') ;
NTIMETOT=Parameters(1);
NPTOT= Parameters(2);
dt=Parameters(3);
ntimeW=Parameters(4);
q=Parameters(5);
Xr=zeros(NPTOT,ntimeW);
Yr=zeros(NPTOT,ntimeW);
dr=zeros(NPTOT,ntimeW);
Tr=zeros(NPTOT,ntimeW);
k=0;
for i=1:size(r,1)
Xr(i-k*NPTOT,k+1)=r(i,2);
Yr(i-k*NPTOT,k+1)=r(i,3);
dr(i-k*NPTOT,k+1)=r(i,4);
Tr(i-k*NPTOT,k+1)=r(i,5);
if (i==(k+1)*NPTOT)
k=k+1;
end
end
%%
for i=1:size(x1,1)
for j=1:size(x1,2)
u(i,j)=q/(2*pi)*x1(i,j)/(x1(i,j)^2+(y1(i,j)-l)^2)+q/(2*pi)*x1(i,j)/(x1(i,j)^2+(y1(i,j)+l)^2);
v(i,j)=q/(2*pi)*(y1(i,j)-l)/(x1(i,j)^2+(y1(i,j)-l)^2)+q/(2*pi)*(y1(i,j)+l)/(x1(i,j)^2+(y1(i,j)+l)^2);
end
end
figure;
xlim([-1 1])
ylim([0 2])
quiver(x1,y1,u,v,'k')
axis equal
hold on
s = scatter(Xr(:,1),Yr(:,1),15,Tr(:,1),'filled','o') % plot all the particles at time k
hold off
xlabel(' X $[m]$ ','interpreter','latex','FontSize',15)
ylabel(' Y $[m]$ ','interpreter','latex','FontSize',15)
colormap jet
c2= colorbar;
c2.Label.String = 'T';
c2.Limits=[min(Tr(:,:),[],'omitnan') max(Tr(:,:),[],'omitnan')];
clim([80 170])
movieVector(1)=getframe(gcf);
for k=2:1:(ntimeW)
% Plot the current location of the particles
s.XData = Xr(:,k);
s.YData = Yr(:,k);
s.CData = Tr(:,k); % plot all the particles at time k
movieVector(k)=getframe(gcf); % tha dwsei ena matrix poy perigrafei to image
end
%%Step 5: Save the Movie
myWriter= VideoWriter('curve');%,'MPEG-4'
myWriter.FrameRate=20;
%Open the VideoWriter object, write the movie, and close the file
open(myWriter);
writeVideo(myWriter,movieVector);
close(myWriter);

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

카테고리

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

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by