how to plot two graphs in one plot

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-23
编辑:Jonas 2021-4-23
if you just want to change the appearance for figure export you could just flip the yticklabels
Do you want the colored surface to flip vertically or do you want the surface and quiver plots to remain how they are but only flip the y-ticks?
i want the surface and quiver plot to remain same but want to only flip y ticks for recatangle
then maybe
yticklabels(flip(yticklabels()))
is what you want
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]

请先登录,再进行评论。

 采纳的回答

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 个评论

Thank you for your kindful information @Adam Danz. I am trying to implement the same thing to other code of mine but it not giving the correct plot. Please find below the code for reference and also the attached data file. Kindly help me for the same mentioned above problem . . Thank You @Adam Danz
%------------CODE---------------------------------%
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
%-----------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',[0 0 53.5 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',[0 40 53.5 5],'FaceColor',[0.502 0.502 0.502])
text (23,42.5,'Piston','Color','black','FontSize',14)
yv=yline(1.2,'r','TDC')
yv.LineWidth = 1;
yv.LabelHorizontalAlignment='center'
yv.LabelVerticalAlignment='middle'
hold on
magnitude=sqrt(avgu.^2+avgv.^2);
magnitude=reshape(magnitude,[13,20]);
[X,Y]=meshgrid(11.461148:1.580848000000000:41.497260000000018, 9.975784000000001:1.564829000000000:28.753731999999996);
contourf(X,Y,magnitude,100,'linestyle','none')
hold on
quiver(x,y,avgu,avgv,'color','black');
set(gca,'ydir','reverse');
axis equal
axis on
c = colorbar;
c.Label.String = 'V [m/s]'
c.Label.FontSize =14;
c.FontWeight = 'normal'
hold off
%--------------END OF ENSEMBLE AVAERAGE---------%
Output:
I don't see where you have made the changes I suggested in my answer.
Is there something unclear in my answer? I won't do it for you but I can help you understand what you need to do if there's something unclear in my answer.
After your saying i edited the first mentioned code in question and i was able to get the required output but when i tried to do the same thing in above mentioned code in comment section i was not able to get it.. Kindly help me in getting the output. . @Adam Danz..... I have attached edited but i am not getting the plot after follwowing your instructions.
clear all;close all;clc
format long;
%--------Reading velocity data-------%
fid=fopen('50112.dat');
fulltext=textscan(fid,'%s','delimiter','\n');
numberofgridpoints=length(fulltext{1});
fclose(fid);
n=numberofgridpoints/200;
fid=fopen('50112.dat');
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
y=flipud(y)
y=reshape(y,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',[0 0 53.5 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',[0 40 53.5 5],'FaceColor',[0.502 0.502 0.502])
text (23,42.5,'Piston','Color','black','FontSize',14)
yv=yline(1.2,'r','TDC')
yv.LineWidth = 1;
yv.LabelHorizontalAlignment='center'
yv.LabelVerticalAlignment='middle'
hold on
magnitude=sqrt(avgu.^2+avgv.^2);
magnitude=reshape(magnitude,[13,20]);
[X,Y]=meshgrid(11.461148:1.580848000000000:41.497260000000018, 9.975784000000001:1.564829000000000:28.753731999999996);
Y=flipud(Y)
contourf(X,Y,magnitude,100,'linestyle','none')
hold on
[theta, radius] = cart2pol(avgu,avgv); % <--- add this
[avgu, avgv] = pol2cart(-theta, radius); % <--- add this
quiver(x,y,avgu,avgv,'color','black');
axis equal
axis on
c = colorbar;
c.Label.String = 'V [m/s]'
c.Label.FontSize =14;
c.FontWeight = 'normal'
hold off
%--------------END OF ENSEMBLE AVAERAGE---------%
Output:
First, in future posts, please format your code using the [>] button (see help). I've formatted your code above for you.
Second, when I run your code I get the error (below). Is '50112.dat' exactly the same as 50112.txt?
Index in position 2 is invalid. Array indices must be positive integers or logical values.
Error in temp (line 17)
u(i,j)=datamatrix(3,d);
Third, you've made lots of changes from the original code you shared. My answer applies to the original code. You didn't add the lines in the correct place.
For example, look at step 2 in my answer, do you see the "<--- add this" ?
Y = flipud(Y); % <--- add this
contourf(X,Y,magnitude,100,'linestyle','none')
That means you're supposed to add it just before the contourf() line....
In step 3, there are three "<--- add this" . Make sure you're adding them in the right place. Try again.
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');
I finally got the required plot ,Thank you @Adam Danz .
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 个)

类别

帮助中心File Exchange 中查找有关 Vector Fields 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by