Tick mark display order - matlab

Is there any way in Matlab to specify that the tick marks should appear on top of the graph?
I'm making a plot with shading for when recessions occur. I currently plot a patch object as the farthest back object in a plot, but this obscures any tick marks that occur during this window. For example:
Is there a way to get the tick marks to appear in front of this shading?

assuming you first draw the patch then the bar plot, this is what a solution would look like:
h=gca;
h.Layer='top'
This uses the layer property of the axes to force the bar plot to be on top.
If you have an older matlab version you might want to add this instead:
set(gca,'Layer','top')

Related

Colorbar properties

I am trying to plot a contour with a colorbar that has a small values (10e-9). This value appears at top of the colorbar. How can I change the location of this value from top to bottom of the colorbar. I want to move this value beneath the colorbar because when I added label above the colorbar it overlapped with this value
I attached image for the figure.
You can accomplish what you want by first finding the handle to the color bar, then changing the yticklabel ticklabels property. This is a cell array of stings, one for each tick mark. You can fill in whatever you want to show there. The multiplier at the top will go away. With the text function you can add your own modifier anywhere you want. But I think it looks nicer within the axis label.
However, the simple solution is to change the units you plot. Multiply everything by 10e9 before plotting, then add a nano prefix to your units.

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

Remove tick marks on second y-axis, but retain box on

Provided with some code like the following:
x=1:.5:10
plot(x,sin(x))
set(gca,'box','on')
I'm trying to get the left axis to maintain it's tick marks and the right axis to have none.
I know I don't want to do the following:
set(gca,'box','off')
set(gca,'Ytick','[]') %this turns off the left and right axes tick marks. I just want the right off.
I would really,really prefer to not use plotyy. Any help would be appreciated.
Are creating dumby axes the only option here?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/261486
I think that you are stuck using a dummy axes (or a variety of even more unappealing options).
FWIW, the code required is only a few lines; the shortest I can get it is below:
a1 = axes('box','on','xtick',[],'ytick',[]); %Under axis, used as background color and box;
a2 = axes(); %Over axis, used for ticks, labels, and to hold data
propLink = linkprop([a1 a2],'position'); %Keep the positions the same
x=1:.5:10 %Make and plot data
plot(a2, x,sin(x))
set(a2,'box','off','color','none'); %Set top axes props last ("plot" overrides some properties)

MATLAB: Plotting two different axes on one figure

The MATLAB surf plot below is essentially two plots plotted adjacent to each other. For clarity, I have included some code below used to prepare the plot:
band1 = horzcat(band1, eSurface2(:,:,1));
band2 = horzcat(band2, eSurface2(:,:,2));
surf(band2,'DisplayName','band2');
surf(band3,'DisplayName','band2');
I would like for the y axis numbering to restart at the start of the second graph. How do I go about doing that?
You can use the 'YTick' and 'YTickLabel' properties of the axis to control the ticks, this way you can make it start from zero for the second graph. It will require some trail and error to get it right. See the relevant doc here (you'll have to scroll all the way to the bottom of the page).
Take advantage of the following feature of 'YTickLabel': "If you do not specify enough text labels for all the tick marks, MATLAB uses all of the labels specified, then reuses the specified labels".

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

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: