Font of axis data and legends
20 views (last 30 days)
Show older comments
How can I change the font of x-axis (or y-axis or the legend)? I want to change a from matlab command not by clicking on the figure. Like "FontSize" for the texts or "LineWidth" for the curves, is there a variable that I can change the font of axis data?
0 Comments
Accepted Answer
Tom
on 23 Feb 2012
Get the handle of the label, legend etc: Handle=xlabel(...) Then you can use set(Handle,'FontName',[name of font])
3 Comments
More Answers (2)
Peter Wittenberg
on 27 Feb 2012
There's a way that's somewhat easier to remember and use, although not as general. First, though, let's assume that you have created and plotted a figure, either from the command line or as part of an m-file. The figure you want to change is the current graphics object or gca, which basically means you clicked on the plot most recently of all the plots you have or you made an m-file. You can do this for any graphics object you want, but it's easier to remember (no handles) if it is the gca.
You'll use the commands like this. I've put in numbers for a fontsize of 16, but you can use anything you like.
xlabel('This is my x label','Fontsize',16);
ylabel('This is my y label','Fontsize',16);
title('This is my figure title','Fontsize',20);
When you look at the MATLAB documentation on these commands, you'll find that you can generalize most of them to do more tricks. Fontsize is by no means the only text property you can set. You can do this for other figures as well. However, most of what you want will be done by these simple and easy to remember commands.
You might find the following pretty neat. I developed a routine to do most of the common plot chores. It writes code that you can put in an m-file and it runs on the command line as well. I find it handy to use as a starting point for plots, then I modify the code in m-files for specific purposes.
% This routine sets up a dialog box for the user to enter values for a
% plot defaults. The routine then writes code to meet this and displays
% it.
% version v1.1 dated 8 June 2010 - added x and y limits
% Peter Wittenberg
prompt = {'Window Title Bar:','Axis fontsize:',...
'x variable name','y variable name','x label name','y label name',...
'x label font size','y label font size',...
'title text','title font size',...
'lower x limit','upper x limit','lower y limit','upper y limit',...
};
dialogTitle = 'Plotmaker - Insert values';
lines = 1;
def = {'Window Title','16',...
'x variable name','y variable name','x label name','y label name',...
'18','18',...
'title text','18','auto','auto','auto','auto'};
answer = inputdlg(prompt,dialogTitle,lines,def,'on');
limitsLines = {};
for index = 11:14
if ~strcmp (answer(index) , 'auto' )
limitsLines = cat(1,limitsLines,...
strcat('limits(', num2str(index-10) , ')= ', char(answer(index)) , ';'));
end;
end;
plotCode = { 'figure(...'
strcat('''Name'',''' , char(answer(1)) , ''',...')
'''NumberTitle'',''off'' ); % A new figure'
strcat('thePlot = axes(''Fontsize'',' , char(answer(2)) , ''');')
strcat('plot(' , char(answer(3)) , ',' , char(answer(4)) , ');')
strcat('xlabel(''' ,char(answer(5)) , ''',''Fontsize'',' , char(answer(7)) , ');')
strcat('ylabel(''' ,char(answer(6)) , ''',''Fontsize'',' , char(answer(8)) , ');')
strcat('titleText = ''', char(answer(9)) , ''';')
strcat('title(titleText, ''FontSize'',' , char(answer(10)) , ');')
'limits = axis();'
};
plotCode = cat(1,plotCode,limitsLines);
plotCode = cat(1,plotCode, 'axis(limits);');
u = (char(plotCode))';
disp(char(plotCode));
thePlotCommands = u(1:end);
thePlotCommands = strrep(thePlotCommands,'...','');
thePlotCommands = strrep(thePlotCommands,'% A new figure','');
eval(thePlotCommands);
0 Comments
Gurudatha Pai
on 23 Feb 2012
you can set once using this command per matlab session or add this to your start-up program
set(0, 'DefaultAxesFontSize', AxisFontSize, 'DefaultAxesFontWeight', AxisFontWeight);
0 Comments
See Also
Categories
Find more on Annotations 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!