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
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 件のコメント
Nisar Ahmed
2022 年 9 月 12 日
@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
Star Strider
2022 年 9 月 12 日
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
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
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
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.
.
Nisar Ahmed
2022 年 9 月 15 日
@Star Strider Thanks dear
Star Strider
2022 年 9 月 15 日
As always, my pleasure!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Graphics Performance についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
