how to add a label to a contourf plot next to colourbar?

64 vues (au cours des 30 derniers jours)
Tony Castillo
Tony Castillo le 2 Juin 2021
Commenté : Tony Castillo le 3 Juin 2021
Hello mathcoders,
I want to add a label next to the colourbar for adding the variable's name, how can I do it?. The resulting plot and code is attached to these brief lines. Also I have noticed that when adding months labels it only present till June, but when remove the colourbar it prints till December, how can I solve this?.
Thanks in advance
figure(1)
irradiation_alongyear=[Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec];
contourf(irradiation_alongyear)
colorbar
ylabel('Hours of the day', 'Fontsize', 14)
xticklabels(LastName)
title('Irradiation distribution along the year','FontSize',14)
  2 commentaires
Scott MacKenzie
Scott MacKenzie le 2 Juin 2021
It might help if you post the code that created the figure. Your code crashes with the following error:
Unrecognized function or variable 'Jan'.
Error in test3 (line 7)
irradiation_alongyear=[Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec];
Tony Castillo
Tony Castillo le 3 Juin 2021
I apologize for not sharing more data, I guess that it but be doable with the part of the code that I shared.

Connectez-vous pour commenter.

Réponse acceptée

Joseph Cheng
Joseph Cheng le 2 Juin 2021
Modifié(e) : Joseph Cheng le 2 Juin 2021
you can set a ylabel by passing the colorbar handles into ylabel
[X,Y] = meshgrid(1:12,1:24);
Z = sin(X) + cos(Y);
figure(1),contourf(Z)
lastname = {'jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec'};
ylabel('hours of the day')
xticks([1:12]);xlim([1 12]);
xticklabels(lastname)
hbar = colorbar;
ylabel(hbar,'Varname');
here essentially i generated a dummy set of data to contourf() then force the x limits to 1 to 12 for the month then set hbar as the colorbar handles such that ylabel() knows what to label
  2 commentaires
Joseph Cheng
Joseph Cheng le 2 Juin 2021
actually figured out why it was set to only half the months or so. as you set the ticklabels because of say the scaling for display changes the ticks it'll take the first X. so to remedy this i edited my initial response to also force the number of ticks to be 12 such that matlab doesn't auto populate what they should be
Tony Castillo
Tony Castillo le 3 Juin 2021
Thank you Sir Cheng, it worked properly.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by