How can i change the font size of XTick and YTick (x axis and y axis) in histogram of a image?
305 views (last 30 days)
Show older comments
I have a image as lena.jpg, from which i was trying to obtain hist graph.
x=imread('lena.jpg');
imhist(x);
set(gca,'FontSize',15);
with this code i am able to change the font size of YTick only but i want to change font size of both. how can i do that????
2 Comments
Answers (4)
KSSV
on 6 Jun 2016
a = get(gca,'XTickLabel');
set(gca,'XTickLabel',a,'FontName','Times','fontsize',18)
3 Comments
DGM
on 16 Jun 2024
Edited: DGM
on 16 Jun 2024
Other than manual editing, none of these answers work as simply as suggested. The reason is that imhist() creates two axes objects. The most recently-created axes (the colorbar) is not the one to which gca points. When imhist() exits, it sets gca to the axes which contains the stem plot. So if you operate on gca, that's the only axes you change.
In the simple case where there's only one thing in the figure, it's easy enough. If you have a bunch of subplots, then you'll have to figure out which pairs of axes correspond to the relevant imhist() call. Unfortunately, imhist() doesn't support any mechanism to return either handle as an output argument. It does provide a unique label for the colorbar axes, but for some bizarre reason, it doesn't label the other. There may be a more foolproof way, but I'm just going to rely on order of operations.
% some 2D image
inpict = imread('cameraman.tif');
% i'm just going to rely on the axes being the two newest children of gcf.
% that means these three lines of code must run together
imhist(inpict);
hax = findobj(gcf,'type','axes'); % all axes in the figure
hax = hax(1:2); % only care about the two most recent
% ... now all sorts of other things can change in the figure
% ... and we won't lose track of those two axes
set(hax,'FontSize',15); % set the properties of _both_ axes
2 Comments
DGM
on 16 Jun 2024
I guess one other way would be to use the axes link to find which pairs of axes correspond to a single imhist() call. That might be more useful if you're trying to modify something after the entire figure has been populated.
% say i have multiple axes in a figure
% in this figure, there are 6 axes
% an image and its histogram
inpict = imread('cameraman.tif');
subplot(2,2,1)
imshow(inpict,'border','tight')
subplot(2,2,3)
imhist(inpict);
% another image and its histogram
inpict = imread('tire.tif');
subplot(2,2,2)
imshow(inpict,'border','tight')
subplot(2,2,4)
imhist(inpict);
% we didn't keep track of anything during figure creation
% so now we have to figure out which of the six axes are which.
% get all colorstripe axes, since they're the only tagged objects.
haxcs = findobj(gcf,'type','axes','tag','colorstripe');
% find the axes pairs using the links
nhgrams = numel(haxcs);
haxpairs = gobjects(2,nhgrams);
for k = 1:nhgrams
S = getappdata(haxcs(1 + nhgrams - k)); % index backwards
haxpairs(:,k) = S.linkColorStripe.Targets;
end
% say you want to set the properties of each axes pair
% the backwards indexing should mean that the first column
% of handles were the first ones created
set(haxpairs(:,1),'FontSize',12); % first pair
set(haxpairs(:,2),'FontSize',15); % second pair
The handle array haxpairs contains one column for each pair of axes. The first row is the stem axes; the second row is the colorstripe axes.
DGM
on 16 Jun 2024
I might as well say it since I'm here. MIMT imhistFB() solves this problem anyway. It also solves a bunch of other problems with using imhist().
% some 2D image
inpict = imread('cameraman.tif');
% MIMT imhistFB() isn't really the same as IPT imhist() anymore.
% it will return axes handles, and it can return outputs and plot simultaneously.
[~,~,~,hax] = imhistFB(inpict,'forceplot');
% even if you didn't get the axes directly, both axes are tagged
% so it would be easy enough to find them with findobj().
% there may still be ambiguity if there are multiple histograms,
% but that's why you should just get the handles from the function call.
hax
hax =
2×1 Axes array:
Axes (imhistFB_plot)
Axes (imhistFB_stripe)
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!