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

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.

Related

Arraging multiple 3D plots

I have created 6 3D plots that I need to include in a research paper. However, I need to all the plots in a grid.
I've been trying the function subplot but all my plots are combining instead of forming a grid. I have tried setting nrows to various numbers but it does not change the layout. Any ideas on how to fix this? Or other functions that may serve the same purpose?
Shows the function for 1/6 plots, then the subplot function. to the right is the output:

Why is line plot in subplot distorted when using barplot in other subplot

The upper subplot is a perfectly normal ECG signal.
The bottom subplot is a bar plot.
Only on windows matlab 2014a, the ECG signal gets screwed up. It seems like it groups data of when zooming in, it seems like the distance between peaks is the same as the width of a bar..
Nothing special happens in the code
why does this happen. both subplots should not even be aware of each other. seems like a bug to me..
figure;
subplot(211)
plot(decg.time,decg.values)
subplot(212)
h=bar(d.time,d.values)
If i understand correctly, the issue you are having is the uneven spacing between the bars in your bar plot; to fix this, i would try the following:
Set the 'barwidth' property to 1: bar(d.time, d.values, 'barwidth', 1)
change the renderer of the figure to painters: figure('renderer', 'painters');
In general, both bar and area plots tend to get "screwy" when having a large amount of data points, so resampling the data to a lower resolution might help. Also, the graphics engine has been updated in matlab 2015b, so simply updating might also help, if none of the above suggestions does.

MATLAB plot: strange horizontal lines

I am trying to produce a plot of theta vs. omega (theta on the x-axis, omega on the y-axis) and the plot I am generating looks ok except for stray horizontal lines that seem to span from -pi to pi. Does anyone know what causes this to happen, or how to remove them?
Thanks
Unless you provide your code, we won't be able to provide the good answer. These methods might help, though:
If there are sufficient points on the plot, and they are dense in the plot and less dense on the unwanted lines, you can try to insert 'bx' into plot, so the graph would consist of only blue crosses that would possibly make lines invisible retaining the remaining graph.
You can convert all the unwanted points in the matrices to NaN either manually or by adding some command like A(A>2)=NaN.

Place MATLAB legend such that it does not overlap on the plot

I am generating multiple plots of different datasets in succession using MATLAB. I would like the legend positions to be such that they don't overlap on the plotted lines and it would be ideal if this placement could be done automatically.
I am aware of setting the 'Location' to 'best' to achieve this but the placement of the legend tends to be awkward when 'best' is used (below). Also, I would like the legend to be inside the plot. I also came across a way to make the legend transparent (here) so that it does not render the plotted data invisible, but explicitly placing the legend elsewhere is what I am looking for.
Is there a way to place the legend at the extremes of the image ('NorthWest', 'SouthWest' etc) automatically such that it does not overlap on the plotted data (apart from the methods suggested above)?
So, you have tried using Location instead of Position? For example:
x =1:100;
y = x.^2;
lgd = legend('y = x.^2');
set(lgd,'Location','best')
and you are getting odd results correct? A quick way of solving this would be to still use Location, with best, and extract the coordinates:
lgd.Position
You should get something like this:
ans =
0.7734 0.3037 0.1082 0.0200
which maps to:
[left bottom width height]
You will need to focus on left and bottom. These two values, left and bottom, specify the distance from the lower left corner of the figure to the lower left corner of the legend, and they are analogous to the grid frame you are using.
Then, depending on the size of the frame (I would suggest you use axis([XMIN XMAX YMIN YMAX]) for this, if possible), you can pinpoint the position of the legend within the grid. What you can do next, is check if and which of your graphs in the plot cross paths with the legend (maybe define a relative distance function based on some distance threshold) and if they do, then randomly reposition the legend (i.e. change the values of left and bottom) and repeat until your conditions are met.
If this still troubles you I can write a short snippet. Finally, know that you can always opt for placing the legend on the outside:
set(lgd,'Location','BestOutside')

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!