The default plot behaviour in Matlab is to overwrite axes and tick marks with the line to be plotted. For instance in the figure below near the origin the x-axis is overwritten and the tick-marker for 0.004 is also overwritten.
Is there a way to have the axes and tick marks have priority over the plotted line?
Change the axes Layer property to 'top'.
set(gca,'Layer','top');
Related
I want to plot ticks at logarithmic values (without labels, only the ticks). For some reason the following code doesn't plot them (set(gca,'XTick') doesn't do anything):
figure1=figure(1)
axes1 = axes('Parent',figure1,'YScale','log','XScale','log');
grid(axes1,'on');
hold(axes1,'on');
[C,h]=contourf(lamx(2:NKX+1),lamz(2:NKZ+1),phi2d(2:NKX+1,2:NKZ+1)',[10],'LineColor','none');
clabel(C,h);
axis tight;
axis([lamx(end) max(lamx) lamz(end) max(lamz)])
xlabel('\lambda_x','Fontsize',20);
ylab=ylabel('\lambda_z','Fontsize',20);
set(gca,'XTick',0.1:0.1:1)
grid on
set(gcf, 'PaperPositionMode','auto');
set(ylab, 'Units', 'Normalized', 'Position', [-0.1, 0.5, 0]);
set(gca,'linewidth',1.5,'FontSize',16)
colormap(flipud(gray(256)));
colorbar;
print('-dpng','-opengl','-r1200','2dspec')
How could the ticks be plotted?
The ticks in your example are obscured by the Contour object itself. The order in which the Axes elements are drawn relative to objects within it is controlled with the Axes Layer property.
The default value for this property is bottom which means anything like a filled contour drawn in the axes will be drawn on top of the ticks, and you won't be able to see them. The contourf function obviously knows this isn't a helpful default for plots that will obscure the ticks in their entirety because it usually changes the property to top when you call it – but only when the Axes NextPlot is set to replace. By calling hold on you also cause contourf to leave that property alone, so it ends up staying at bottom.
In your example you can deal with this in one of two ways.
As it's currently written, there no intentional effect of the hold(axes1,'on'); line, because you're only adding one plot. So you could just remove that line if your full code doesn't rely on it. Otherwise,
Set the Layer property to top from the outset when you create the axes:
axes1 = axes('Parent',figure1,'YScale','log','XScale','log','Layer','top');
In my previous question I asked how to give axes and tick marks priority over the plotted line. The correct solution was:
set(gca,'Layer','top');
However in the case that I also want to plot a grid on the figure this gives the grid priority over the plotted lines as shown in the attached figure when exported as an .eps file. This is undesirable and leads to a dashed appearance to the blue line.
How can I give priority to axes and tick marks but not give priority to the grid?
The 'Layer' property of an axes object controls the layering of the axes, tick marks, and grid lines, so they can't be layered separately. Your options are:
Stack a couple of axes on top of each other with the same limits, with the bottom axes having grid lines and no data plotted and the top axes having your data and axes ticks that are layered on top.
Plot your grid lines yourself first, then plot your data on top of them, For example:
[xGridv, yGridv] = meshgrid([0.1 0.2 0.3], [0 1.7]); % Vertical grid lines
[yGridh, xGridh] = meshgrid([0.5 1 1.5], [0 0.32]); % Horizontal grid lines
hold on;
plot(xGridv, yGridv, 'k:');
plot(xGridh, yGridh, 'k:');
% Plot your data
Hi I'm trying to implement as following code.
plot(bins,r);
plot(bins,g);
plot(bins,b);
But I want to plot in one figure.
Is there any way?
For multiple plots in the same figure and not the same axis. You have to use subplot(x,y,z). The first argument 'x' is the number of plot you want to produce, in your case 3. Second 'y' just adjusts the size of the plots, you can use 1. The third 'z' is the position of the plot, whether a certain plot comes first, second or third.
subplot(3,1,1)
plot(bins,r);
subplot(3,1,2)
plot(bins,g);
subplot(3,1,3)
plot(bins,g);
To distinguish between all three plot you can add another argument to plot() so that you can change colors. For example:
plot(bins,r,'r')
'r' will make the color of the plot red, 'b' makes it blue, 'k' makes it black...so on.
Yes, you can plot everything in one go:
plot(bins,r,bins,g,bins,b)
or use hold on after the first call to plot.
You need to use hold on
hold on retains plots in the current axes so that new plots added to
the axes do not delete existing plots. New plots use the next colors
and line styles based on the ColorOrder and LineStyleOrder properties
of the axes. MATLAB® adjusts axes limits, tick marks, and tick labels
to display the full range of data.
hold on
plot(bins,r)
plot(bins,g)
plot(bins,b)
I have a Matlab GUI with 3 axes components. Their tags are predicted_ax, cost_ax and error_ax. I want to draw vertical line on particular position on the first axes component (the one with tag predicted_ax). How do I do that?
I tried this code:
ylim = get(handles.predicted_ax, 'ylim');
line([linePos, linePos], ylim);
But it draws the line on different axes (the ones with tag error_ax)! I'm sure I did not confuse tags or axes components. In the fact another test
ylim = get(handles.cost_ax, 'ylim');
line([linePos, linePos], ylim);
gives exactly the same result: the line is drawn on the last axes component with tag error_ax. So how do I draw the line on the right axes?
You need to set the 'parent' property of the line, as by default it will always be the current axis:
h = line([linePos, linePos], ylim);
set(h, 'parent', handles.predicted_ax);
I think you need to use the axes command to set the current axis on which the line will be drawn. Try axes(handles.predicted_ax); prior to your line command.
(Getting the ylim value for the axis apparently does not make it current.)
I use datetick('x','HH:MM:SS.FFF'). How can I specify how many labels (specially denotes points) on the x-axis it puts?
datetick replaces the labels of the current tick marks with formatted date/time strings.
You control the actual location of the ticks using axes properties
XTick, YTick, ZTick: vector of data values locating tick marks
set(gca,'xtick', [1 4 6]);%sets ticks at x=1, x=4, x=6
gca is "get current axes", so this will change the xtick locations on the most recent axes you've activated (created, clicked on, or however else). If you have a axes handle to another set of axes, you can use that handle in place of "gca".