increasing the fontsize of the y-label tickmarks on a dendrogram/tree

I created a dendrogram where the x-axis is the distance/dissimilarity between clusters and the y-axis are the objects. I want to increase the font size, but only the x-axis objects are increased, the y-axis labels remain the same. How do I do this?

6 Kommentare

Please show us what you tried already.
I have tried the usual set(gca,'Fontsize',18). This does not work.
I have also tried calling the objects ax.YTickLabels, etcetc... Still no success.
Please provide a minimal working example that reproduces the problem.
I don't see any problem with the example below.
X = rand(10,3);
tree = linkage(X,'average')
figure()
dendrogram(tree)
set(gca,'FontSize',18)
sorry. i forgot to say that i used custom labels here. So only the distance/dissimilarity measures are default. The labels of the individual points are custom. So upon resizing, only the distance axis is resized. The parts where I used my custom label are unaffected by the fontsize change.
I can provide a quite answer as soon as I have a working example that produces your plot (or you could attach the figure file).
%%I also made modifications to the colours as a visualization requirement.
X = rand(10,5);
Y = pdist(X);
Z = linkage(Y);
labels = {'man1','man2', 'man3','man4','man5','woman1','woman2','woman3','woman4', 'woman5'};
H = dendrogram(Z,0, 'Labels', labels, 'Orientation','left');
set(H,'LineWidth',2)
ax = gca; % get the axes handle
X = get(ax.Children,'XData'); % Get x values of all lines
Y = get(ax.Children,'YData'); % Get y values of all lines
ax = gca; % get the axes handle
lab = ax.YAxis.TickLabels; % get all the labels
loc = ax.YAxis.TickValues; % get all labels location
[ulab,~,lab_ind] = unique(lab); % find all unique labels
clr = lines(numel(ulab)); % make a color map
for k = 1:numel(ulab) % for every type of lable
ind = strcmp(ulab{k},lab); % find all instances in lab
x = repelem(ax.XAxis.Limits(1)-0.01,sum(ind)); % make an x position vector
% place this lable at the same locations with a distinct color:
text(x,loc(ind),lab(ind),'Color',clr(k,:));
end
ax.YAxis.TickLabels = []; % remove the original labels
% replace the original labels with white space, to keep the axes position:
ax.YAxis.TickLabels = repelem(' ',max(cellfun(@numel,lab)));
for k = 1:numel(Y)
if Y{k}(1)==fix(Y{k}(1))
line(ax,X{k}(1:2),Y{k}(1:2),'Color',clr(lab_ind(Y{k}(1)),:),...
'LineWidth',2);
end
if Y{k}(3)==fix(Y{k}(3))
line(ax,X{k}(3:4),Y{k}(3:4),'Color',clr(lab_ind(Y{k}(3)),:),...
'LineWidth',2);
end
end
set(gca,'fontsize',18)
xlabel('distance/dissimilarity between clusters');

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Your y-labels aren't really tick labels. They are text objects. You need to store their handles and set their fontsize directly.
textLabels = gobjects(size(ulab)); % PREALLOCATE HANDLES VECTOR
for k = 1:numel(ulab)
% [ YOUR OTHER CODE GOES HERE] ....
textLabels(k) = text(x,loc(ind),lab(ind),'Color',clr(k,:)); % STORE EACH HANDLE
end
Then change fontsize
set(textLabels,'FontSize', 18)
However, the whole approach seems inefficient.
Instead of defining the labels at the top of your code and then change the labels within the loop, just set the labels correctly before making the plot. That way your lables are actual y-tick labels and will respond to set(gca,'fontsize',18).

4 Kommentare

thanks for your suggestion!
Adam Danz
Adam Danz am 30 Mai 2020
Bearbeitet: Adam Danz am 1 Jun. 2020
Glad I could help!
What is x in your code?
I'm using the same variables the OP shared in a previous comment.
See the first input to the text(x,y,str) function.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Objects finden Sie in Hilfe-Center und File Exchange

Gefragt:

cgo
am 29 Mai 2020

Kommentiert:

am 25 Mai 2021

Community Treasure Hunt

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

Start Hunting!

Translated by