I have a figure with a legend in Matlab, I want to know how to change the fontsize of the legend (ie: increase the size of the words "speed" and "distance")
plot(x1,y1,x2,y2)
legend('speed','distance')
If you had searched a bit, you would have found it:
From the documentation of Mathworks
figure
plot(x,ydata,'*',x,ycos)
legend({'Collected Data','y = cos(x)'},'FontSize',8,'FontWeight','bold')
It's pretty clear I think.
Related
I am playing with the visual effect of plots, and a question came up while changing the style of a legend.
To be able to save the figure with legends big enough that can be seen usually one needs to change the FontSize property to e.g. 24. When you do that, the size of the font changes, however, the small line next to it has the same size than when it was small. The proportion between line/text seem quite appropriate to me with a FontSize of around 10, while I believe that with big font sizes bigger "eat" visually the line, which is the important part.
Example with fontsize 30 and 10 (please ignore how much I suck in mspaint and the low resolution of the zoomed legend). The proportion between line/text is nicer in the small one.
I was wondering if there is a way to modify that line. I have been checking the properties but I haven't found any relevant one.
NOTE: The LineWidth property does not change the width of the colour lines, but the width of the bounding box.
You could play with the outputs arguments of legend, especially the icons variable (check here).
According to the docs, they correspond to
Objects used to create the legend icons and descriptions, returned as
text, patch, and line object.
Therefore you might use something like this to modify the LineWidth property of any of your plot, or both of course:
clear
clc
close all
x = 1:10;
plot(x,rand(1,10));
hold on;
plot(x,x,'k');
[h,icons,plots,str] = legend('First plot','Second plot','Location','NorthWest');
set(h,'FontSize',30);
set(icons(:),'LineWidth',2); %// Or whatever
Which outputs:
Note that I used R2014a so it might be a bit different for R2014b.
I am currently writing up a scientific thesis and am very desparate about creating figures that have the exact dimensions I want them to have. Especially the font sizes do not match.
I already googled alot and there are a bunch of guides and scripts about this topic but nothing really helped - I have not yet figured out (sorry) why my approach does not work:
FS=8; %font size in points (the same as in my document)
width=12; %width of figure in cm
height=4; %height of figure in cm
scatter(1:20,rand(20,1));
xlabel('X','fontsize',FS),ylabel('Y','fontsize',FS),title('X vs. Y','fontsize',FS)
%now I scale the figure and place it in the bottom left corner. The white margins around it are cropped automatically
set(gca,'units','centimeters','outerposition',[0 0 width height])
%export as .eps
print(gcf,'-depsc','test')
When I load test.eps into Inkscape, the figure is 10.16 x 3.529 cm large and the font sizes (of title and axis labels) are 10.
How do I get a figure with the exact scaling, especially regarding the font size?
I did the following:
FS=8; %font size in points (the same as in my document)
width=12; %width of figure in cm
height=4; %height of figure in cm
scatter(1:20,rand(20,1));
set(gca, 'fontsize', FS);
xlabel('X','fontsize',FS),ylabel('Y','fontsize',FS),title('X vs. Y','fontsize',FS)
set(gcf,'units','centimeters','position',[0 0 width height])
export_fig(gcf, 'test.pdf', '-transparent', '-nocrop')
The output figure is 12cm x 4cm. The font size still claims to be 10 in Inkscape, however, but it looks the same size as that in the figure. Export_fig can be downloaded from the MATLAB file exchange.
Here is how I solve it as of now - it is not exactly elegant but it works...
I plot my figure and arrange and scale it in the figure window the way I want it to be scaled:
set(gcf,'units','centimeters','position',[0 0 width height])
Due to the white margins around the axes, I increase the width/height by approximate (trial and error...) values that the margins use up. I then export it:
export_fig(gcf,'test','-eps','-transparent')
And load it into Inkscape. Now I set the document properties so that the document has the exact size I want my figure to have - the figure is partly out of this frame because I increased the width/height earlier.
Then I arrange the axes to have as much white space between them as I want them to have; hopefully, everything is inside the document borders after that.
Probably the drawing is smaller than the document borders now - to ensure that it will not get expanded, messing up the scaling, when I put it in my actual document (my thesis, not the Inkscape document...), I simply create a white background that matches the document borders. Aaand done.
Except for the fontsize and fontname properties in Matlab - I have not figured out why they are not properly exported...but this is not hard to manually fix in Inkscape.
Thanks for your help, everyone.
I want to display overlapping boxplots using Sigmaplot 12. When I choose the scale for the x-axis as linear then the boxes do indeed overlap but are much too thin. See figure below. Of course they should be much wider.
When I choose the scale of the x-axis to be "category", then the boxes have the right width, but are arranged along each single x-value.
I want the position as in figure 1 and the width as in figure 2. I tried to resize the box in figure 1 but when I choose 100% in "bar width" than it still looks like Figure 1.
many thanks!
okay, I found the answer myself. In Sigmaplot, there is often the need to prepare "style"-columns, for example if you want to color your barcharts, you need a column that holds the specific color names.
For my boxplot example I needed a column that has the values for "width". These had to be quite large (2000) in order to have an effect. Why ? I have no idea. First I thought it would be because of the latitude values and that the program interprets the point as "1.000"s, but when I changed to values without decimals, it didnĀ“t get better.
Well, here is the result in color.
Have fun !
The font spacing in TeX-typeset equations in MATLAB defaults to being highly compressed. Is there a way to increase the amount of spacing, so that, for example, the numerator and denominator of a fraction do not make contact with the line separating the two?
plot(1:10,rand(1,10));
set(gca,'FontSize',18);
legend('$\frac{xy}{\exp\left(\frac{x}{y}\right)}$');
set(legend(),'interpreter','latex');
I think the easiest way is to use some LaTeX trickery.
Long story short, in LaTeX $ ... $ is used for inline math, but for display math, you should either use \[ ... \] or the legacy way of doing the same $$ ... $$. For LaTeX documents, don't use the latter, but for MATLAB it should be enough.
The difference between inline math and display math, is like the difference between using backticks (``) and indentation in StackOverflow. The first will show your code in-between text, the latter in-between paragraphs. With math, only display mode math will have decent lay-out for larger formulas.
So the following code should fix your problem:
plot(1:10,rand(1,10));
set(gca,'FontSize',18);
legend('$$\frac{xy}{\exp\left(\frac{x}{y}\right)}$$');
set(legend(),'interpreter','latex');
If you want even more, you might want to consult the Not So Short Introduction To LaTeX2e which gets you started with a lot of the tricks of the LaTeX trade.
edit:
What I tend to use as a trick to improve spacing in formulae is using phantoms (\phantom, \vphantom, \hphantom), but \vspace or \vskip might be a little cleaner to use.
Looking through the list of properties for the legend, there doesn't seem to be any way of specifying a line spacing that is consistent with automatic positioning. You can fudge the line spacing by enlarging the box, however, by changing the final entry (height) in the OuterPosition property. It seems the placement of the box is based on its bottom-left corner, so if your legend box is in a North position then you will also need to reduce the second entry (y-position) by an equal amount.
In this example I increase the height of a North-positioned legend box by 25% (which I have found that gives nice results), which increases the line spacing.
h = legend(s1,s2,s3, 'location', 'northeast');
set(h, 'fontsize', 16, 'interpreter', 'latex')
outerposition = get(h, 'OuterPosition');
delta_h = 0.25*outerposition(4);
outerposition(2) = outerposition(2) - delta_h;
outerposition(4) = outerposition(4) + delta_h;
set(h, 'OuterPosition', outerposition)
You have to be wary about resizing the figure after running this code fragment, since changing the OuterPosition property clears the automatic placement of the box with respect to the plot axes. If you resize the figure the legend box will go walkabouts.
I want to change Font Size for xlabel, ylabel, axis size, legend font size a.k.a everything at once, is this possible? By default, font is Helvetica 10.
Is there way to change this?
I want to use 'FontSize',14, for x or y labels.
Jonas's answer is good, but I had to modify it slightly to get every piece of text on the screen to change:
set(gca,'FontSize',30,'fontWeight','bold')
set(findall(gcf,'type','text'),'FontSize',30,'fontWeight','bold')
If you want to change font size for all the text in a figure, you can use findall to find all text handles, after which it's easy:
figureHandle = gcf;
%# make all text in the figure to size 14 and bold
set(findall(figureHandle,'type','text'),'fontSize',14,'fontWeight','bold')
It's possible to change default fonts, both for the axes and for other text, by adding the following lines to the startup.m file.
% Change default axes fonts.
set(0,'DefaultAxesFontName', 'Times New Roman')
set(0,'DefaultAxesFontSize', 14)
% Change default text fonts.
set(0,'DefaultTextFontname', 'Times New Roman')
set(0,'DefaultTextFontSize', 14)
If you don't know if you have a startup.m file, run
which startup
to find its location. If Matlab says there isn't one, run
userpath
to know where it should be placed.
If anyone was wondering how to change the font sizes without messing around with the Matlab default fonts, and change every font in a figure, I found this thread where suggests this:
set(findall(fig, '-property', 'FontSize'), 'FontSize', 10, 'fontWeight', 'bold')
findall is a pretty handy command and in the case above it really finds all the children who have a 'FontSize' property: axes lables, axes titles, pushbuttons, etc.
Hope it helps.
To change the title font size, use the following example
title('mytitle','FontSize',12);
to the change the graph axes label font size, do the following
axes('FontSize',24);
Jonas's answer does not change the font size of the axes.
Sergeyf's answer does not work when there are multiple subplots.
Here is a modification of their answers that works for me when I have multiple subplots:
set(findall(gcf,'type','axes'),'fontsize',30)
set(findall(gcf,'type','text'),'fontSize',30)
To change the default property for your entire MATLAB session, see the documentation on how default properties are handled.
As an example:
set(0,'DefaultAxesFontSize',22)
x=1:200; y=sin(x);
plot(x,y)
title('hello'); xlabel('x'); ylabel('sin(x)')