필터 지우기
필터 지우기

how to plot two graphs in one plot

조회 수: 1 (최근 30일)
HARIKRISHNA B YADULADODDI
HARIKRISHNA B YADULADODDI 2021년 4월 23일
댓글: Adam Danz 2021년 4월 27일
I have created the following plot but i want to reverse the y direction(i,e top left corner should start with 50 in y direction) but if i reverse the y direction the vectors will point in downward direction and i dont want the vectors to point in downward direction if i reverse the y direction kindly help me . .
  댓글 수: 5
Jonas
Jonas 2021년 4월 24일
then maybe
yticklabels(flip(yticklabels()))
is what you want
Adam Danz
Adam Danz 2021년 4월 24일
HARIKRISHNA B YADULADODDI's duplicate question moved here
The Y-axis ticks go like 0,5,10- - - - - 50
I want the plot to remain as it is, but flip the ticks so that they go like 50 45 40 - - - - - - 5,0.
Can this be done? please find the code and file for reference.
clear all;close all;clc
format long
%--------Reading velocity data-------%
fid=fopen('50112.txt');
fulltext=textscan(fid,'%s','delimiter','\n');
numberofgridpoints=length(fulltext{1});
fclose(fid);
n=numberofgridpoints/200;
fid=fopen('50112.txt');
datamatrix=fscanf(fid,'%f%f%f%f',[4,inf]);
%-------stroing velocity-----------%
for i=1:200
c=i-1;
for j=1:n
d=(c*n)+j;
x(j)=datamatrix(1,j);
y(j)=datamatrix(2,j);
u(i,j)=datamatrix(3,d);
v(i,j)=datamatrix(4,d);
end
end
x1(1:13,1:20)=0;
y1(1:13,1:20)=0;
a=-19;
b=10;
for j=1:20
for i=1:13
x1(i,j)=a;
end
a=a+2;
end
x1=reshape(x1,1,260)
for i=1:13
for j=1:20
y1(i,j)=b;
end
b=b+1.92307692307;
end
y1=reshape(y1,1,260)
%-----------Ensemble mean------------%
addu(1:n)=0;
addv(1:n)=0;
for j=1:n
for i=1:200
addu(1,j)=addu(1,j)+u(i,j);
addv(1,j)=addv(1,j)+v(i,j);
end
avgu(1,j)=addu(1,j)/200;
avgv(1,j)=addv(1,j)/200;
end
%---------------------plot--------%
figure('name','ENSEMBLE AVARAGE FOR 50112')
rectangle('Position',[-26.75 0 53.55 50]);
xlabel('x (mm)')
ylabel('y (mm)')
axis equal
axis ([-25 25 0 50])
set(gca,'ytick',0:10:50)
axis tight
hold on
rectangle('Position',[-26.75 40 53.5 5],'FaceColor',[0.502 0.502 0.502])
text (-2.5,42.5,'Piston','Color','black','FontSize',14)
y=yline(1.2,'r','TDC')
y.LineWidth = 1;
y.LabelHorizontalAlignment='center'
y.LabelVerticalAlignment='middle'
hold on
magnitude=sqrt(avgu.^2+avgv.^2);
magnitude=reshape(magnitude,[13,20]);
[X,Y]=meshgrid(-19:2:19,10:1.9230769:33.0769230);
contourf(X,Y,magnitude,100,'linestyle','none')
hold on
h=quiver(x1,y1,avgu,avgv,'color','black');
set(gca,'ydir','reverse')
axis equal
c = colorbar;
c.Label.String = 'V [m/s]'
c.Label.FontSize =14;
c.FontWeight = 'normal'
hold off
output: [see figure in main question]

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

채택된 답변

Adam Danz
Adam Danz 2021년 4월 24일
편집: Adam Danz 2021년 4월 24일
You're reversing the y-axis with this line in your code.
set(gca,'ydir','reverse')
If you want the y-axis direction to be normal but leave the contour and quiver plots in their current orientation, that means the data are in the wrong order. Ideally you would alter the code so that the matrices and vectors are constructed in the correct order. Bu here's a way to flip the data as it is.
  1. Remove the line that reverses the y-direction of the y-axis.
  2. Flip the y-coordinates for the contour plot (see below)
  3. Flip the y-coordinates and the y-vector-component of the quiver plots in polar coordinates (see below)
