Hello everyone,
I have the following arrays: spatial_grid(30001x1 elements), physical_time_elastic (2431x1 elements), and data_set_elastic (30001x4x2431 elements). What I want to is to plot a something like the figures that appears here: https://blogs.mathworks.com/steve/2016/04/25/clim-caxis-imshow-and-imagesc/. In my case, the x-th axis will be spatial_grid, the y-th axis the physical_time_elastic array, and the magnitude that will be encoded by colors will data_set_elastic (:,2,i), where i runs over the elements of physical_time_elastic (note that length(physical_time_elastic)=2431, as the number of elements of data_set_elastic in the third dimension). For each i, therefore, I have the value that corresponds to (spatial_grid, physical_time_elastic). What I have tried to do is the following, but it does not work:
outputdir='Figures';
u1=figure(1)
for i=1:length(physical_time_elastic)
image(spatial_grid.*(10^6),physical_time_elastic(i).*(10^(12)),data_set_elastic(:,2,i))
end
colormap jet;
axis xy;
clr1=colorbar;
xlabel('Track position, $x \, \, \left( \mu\mathrm{m} \right)$','FontSize',14,'interpreter','latex')
ylabel('Time, $t \, \, \left( \mathrm{ps} \right)$','FontSize',14,'interpreter','latex')
ylabel(clr1,'$m_x$','Interpreter','Latex','FontSize',14);
t1=title('Elastic scattering, $w_{1,2}=1/2$','FontSize',14,'interpreter','latex')
set(t1,'interpreter','latex','FontSize',14)
set(gca,'TickLabelInterpreter','latex','FontSize',14)
set(u1,'Units','Inches');
posu1=get(u1,'Position');
set(u1,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[posu1(3),posu1(4)])
saveas(gcf,fullfile(outputdir,['In-Plane_Magnetization_Distribution_Elastic_Scattering.pdf']))
Any idea? I have attached a sample of what I get. The values that appears in the colorbar are completly wrong, because the variable that it is encoded in the color has values between -1 and +1. Also the track position begins in 0 and goes to a value like 3000, increasing its value linearly.

 채택된 답변

Joseph Cheng
Joseph Cheng 2020년 2월 20일
편집: Joseph Cheng 2020년 2월 20일

1 개 추천

