make grid lines bigger in matlab figures - matlab

How can I make grid lines bigger (more fat for printing purpose) in my matlab figures?
I'm including matlab figures in to my .tex document after using the following
print -depsc testFig.eps
to convert the figure into .eps for inclusion in my .tex doc.
But my grid lines don't look good at all. i.e they appear faint when I print the document. Is there anyway I can increase the size/width of the grid lines?

If you use
set(gca,'LineWidth',10)
after "grid on" this should increase the boarders of all axes, including the grid lines.

Related

White lines in figure after exporting to PDF

A 2D data matrix was plotted in MATLAB 2016a using contour (the first Figure below), and then I saved as the figure in the *.emf format. Next, I inserted the figure (emf) into a MS word document. And finally, the word document was converted to a pdf file.
I found that there are many white lines in the figure (when in a pdf format) as shown in the second figure below. My question is how can I remove those white lines?
The code is attached here:
path = 'C:\Users\Administrator\Desktop\';
data = importdata([path, 'lsa2.txt'], ' ', 6);
cdata = data.data;
n = 25;
contourf(cdata,n, 'LineStyle', 'none');
colormap(jet);
axis equal;
The data can be accessed here: https://www.dropbox.com/s/hzf75qiju6zsy9i/lsa2.txt?dl=0
As I mentioned in my comment, this is a bug with the way MATLAB exports graphics, as explained by Yair Altman and Dene Farrell:
I discovered that these white line artifacts happen when the painters renderer is used ... [which] is the default rendering [format engine] for vectorized (EPS/PDF) formats.
There are two separate issues with the Matlab export:
1. The main thing that everyone notices is that patches are broken up into triangles, each of which is a separate path object if inspected in illustrator.
2. Matlab sometimes adds extraneous 'cropping paths' that create an apparent white line even when there is no issue with fractured paths.
One workaround suggested there by ambramson is the following:
1. Save the figure as an .eps file (using the print command).
2. Using a text editor, change the line in the eps header from:
/f/fill ld
to:
/f{GS 1 LW S GR fill}bd
and move the line down several lines, to right below the /LW/setlinewidth ld line.
From here, your eps file should display fine on all pdf viewers.

Representing legend in matrix form in Matlab

I am supposed to represent the legend as a 2X3 matrix including the line style in the matrix (like dashed represent one curve, straight line represents other curve).
I did lot of google search but did not really get anything. Legend flex also didn't work.
Can you kindly suggest how can I align my legend data manually in a matrix form ??
Well, I realized there are no such formats supported on Matlab legend formats. Therefore, I ended up editing the text box with extra spaces and symbols. However, I still believe there is a way to insert a matrix on the plots like legends.

Matlab: How to avoid artefacts in filled contour plots

I am trying to export filled contour plots from Matlab as vector graphics to include in a Latex file. My current methodology is:
contourf(x,y,v_mag,20), axis([0,width,0,height]),daspect('manual') ;
grid off
colormap jet
h = colorbar;
caxis([0 v_lid])
h.Label.String = 'Velocity Magnitude (m/s)';
set(gcf,'renderer','painters')
export_fig('-painters', '-transparent', 'pdf', 'filename.pdf');
The problem with this method is that it produces artefacts (the white lines) which look like the following:
I understand that these white lines are the polygons defining the shaded areas which have invisible edges, and don't quite overlap (according to here). The problem is caused by the pdf viewer itself which tries to smooth the lines displayed on the screen (according to here). My problem is that most people viewing the document will not know this and will not know how to prevent the viewer doing this. So my questions is:
Is it possible to create a vector graphic of a filled contour plot from Matlab without these artefacts?
Eps produces the same problems. I have tried to use the SVG function but have not had any luck. I am trying to avoid using raster graphics due to the pixelation caused by zooming in. Any advice would be much appreciated.
EDIT - Additional info - Using Matlab v.2014b and Ghostscript v.9.15
This is an extremely frustrating issue for which there seems to be no solution (or even, few attempts at a solution), and it has been many years now. In summary, Matlab cannot cope with outputting artefact-free contour or surface plots (anything with complicated meshes or transparencies).
I can suggest a simple workaround that will work in most cases, where the colours or details of the underlying contour plot do not need to be preserved perfectly.
Output a version of the figure without lines in png format with high enough resolution.
Output a version of the figure without colours in pdf format. This should be free of any artefacts. If your figure it complicated and has many transparencies, you may need to output multiple versions building up the 'levels'.
Use Adobe Illustrator (or some equivalent) to perform a vectorized trace of the raster image. You may lose some detail here, but for simple contour plots with limited details, it will be converted easily to vectorized form.
Overlay the two images within Illustrator. Output in vector format.
This also allows you to use things like Illustrator's ability to compress pdfs.
If you don't want to toy with vectorizing the raster output, you can also simply replace steps 3-4 and combine a raster colour image with a vectorized line image. This would work well for complicated contour plots giving you crisp lines, but the ability to compress the underlying colours.
Eventually, MatLab 2013b doesn't have this problem. Furthermore the files it produces has much less volume. That is because MatLab 2013b composes vectorized image of big overlapping figures, while MatLab 2014b makes that awful meshing.
Here the first file was got with 2013b and the second with MatLab 2014b (I highlighted one of the polygons with red stroke to show the difference). The volumes differ in approximately 22 times (38 Kb vs. 844 Kb).
So it is not the viewer problem, it's how the image is exported from MatLab.
The issue is also discussed here Triangular split patches with painters renderer in MATLAB 2014b and above, but still no direct solution.