Step 2:
Y = flipud(Y); % <--- add this
contourf(X,Y,magnitude,100,'linestyle','none')
Step 3:
for i=1:13
for j=1:20
y1(i,j)=b;
end
b=b+1.92307692307;
end
y1 = flipud(y1); % <--- add this
y1=reshape(y1,1,260)
...
...
...
[theta, radius] = cart2pol(avgu,avgv); % <--- add this
[avgu, avgv] = pol2cart(-theta, radius); % <--- add this
h=quiver(x1,y1,avgu,avgv,'color','black');
Result
  댓글 수: 6
HARIKRISHNA B YADULADODDI
HARIKRISHNA B YADULADODDI 2021년 4월 27일
I finally got the required plot ,Thank you @Adam Danz .
Adam Danz
Adam Danz 2021년 4월 27일
Great!!
Here's what I did with the attached data. Look for the 5 leftward arrows "% <------ "
%--------Reading velocity data-------%
fid=fopen('50112.txt');
fulltext=textscan(fid,'%s','delimiter','\n');
numberofgridpoints=length(fulltext{1});
fclose(fid);
n=numberofgridpoints/200;
fid=fopen('50112.txt');
datamatrix=fscanf(fid,'%f%f%f%f',[4,inf]);
%-------stroing velocity-----------%
for i=1:200
c=i-1;
for j=1:n
d=(c*n)+j;
x(j)=datamatrix(1,j);
y(j)=datamatrix(2,j);
u(i,j)=datamatrix(3,d);
v(i,j)=datamatrix(4,d);
end
end
x1(1:13,1:20)=0;
y1(1:13,1:20)=0;
a=-19;
b=10;
for j=1:20
for i=1:13
x1(i,j)=a;
end
a=a+2;
end
x1=reshape(x1,1,260);
for i=1:13
for j=1:20
y1(i,j)=b;
end
b=b+1.92307692307;
end
y1 = flipud(y1); % <------ ADD THIS
y1=reshape(y1,1,260);
%-----------Ensemble mean------------%
addu(1:n)=0;
addv(1:n)=0;
for j=1:n
for i=1:200
addu(1,j)=addu(1,j)+u(i,j);
addv(1,j)=addv(1,j)+v(i,j);
end
avgu(1,j)=addu(1,j)/200;
avgv(1,j)=addv(1,j)/200;
end
%---------------------plot--------%
figure('name','ENSEMBLE AVARAGE FOR 50112')
rectangle('Position',[-26.75 0 53.55 50]);
xlabel('x (mm)')
ylabel('y (mm)')
axis equal
axis ([-25 25 0 50])
set(gca,'ytick',0:10:50)
axis tight
hold on
rectangle('Position',[-26.75 40 53.5 5],'FaceColor',[0.502 0.502 0.502])
text (-2.5,42.5,'Piston','Color','black','FontSize',14)
y=yline(1.2,'r','TDC');
y.LineWidth = 1;
y.LabelHorizontalAlignment='center';
y.LabelVerticalAlignment='middle';
hold on
magnitude=sqrt(avgu.^2+avgv.^2);
magnitude=reshape(magnitude,[13,20]);
[X,Y]=meshgrid(-19:2:19,10:1.9230769:33.0769230);
Y = flipud(Y); % <------ ADD THIS
contourf(X,Y,magnitude,100,'linestyle','none')
hold on
[theta, radius] = cart2pol(avgu,avgv); % <------ ADD THIS
[avgu, avgv] = pol2cart(-theta, radius); % <------ ADD THIS
h=quiver(x1,y1,avgu,avgv,'color','black');
% set(gca,'ydir','reverse') % <------ REMOVE THIS
axis equal
c = colorbar;
c.Label.String = 'V [m/s]';
c.Label.FontSize =14;
c.FontWeight = 'normal';
hold off

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by