Change legend line style - matlab

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.

Related

How to set the same size for all the figures for saving in Matlab?

I want to set the same size for all figures using Matlab in order to save it later. How can I do that?
I thought it would be better to plot a figure first, and then get the size and position using command pos = get(gcf, 'Position'), and set the position for all the other figures.
Is it correct? Any better approaches?
There are a lot of options of how to save a figure in Matlab. If you do not use a Save As dialog box, you have two functions to choose from: saveas and print.
'Position' defines location and size of the drawable area, specified as a vector of the form [left bottom width height]. This area excludes the figure borders, title bar etc. Right now you are basically getting the size and location of your first figure as it appears on screen and save based on these dimensions.
When saving your figures this way, the dimensions will correspond to whatever internally was defined in Matlab or you yourself redefined using 'Position' property. But you don't always want/need the size of the saved figure and the size of the figure as it appears on screen to be the same. And you also have to take care of the position of your figures, which is in your case you retrieved using a set function, I'll skip it in my example.
gcf=figure;
figure_width_to_save = 12.5; %cm
figure_height_to_save= 10; %cm
location_x=2; %cm
location_y=2; %cm
gcf.Units = 'centimeters';
gcf.Position = [location_x location_y figure_width_to_save figure_height_to_save];
saveas(gcf,[savefigures_path,savefigure_name,'_saveas.tiff'],'tiffn');
print(gcf, '-dtiffn', [savefigures_path,savefigure_name,'_print.tiff'], '-r300');
But it's better to have a separate control over the settings used for saving a figure. For that you have to define 'PaperPosition' property. 'PaperPosition' defines figure size and location on page when saving, specified as a four-element vector of the form [left bottom width height], but actually with 'PaperPosition' property you don't need to think about the location of your figure as much as you would with the 'Position' property.
Now about the saving itself, you didn't mention which approach you use though.
The saveas function uses a resolution of 150 DPI and uses the 'PaperPosition' and 'PaperPositionMode' properties of the figure to determine the size of the image. If you want to print or save figures that are the same size as the figure on the screen, ensure that the 'PaperPositionMode' property of the figure is set to 'auto', but I prefer to have control over these properties myself.
If you save your figure in Matlab with saveas, then as an example you need to specify this:
gcf.PaperPositionMode = 'manual';
gcf.PaperUnits = 'centimeters';
gcf.PaperPosition = [0 0 figure_width_to_save figure_height_to_save];
saveas(gcf,[savefigures_path,savefigure_name,'.tiff'],'tiffn');
Function print additionally allows you to have control over the saved resolution of the figure. For example, a flag '-r300' sets the output resolution to 300 dots per inch. To specify screen resolution, use '-r0'.
print([savefigures_path,savefigure_name,'.tiff'],'-dtiffn','-r300')
Check out Matlab's examples about saving figures at specific size and resolution

Matlab: Scale figures for publishing - exact dimensions and font sizes

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.

Sigmaplot: How to scale x-axis for correctly displaying boxplots

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 !

How to determine a projected (if 3D) aspect ratio (if set) of a figure in Matlab to specify a proper paper size?

I saw many Q&A here about squeezing space out of Matlab figures. However I want to squeeze space resulted from a possibly fixed aspect, i.e. to choose proper paper size for figure printing when aspect is fixed.
Quite often I work with DEM/map/image thus I use axis image. Now if I want to produce a high resolution image I do something like
set(gcf,'PaperUnits','inches','PaperPosition',[0 0 4 3])
print('-dpng','-r300','somefile.png')
as described in Matlab KB.
The problem here is to determine a proper aspect such that I can specify proper paper size that would leave no white/background stripes on either sides.
Apparently if I have a map (let's say 1000x2000 cells) with aspect ratio of 0.5, and I'm printing it on 4"x3" paper, I'll get background stripes on the sides. This is quite annoying as I'd prefer 1.5"x3" paper + axes & labels or so. Right now I have to manually adjust paper size.
This is inconvenient as I'd like a universal solution. For instance I may print a plot into file that I expect to occupy 4"x3" as well that has no fixed aspect. Or I may want to print a 3D figure. I'm aware of daspect and pbaspect, but how can I know how it is currently drawn?
Perhaps I can derive current 2D aspect from get(gca,'Position') and then scale it to my maximum allowed desired size (e.g., 4"x3") while respecting whether DataAspectRatioMode (?) property is set to manual. Is it the way to proceed or is there a better way?
I am not exactly sure if I understand your problem exactly, but I have used the following commands to create pdf images that are sized exactly to the size of the figure. I have used this for both 2D and 3D figures. The "handle" variable is simply your figure handle.
set(handle,'Units','inches');
set(handle,'PaperUnits','Inches','PaperPositionMode','auto');
P = get(handle,'Position');
set(handle,'PaperSize', [P(3),P(4)]);

MATLAB: latex interpreter font spacing

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.