MATLAB: Interactive tool to "draw" a Plot?

Do you know a simple way to draw arbitrary splines or lines in a plot-figure? I've got the following figure which I created in powerpoint with more or less arbitrary spline-curve (blue) but I'd like to do the same in MATLAB now because of a better look in the final plot-output.
I'm now wondering if I've got to manually "find" data-values to draw some sort of spline (which looks roughly like the blue one below) myself or if there's maybe a tool where I can simply insert some points into a plot interactively and there's a curve fitted though it to create something similar!?
The green and red lines I can figure out myself (probably also have to plot them manually, do I)?!?
Thanks in advance!
EDIT
Okay I found a way myself doing it in MATLAB to gnerate a nice spline: Use splinetool, then either use an example or import some data and then you can interactively add and delete points until your spline looks roughly like it should. Then file->print to figure and then tools->edit plot! You can then delete everything you don't need, add title, xlabel and ylabel etc. and then export it to latex with e.g. matlab2tikz :-) Very nice stuff!!
According the purpose, print nice plots for the thesis, I have some out-of-Matlab recommendations.
1) plot everything as usual, you get a figure handle and an axes handle
h = figure( ... )
a = gca
2) use the data cursor function of the figure window and interactively insert the base points for your later splines. You can additional points by right-click.
t = linspace(0,2*pi,1000);
[x y] = deal(sin(t),cos(t))
Later you delete the "visual" part of the data tip inside Illustrator/Inkscape, if you just want to keep the anchor point of the vector graphic to snap your splines.
There is also the possibility of custom data tips: Tutorial at Matlab Central
3) I once wrote a function to nicely plot Matlab figures as vector graphic based PDFs. You can specify height, width and how much white margin around you want. You just need to pass figure and axes handle and the name:
function saveFigure( fig_handle, axes_handle, name , height , width , margin)
set(axes_handle,'LooseInset',get(gca,'TightInset'));
set(fig_handle, 'Units','centimeters','PaperUnits','centimeters')
% the last two parameters of 'Position' define the figure size
set(fig_handle,'Position',[-margin -margin width height],...
'PaperPosition',[0 0 width+margin height+margin],...
'PaperSize',[width+margin height+margin],...
'PaperPositionMode','auto',...
'InvertHardcopy', 'on',...
'Renderer','painters'... %recommended if there are no alphamaps
);
saveas(fig_handle,name,'pdf')
end
4) You get a PDF you could directly use for Latex - for use in MS Office use 'emf' (
Enhanced metafile) rather than 'pdf'. Open this file with Adobe Illustrator (preferable as it offers layers) or Inkskape (Open Source).
5) The datatips are recognized as graphical objects, you can catch them and draw a spline on them. For Illustrator I'd recommend to put the spline in another layer than the actual figure. Later you can just swap the figure and give the spline new anchor points. In Inkscape you could use the grouping function to keep everything together.
6) I'd say you save a lot of time over a only-Matlab-solution. Good look!

How to reduce the borders around subplots in matlab AND subaxis doesn't work

I've written a GUI that gives a matlab subplot of varying size and I'm looking for a generic way to squeeze the subplots together. Subaxis works for the vertical spacing, but it doesn't affect the horizontal spacing.
What's the simplest way to squeeze them together?
An example of one of the output figures it was produced with the following code in a for loop
subaxis (1+ceil(max(zindex)/5),5,5+i, 'Padding', 0, 'Margin', 0,'SpacingHoriz',0.0001,'SpacingVert',0.009);
It looks like your figure dimensions are way different than the number of plots you have. Subaxis works best if you define your figure size, and you should pick the aspect ratio based on how many plots you have.
in the example you have 13 x 5 subplots, but your figure's aspect ratio is more like 7:13. Hence the vertical plots are close together but there is much horizontal white space.
Before plotting, try defining your figure like:
nRows=13;
nCols=5;
PlotWidth=3; %This is your Plot width in cm.
FigW=nCols*FigWidth;
FigH=nRows*FigWidth; %Note: I'm assuming the plots are square
Figure1=figure(1);clf;
set(Figure1,'PaperUnits','centimeters',...
'PaperSize',[FigW FigH],...
'PaperPosition',[0,0,FigW,FigH],...
'Units','centimeters','Position',[1,9,FigW,FigH]);
and see if your figure spacing looks better. A few notes, if you want to use 'inches' instead of cm that's fine. Also, I don't have any margins on my paper plot (defining the paper-size and paper position is useful for exporting). If you wanted a margin you might try something like:
Mgn=1;
set(Figure1,'PaperUnits','centimeters',...
'PaperSize',[FigW+2*Mgn FigH+2*Mgn],...
'PaperPosition',[Mgn,Mgn,FigW,FigH],...
'Units','centimeters','Position',[1,9,FigW,FigH]);
You can then export using matlab's print command to the format of your choice.