How to plot colorbar and plot size different?

Hi
When I plot colorbar, it reduces the size of my actual plot. I have 5 subplot and each has a separate colorbar. How to fix this issue?
I want to plot colorbar for each subplot and next to subplot. how can i solve it?
how can I adjust the ticks on colorbar (interval between colorbar ticks)?

 채택된 답변

Star Strider
Star Strider 2022년 9월 12일
One option would be to stretch the figure by adjusting the 'Position' property. It is possible to re-position it and stretch (or compress) it vertically as well, depending on what you want as the result. See the documentation on the Position property for a full description and details.
Example —
x = 0:999;
y = (1:5:25).'+sin((1:5:25).'*2*pi*x/20);
figure
for k = 1:5
subplot(1,5,k)
plot(x, y(k,:))
xlim([0 20])
colorbar
end
sgtitle('Original')
figure
for k = 1:5
subplot(1,5,k)
plot(x, y(k,:))
xlim([0 20])
colorbar
end
pos = get(gcf, 'Position');
set(gcf,'Position',pos+[-50 0 250 0]) % Shift Slightly Left & Stretch To Right
sgtitle('Stretched Width')
.

댓글 수: 8

@Star Strider getting error. I have attached my data, can you plz help to plot my data?
as (VD, Prob_vp0), (VD,Prob_vs0) and so on... for all five plots. VD should be on Y axis, each data has different x limits
What is the error? I have no idea, and may not be able to reproduce it.
My code should work with all recent MATLAB versions/releases.
I need more than the data, specificlally I need the code that plotted it, and how you implememted my code with it.
Nisar Ahmed
Nisar Ahmed 2022년 9월 12일
이동: Star Strider 2022년 9월 12일
@Star Strider Here is my code to obtain Prob_vp0, Prob_vs0, Prob_rho0.....and are plotted against VD as:
x2 = [1500:50:6000]';
Prob_vp0 = zeros(length(VD), length(x2));
x3 = [800:50:3800]';
Prob_vs0 = zeros(length(VD), length(x3));
x4 = [2000:10:2800]';
Prob_rho0 = zeros(length(VD), length(x4));
x5 = [20:1:64]';
Prob_qp0 = zeros(length(VD), length(x5));
x6 = [10:1:40]';
Prob_qs0 = zeros(length(VD), length(x6));
for i=1:length(VD)
Prob_vp0(i,:) = ksdensity(vp0(i,:),x2);
Prob_vp0(i,:) = Prob_vp0(i,:)/sum(Prob_vp0(i,:));
Prob_vs0(i,:) = ksdensity(vs0(i,:),x3);
Prob_vs0(i,:) = Prob_vs0(i,:)/sum(Prob_vs0(i,:));
Prob_rho0(i,:) = ksdensity(rho0(i,:),x4);
Prob_rho0(i,:) = Prob_rho0(i,:)/sum(Prob_rho0(i,:));
Prob_qp0(i,:) = ksdensity(qp0(i,:),x5);
Prob_qp0(i,:) = Prob_qp0(i,:)/sum(Prob_qp0(i,:));
Prob_qs0(i,:) = ksdensity(qs0(i,:),x6);
Prob_qs0(i,:) = Prob_qs0(i,:)/sum(Prob_qs0(i,:));
end
%subplot(151);
figure(1), imagesc([1500:50:6000], VD, Prob_vp0); xlabel('V_P (m/s)');
axis([1600 6000 1.888e+03 2.131e+03]); set(gca,'ytick',[1.888e+03:55:2.131e+03]);
set(gca,'xtick',[1600:4400:6000]); set(gca,'FontSize',9); ylabel('Depth (m)');
colorbar; caxis([0 0.05]);
set(gcf, 'position', [500 285 200 400]);
figure(2), imagesc([800:50:3800], VD, Prob_vs0); set(gca,'FontSize',9);
ylabel('Depth (m)'); set(gca,'xtick',[800:3000:3800]); xlabel('V_S (m/s)');
set(gca,'ytick',[1.888e+03:55:2.131e+03]); colorbar;
colorbar; caxis([0 0.15]);
set(gcf, 'position', [600 285 200 400]);
figure(3), imagesc([2000:10:2800], VD, Prob_rho0); set(gca,'FontSize',9);
ylabel('Depth (m)'); set(gca,'xtick',[2000:800:2800]); xlabel('Rho (kg/m^3)');
set(gca,'ytick',[1.888e+03:55:2.131e+03]); colorbar;
colorbar; caxis([0 0.11]);
set(gcf, 'position', [700 285 200 400]);
figure(4), imagesc([20:1:64], VD, Prob_qp0); ylabel('Depth (m)'); xlabel('Q_P');
set(gca,'xtick',[20:44:64]); set(gca,'FontSize',9); set(gca,'ytick',[1.888e+03:55:2.131e+03]); colorbar;
colorbar; caxis([0 0.17]);
set(gcf, 'position', [800 285 200 400]);
figure(5), imagesc([10:1:40], VD, Prob_qs0); ylabel('Depth (m)'); xlabel('Q_S');
axis([10 40 1.888e+03 2.131e+03]); set(gca,'ytick',[1.888e+03:55:2.131e+03]);
set(gca,'xtick',[10:30:40]); set(gca,'FontSize',9);
colorbar; caxis([0 0.35]);
set(gcf, 'position', [900 285 200 400]);
Star Strider
Star Strider 2022년 9월 12일
이동: Star Strider 2022년 9월 12일
I thought they were plotted as subplot plots, similar to the way I plotted my example.
If you plot them as subplots in one figure rather than as individual figures, my code should work.
Otherwise, to plot them as individual figures, it would be necessary to get different ‘pos’ vectors for each figure and adjust them accordingly. You will need to vary the third element in the 'Position' vector for each figure, stretching each one, since you are plotting them as separate figures. That will likely take some experimentation.
I would just plot them all as subplot plots, stretch the figure width as I did, and be done with it.
Nisar Ahmed
Nisar Ahmed 2022년 9월 12일
이동: Star Strider 2022년 9월 12일
@Star Strider Here I plotted separate figs, but I want to plot them as subplot... when subplot wans not working then i plotted them separately. On subplot it is giving error.
This works for me (in R2022a) —
% LD = load(websave('xy','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1122910/xy.mat'))
load(websave('xy','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1122910/xy.mat'))
figure
subplot(1,5,1)
imagesc([1500:50:6000], VD, Prob_vp0); xlabel('V_P (m/s)');
axis([1600 6000 1.888e+03 2.131e+03]); set(gca,'ytick',[1.888e+03:55:2.131e+03]);
set(gca,'xtick',[1600:4400:6000]); set(gca,'FontSize',9); ylabel('Depth (m)');
colorbar; caxis([0 0.05]);
subplot(1,5,2)
imagesc([800:50:3800], VD, Prob_vs0); set(gca,'FontSize',9);
ylabel('Depth (m)'); set(gca,'xtick',[800:3000:3800]); xlabel('V_S (m/s)');
set(gca,'ytick',[1.888e+03:55:2.131e+03]); colorbar;
colorbar; caxis([0 0.15]);
subplot(1,5,3)
imagesc([2000:10:2800], VD, Prob_rho0); set(gca,'FontSize',9);
ylabel('Depth (m)'); set(gca,'xtick',[2000:800:2800]); xlabel('Rho (kg/m^3)');
set(gca,'ytick',[1.888e+03:55:2.131e+03]); colorbar;
colorbar; caxis([0 0.11]);
subplot(1,5,4)
imagesc([20:1:64], VD, Prob_qp0); ylabel('Depth (m)'); xlabel('Q_P');
set(gca,'xtick',[20:44:64]); set(gca,'FontSize',9); set(gca,'ytick',[1.888e+03:55:2.131e+03]); colorbar;
colorbar; caxis([0 0.17]);
subplot(1,5,5)
imagesc([10:1:40], VD, Prob_qs0); ylabel('Depth (m)'); xlabel('Q_S');
axis([10 40 1.888e+03 2.131e+03]); set(gca,'ytick',[1.888e+03:55:2.131e+03]);
set(gca,'xtick',[10:30:40]); set(gca,'FontSize',9);
colorbar; caxis([0 0.35]);
sgtitle('Original')
figure
subplot(1,5,1)
imagesc([1500:50:6000], VD, Prob_vp0); xlabel('V_P (m/s)');
axis([1600 6000 1.888e+03 2.131e+03]); set(gca,'ytick',[1.888e+03:55:2.131e+03]);
set(gca,'xtick',[1600:4400:6000]); set(gca,'FontSize',9); ylabel('Depth (m)');
colorbar; caxis([0 0.05]);
subplot(1,5,2)
imagesc([800:50:3800], VD, Prob_vs0); set(gca,'FontSize',9);
ylabel('Depth (m)'); set(gca,'xtick',[800:3000:3800]); xlabel('V_S (m/s)');
set(gca,'ytick',[1.888e+03:55:2.131e+03]); colorbar;
colorbar; caxis([0 0.15]);
subplot(1,5,3)
imagesc([2000:10:2800], VD, Prob_rho0); set(gca,'FontSize',9);
ylabel('Depth (m)'); set(gca,'xtick',[2000:800:2800]); xlabel('Rho (kg/m^3)');
set(gca,'ytick',[1.888e+03:55:2.131e+03]); colorbar;
colorbar; caxis([0 0.11]);
subplot(1,5,4)
imagesc([20:1:64], VD, Prob_qp0); ylabel('Depth (m)'); xlabel('Q_P');
set(gca,'xtick',[20:44:64]); set(gca,'FontSize',9); set(gca,'ytick',[1.888e+03:55:2.131e+03]); colorbar;
colorbar; caxis([0 0.17]);
subplot(1,5,5)
imagesc([10:1:40], VD, Prob_qs0); ylabel('Depth (m)'); xlabel('Q_S');
axis([10 40 1.888e+03 2.131e+03]); set(gca,'ytick',[1.888e+03:55:2.131e+03]);
set(gca,'xtick',[10:30:40]); set(gca,'FontSize',9);
colorbar; caxis([0 0.35]);
pos = get(gcf, 'Position');
set(gcf,'Position',pos+[-50 0 450 0]) % Shift Slightly Left & Stretch To Right
sgtitle('Stretched')
The Online Run feature has some (necessary) constraints, so you will likely get slightly different results than appear here when you plot it. Make appropriate adjustments to get the result you want.
The only change I made to your original posted code was to remove the ‘set(gcf,'position',...)’ calls for each figure, since they are not necessary here.
.
@Star Strider Thanks dear
As always, my pleasure!

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

추가 답변 (0개)

카테고리

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

질문:

2022년 9월 12일

댓글:

2022년 9월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by