I'm puzzeled by your first portion and the need for a for loop. unless you have a hold somewhere the images shouldn't be stacking and you're only doing it per item in physical_time_eleastic so you're imaging a single index at a time but without hold.... i'm not sure what is going on there.
outputdir='Figures';
u1=figure(1)
for i=1:length(physical_time_elastic)
image(spatial_grid.*(10^6),physical_time_elastic(i).*(10^(12)),data_set_elastic(:,2,i))
end
colormap jet;
in each loop you are imaging a 1x30001 image and overwriting it. as well as axes are not set correctly as your x and y do not match column row of data_set_elastic. so something else is going on there.
why don't you image the whole 2nd dimension?
here is dummy data
spatial_grid = [0:300]';
physical_time_elastic = [0:10:1000]';
data_set_elastic = 2*rand(301,3,101)-1;
u1=figure(1)
% hold on
% for i=1:length(physical_time_elastic)
% image(spatial_grid.*(10^6),physical_time_elastic(i).*(10^(12)),data_set_elastic(:,2,i))
% end
imagesc(spatial_grid,physical_time_elastic,squeeze(data_set_elastic(:,2,:))');
colormap jet;
colorbar
note squeezing data_set_elastic(:,2,:) which for you is (30001 x 1 x 2431 elements) would turn out to be (30001 x 2431 elements) which then doesn't match your 30001 for x (ie columns of the image) and 2431 for y. which is why there is a transpose of squeezing data_set_elastic(:,2,:)' .
also i'm not sure image deals with negative values which you should then use imagesc or other functions which you define the color scale.

댓글 수: 7

Roderick
Roderick 2020년 2월 20일
편집: Roderick 2020년 2월 20일
Hey! Thank you very much for your answer. Definitely it allowed me to do a step further. Certainly I was completely misunderstanding working with three-dimensional array, and the for loop was nothing but a demonstration of that. I have done the following now, instead using squeeze I have isolated the data from the second element of the second dimension of my array data_set_elastic, but of course your approach is correct:
mx_elastic=zeros(length(spatial_grid),length(physical_time_elastic));
for i=1:length(physical_time_elastic)
for j=1:length(spatial_grid)
mx_elastic(j,i)=data_set_elastic(j,2,i);
end
end
u1=figure(1)
imagesc(spatial_grid.*(10^6),physical_time_elastic.*(10^(12)),mx_elastic')
clr1=colorbar;
xlabel('Track position, $x \, \, \left( \mu\mathrm{m} \right)$','FontSize',14,'interpreter','latex')
ylabel('Time, $t \, \, \left( \mathrm{ps} \right)$','FontSize',14,'interpreter','latex')
set(gca,'ydir','reverse')
ylabel(clr1,'$m_x$','Interpreter','Latex','FontSize',14);
t1=title('Elastic scattering, $w_{1,2}=1/2$','FontSize',14,'interpreter','latex')
set(t1,'interpreter','latex','FontSize',14)
set(gca,'TickLabelInterpreter','latex','FontSize',14)
set(u1,'Units','Inches');
posu1=get(u1,'Position');
set(u1,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[posu1(3),posu1(4)])
saveas(gcf,fullfile(outputdir,['In-Plane_Magnetization_Distribution_Elastic_Scattering.pdf']))
I have attached my current result, pretty the final stage. Nevertheless, I have transposed my mx_elastic, as you have told me. The problem is that now my data is turned in a strange way, because the y-th axis of my plot is flipped, the values and labels of the values. Why this happens and how I could fix that? Moreover, I have another question related to this plot. I am not very happy with the labels of my colorbar, because I want them to be [-1:0.5:1], where -1 corresponds to the lowest value of my plot and +1 to the highest. The problem is that I usually fix this referring to the problematic axis, but here I have used two times ylabel and I am not sure how to manipulate only the one related to the colorbar. My usual approach is as follows (it is an example):
set(gca,'TickLabelInterpreter','latex','FontSize',18)
set(gca,'XTick',0:vminus/5:vminus)
set(gca,'XTickLabel',{'0','0.2','0.4','0.6','0.8','1'})
Any idea? Thank for your help!
to set the y axis to be normal cartesian (ie [0,0] being lower left corner) use:
set(gca,'Ydir','normal')
for the scale you can set that as the 4th term max and min value (refer to the help portion of of imagesc) but then again if you take a look at the example Jan has in this post https://www.mathworks.com/matlabcentral/answers/58634-setting-ticks-in-a-colorbar
you'll be able to set ticks and limits of the color bar.
Everything is all right. Nevertheless, concerning the change of the labels of the colorbar, I have followed the method of your recommended post. In its spirit, I have added the following to my code:
clr1=colorbar('XTickLabel',{'-1','-0.5','0','0.5','1'},'XTick',[-1:0.5:1]);
This does not display the +1 and -1 in the colorbar axis (see my figure). Any idea? Thank you very much for your instructive answers!
that in itself isn't setting the limits, the line above it in Jan's post
AxesH = axes('CLim', [-12, 12]);
which can also be done using the function caxis() https://www.mathworks.com/help/matlab/ref/caxis.html. the items there are only the text values in the colorbar object.
Thank you for your comment. I have tried it, and I obtain something very strange.
u1=figure(1)
imagesc(spatial_grid.*(10^6),physical_time_elastic.*(10^(12)),mx_elastic')
AxesH=axes('CLim',[-1, 1]);
clr1=colorbar('peer',AxesH,...
'YTickLabel',{'-1','-0.5','0','0.5','1'},'YTick',[-1:0.5:1]);
% clr1=colorbar(axes('CLim',[-1,1]),'YTickLabel',{'-1','-0.5','0','0.5','1'},'YTick',[-1:0.5:1]);
xlabel('Track position, $x \, \, \left( \mu\mathrm{m} \right)$','FontSize',14,'interpreter','latex')
ylabel('Time, $t \, \, \left( \mathrm{ps} \right)$','FontSize',14,'interpreter','latex')
ylabel(clr1,'$m_x$','Interpreter','Latex','FontSize',14);
t1=title('Elastic collision, $w_{\mathrm{overall}}=1$','FontSize',14,'interpreter','latex')
set(t1,'interpreter','latex','FontSize',14)
set(gca,'TickLabelInterpreter','latex','FontSize',14)
set(gca,'Ydir','normal')
set(u1,'Units','Inches');
posu1=get(u1,'Position');
set(u1,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[posu1(3),posu1(4)])
saveas(gcf,fullfile(outputdir,['In-Plane_Magnetization_Distribution_Elastic_Collision.pdf']))
Maybe I am still misunderstanding something from the Jan's post.
you shouldn't just implement but digest what is going on. by doing axes afer imagesc you created a new axes inside the figure.
again just use caxis() function like you would xlim, ylim or zlim to set the limits in a plot if you are not familiar with matlab graphical handles.
see example:
figure(1);
dummydata = randi(10,10,10);
subplot(121),imagesc(dummydata )
caxis([0 10])
colorbar
subplot(122),imagesc(dummydata ),caxis([0 50])
colorbar
Roderick
Roderick 2020년 2월 20일
I see your point about creating additional axis. The function caxis works as expected. Nothing more to ask, thank you very much for everything.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 White에 대해 자세히 알아보기

질문:

2020년 2월 20일

댓글:

2020년 2월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by