Hello, I am trying to configure a colorbar but there are two information that I don't know how to do.
This is the image of my plot
In addition, I am adding the code below:
function [ ] = Figure1(TT,aux,x,y)
set(gcf,'Color',[1 1 1]);
h=colorbar('SouthOutside');
set(h, 'Position', [.05 .15 .9 .05]);
load('MyColormap.mat')
if aux == 1
subplot('Position',[.0 .25 .20 .20]);
elseif aux == 2
subplot('Position',[.25 .25 .20 .20]);
elseif aux == 3
subplot('Position',[.5 .25 .20 .20]);
else
subplot('Position',[.75 .25 .20 .20]);
end
shading flat
pcolor(x,y,TT')
colormap(mymap)
caxis([275 320])
shading interp
view(2)
axis square
set(gca,'Visible','off')
end
What I need:
  1. Define (reduce) the space between subplot(1,4,1) e subplot(1,4,2) .....
  2. Add a title on my horizontal colorbar.
PS: I have already read many links, but I couldn't find a concrete information!
Cheers

12 Kommentare

jonas
jonas am 1 Jul. 2018
Just change the first values in the position vector to reduce the space?
Title is added by
h.Label.String='title'
As described in the doc
yogan sganzerla
yogan sganzerla am 1 Jul. 2018
Hey Jonas! Thanks for your answer. I didn't understand what do you mean with: "Just change the first values in the position vector" Could you show in the code what First position should I change?
PS: Your help with the title worked perfectly!
Kind regards.
Walter Roberson
Walter Roberson am 1 Jul. 2018
Bearbeitet: Walter Roberson am 1 Jul. 2018
The 'Position' option is followed by [lower_left_x, lower_left_y, width, height] . If you want your axes to be closer together, you can modify the coordinates to put them closer together.
However, you need to be careful with subplot: if it thinks there is an overlap with an existing axes, it will delete the axisting axes.
It might make more sense to create four axes ahead of time using axes() with appropriate position, and then to fetch the handle for the appropriate one according to the value of aux.
yogan sganzerla
yogan sganzerla am 1 Jul. 2018
Hey Walter! Thanks for your help! I understand that the "Position" is followed by [lower_left_x, lower_left_y, width, height] and because of that I set the end of the first subplot with the beginner of the next. You can see the image below.
Could your change in the code your idea or add this function axes() and replay me?
cheers
function [ ] = Figure1(TT,aux,x,y)
persistent known_axes mymap
if isempty(known_axes) || ~all(ishandle(known_axes))
delete( findobj(gcf, '-regexp', 'tag', '^bax_\d+') );
basepos = 0.15; %for example
axwidth = 0.25;
for K = 1 : 4
known_axes(K) = axes('InnerPosition', [basepos + (K-1)*axwidth, 0.25, axwidth, 0.20], 'Tag', sprintf('bax_%d', K)); %note use of InnerPosition
end
datastruct = load('MyColormap.mat');
mymap = datastruct.mymap;
end
cax = known_axes(aux);
set(gcf, 'Color',[1 1 1]);
h = colorbar(cax, 'SouthOutside');
set(h, 'Position', [.05 .15 .9 .05]);
shading(cax, 'flat');
pcolor(cax, x, y, TT');
colormap(cax, mymap);
caxis(cax, [275 320]);
shading(cax, 'interp');
view(cax, 2);
axis(cax, 'square');
set(cax,'Visible','off')
end
Note: this code is not correct for the case where you are using the function for two different figures.
yogan sganzerla
yogan sganzerla am 2 Jul. 2018
Thanks Walter, Let me know one thing... If I would like to increase or decrease the gap between subplot, what part of your code should I change?
Cheers
You can change axwidth and you can change the equation
basepos + (K-1)*axwidth
For example you could define
overlap = 0.02;
and use
basepos + (K-1)*(axwidth - overlap)
jonas
jonas am 2 Jul. 2018
You may find this FileExchange function useful ( tight_subplots )
yogan sganzerla
yogan sganzerla am 2 Jul. 2018
Hey Jonas, I have already read this function, but I couldn'tapply in my code.
Can you show what are the input values of this functions and in especial, the output?
Cheers
jonas
jonas am 2 Jul. 2018
Bearbeitet: jonas am 2 Jul. 2018
Try typing (in matlab)
help tight_subplot
The output is an array of handles, with a size of Nw*Nh (i.e. number of axes). Each handle is one axes.
When you want to plot in a certain axes, you need to make that axes the current axes. Let's say your array of handles is called ha (output from the function) and you want to plot in the axes located in the upper left corner of your grid. Then you type:
axes(ha(1))
plot(...
Always check the doc when using a new function, it's usually very comprehensive and filled with examples. If you use functions from FileExchange, then type help function in matlab.
yogan sganzerla
yogan sganzerla am 5 Jul. 2018
Mr Jonas, As I said before, I have already studied this function and I am studying again but it doesn't work with me!!
Could you give a complete example that the answer is a plot with 1 row and 4 columns with the circle closer than the default?
I create a vector with the position to plot the 4 circle. The image I am adding below.
And ha(1) = [0.0400 0.0300 0.2075 0.9400] ha(2) = [0.2775 0.0300 0.2075 0.9400] ha(3) = [0.5150 0.0300 0.2075 0.9400] ha(4) = [0.7525 0.0300 0.2075 0.9400]
How can I integrate the subplot(1,4,1) with ha(1)?
jonas
jonas am 5 Jul. 2018
I don't know what it means to integrate subplot(1,4,1) with ha(1). See my answer below.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

jonas
jonas am 5 Jul. 2018
Bearbeitet: jonas am 5 Jul. 2018

1 Stimme

I'm just gonna sum up the comment chain as an answer for future reference.
Walter Robinson gave an excellent solution for dealing with axis placement without use of third-party functions (see question comments). The following example makes use of the FileExchange function tight_subplot ( link ). Similar functions are subtightplot ( link ) and subplot_tight ( link ). Syntax may differ depending on function.
%%Create 4 axes with zero gap and zero margins
ha=tight_subplot(1,4,0,0,0);
%%Plot 4 circles with zero space in between and remove any axis graphics
for i=1:4
axes(ha(i));
viscircles([0 0],1);
axis square
axis tight
set(gca,'color','none','xcolor','none','ycolor','none')
end
Colorbar title is added by
h.Label.String='title'

12 Kommentare

jonas
jonas am 5 Jul. 2018
You gave me some files then you deleted them again.. Issue resolved or not?
yogan sganzerla
yogan sganzerla am 5 Jul. 2018
Hey Jonas, Thanks for your help! The gap between the circle I have already defined.
Now I would like to insert a title in each subplot like the image below.
The other configuration that I need is the size of the numbers in colorbar. Can I set it?
cheers
jonas
jonas am 5 Jul. 2018
Yes of course. Title in plot 1 is set by
axes(ha(1))
title('string')
And fontsize by
cb.FontSize=12
Where cb is the handle of the colorbar.
As a general tip, you can always type
get(handle)
to display a list of all properties that you can define. The content of the list depend on the type of object class (e.g axes, line, patch).
yogan sganzerla
yogan sganzerla am 29 Jul. 2018
What about changing the location of the title like the image below, is it possible?
In addtion of the title I would like to change the position of the number in colorbar like you can see the number 295 and 300.
Is it possible?
cheers
jonas
jonas am 29 Jul. 2018
Bearbeitet: jonas am 30 Jul. 2018
The Title is easy, just change its position and rotation properties. Here is an example where the position property of the title is connected to the position of the axes.
ha=axes
title('Title')
ha.Title.Rotation=-90
pos_ax=ha.Position;
ha.Title.Position=[pos_ax(1)+pos_ax(3)+0.1 (pos_ax(2)+pos_ax(4)/2)]
The colorbar ticklabels turned out to be more difficult. Will get back to that later today.
yogan sganzerla
yogan sganzerla am 1 Aug. 2018
Dear Jonas, I couldn't apply your idea in my code. Because of that, I am uploading my main code and a function that plot the colorbar.
Could you help me with your idea?
Kind regards
jonas
jonas am 1 Aug. 2018
I think you attached the wrong m-file. Make sure to include all the files needed to run the code.
yogan sganzerla
yogan sganzerla am 1 Aug. 2018
Yes Jonas, I forgot one file! I am attaching here. Cheers
jonas
jonas am 1 Aug. 2018
Bearbeitet: jonas am 1 Aug. 2018
When you set the axis color to 'none', then the property 'visible' changes for all labels, including the title. Add this at the very end of the main script.
for i=1:4
axes(ha(i))
pos_ax=ha(i).Position;
title('title','color',[1 0 0],'visible','on','units','normalized','rotation',-90,...
'position',[0.88+0.01*i (pos_ax(2)+pos_ax(4)/2)])
end
PS: I didn't get your colormap, so my fig is not identical to yours.
yogan sganzerla
yogan sganzerla am 2 Aug. 2018
Thanks Jonas, It worked perfectly!
What about the numbers in colorbar? Look the number 300 that I changed using paint.Is it possible to configure it?
yogan sganzerla
yogan sganzerla am 6 Aug. 2018
Mr Jonas?
Do you know how to change it?
Cheers
jonas
jonas am 6 Aug. 2018
Will give it a try! I have seen your other question, will reply there if I come up with an answer.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by