Draw Matlab graphs with frame, ticks, on top of graph lines - matlab

Consider something like
figure
plot(sin(0:0.01:pi))
axis tight
set(gca,'box','on','ticklength',[0.02 0.05])
then export the graph to PDF or whatever. The lines of the graph are on top of the tick labels and the axes. (Furthermore, the lines of the axes don't meet correctly, but that's another story.)
Is there a way (that can be automated) to have the axes drawn on top?

Try:
set(gca, 'Layer','top')
according to the documentation page:
Layer
{bottom} | top
Draw axis lines below or above graphics objects. Determines whether
to draw axis lines and tick marks on
top or below axes children objects for
any 2-D view (for example, when you
are looking along the x-, y-, or
z-axis). Use this property to place
grid lines and tick marks on top of
images.
and to visually see the effect (zoomed in 1200%), I save the figure as a PDF file:
Default (Layer=bottom):
with Layer=top:

Related

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')

How do you fix the size of an axes graphic object?

I'm creating a simple GUI in MATLAB and I'm trying to create a figure that contains a 2d plot on an axes, all is working well, but I have a rough time trying to figure out how to fix the position of the axes/plot.
The axes seems to scale with the figure window size. If I maximize the figure on my screen, the axes is maximized within, etc. What I want to accomplish is to have a row of buttons beneath the plot on the same figure as the plot, basically an area of 50 (ish) pixels tall that the plot does not encroach on. I know how to do this in HTML, but I can't figure out a good way in MATLAB.
Any alternative approaches would also be greatly appreciated.
Change the units of the axes to anything other than 'normalized', like 'pixels'. Then it won't automatically resize with the figure. From this documentation page:
When you create a graph, MATLABĀ® creates an axes to display the graph. The axes is sized to fit in the figure and automatically resizes as you resize the figure. MATLAB applies the automatic resize behavior only when the axes Units property is set to normalized (the default).
Use set(gca,'Position',pos) where pos = [x y w h] to set the position and size of the axes in the units you chose.
See this answer for an example and a function for holding an axis size once you have it in place.

How to place axes objects on top of a figure in MATLAB?

I'm now using MATLAB R2012b. I want to draw a grey rectangle together with the axes, and want the rectangle to be behind the axes. I tried
uistack(gca,'top');
but nothing happens.
In this article they mentioned that
However, it is possible to place axes objects on top of UIPANEL
objects (which were introduced in MATLAB 7.0 (R14)).
So I think there should be some way to do that.
Try the following:-
set(gca, 'Layer', 'top')
Ref: Draw Matlab graphs with frame, ticks, on top of graph lines

Plot lines get under the X-axis in Core Plot

I've changed the look of my plots, and now the line of the plots accidentally goes under the X-axis. The problem looks like this (notice the black lines under the axis):
How can I get rid of it? I've change the plot line like this:
aaplPlot.interpolation = CPTScatterPlotInterpolationCurved;
Plots do not draw outside the plot area. You can control its size by setting padding on the plot area frame to push the edges of the plot area inwards. You will also need to adjust the plot ranges of the plot space to keep the same appearance.

Display multiple axes on image plot

I have a large plot showing an image. Now, I want to diplay several small bar plots on top of that image, each having its own axes and positioned on a specified position on the image.
I already tried to just append more axes (with transparent background color) to the same figure. This basically works, but If I now zoom in or pan around the background image, the small axes stay on the same position relative to the figure, so they lose the relation to their position on the background image.
Does Matlab offer a better solution?
I recommend drawing the bars using patch command.
For example:
plot(rand(10));
hold on;
patch([1;1;2;2],[1;2;2;1],'r')