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');
Related
I am trying to line up a plot of a signal with an image where the image has a colorbar. The colorbar causes the axes to be offset horizontally.
My intuitive approach would be to fix the size of figures to something, like in Gnuplot with papersize. However, not sure which would be the best fit here.
To Adjust Scaling to Square in Full Screen Mode of Matlab?
I want to maintain the relations between the two figures. I cannot use squareform in the first figure for some reason, while I can in the latter figure.
Code
figure
ax2=subplot(2,2,2);
plot(mat2gray(pdist(data, 'correlation')));
title('Corr pdist');
cbar2 = colorbar(ax2);
xlim([0 size(mat2gray(pdist(data, 'correlation')),2)]);
set(cbar2, 'Visible', 'off');
ax4=subplot(2,2,4);
imshow(squareform( mat2gray(pdist(data, 'correlation')), 'tomatrix') );
colormap('parula'); colorbar;
title('Square Corr pdist');
Wrong Scaling in Output when Full Screen Mode of Matlab where you see the colorbar method is not sufficient for holding relations as proposed in the answer here about How to Control Relative Size of Figures with Colorbar in Matlab?
Right Scaling in Output when Default View
How can you maintain the square view of figures in the Full Screen Mode of Matlab?
I would simply create a colorbar for the top axes as well and set the visibility to off.
figure;
ax1 = subplot(2,1,1);
ax2 = subplot(2,1,2);
cbar1 = colorbar(ax1);
cbar2 = colorbar(ax2);
set(cbar1, 'Visible', 'off')
The benefit here is that you will get consistent behavior when resizing the figures, etc. because the size and position of the two axes will be rendered in the same way.
The other thing you will need to remember is to keep the axes the same in all aspects. So for example if you have an image in the bottom axes (using imshow), MATLAB by default sets the axes to a square. To get your top plot to also be square you will need to use axis square. Then they will continue to line up.
axis(ax1, 'square')
When the graph below is plotted, NSS1 which is simply a constant set equal to one is right on the top border of the graph and thus hard to see.
How can I change the length of the y-axis to say 1.2 so that the NSS1 can be seen more clearly?
lambda=5;
tau=0:30;
tau(1)=0.000001;
NSS1=1*ones(1,31);
NSS2=(1-exp(-tau/lambda))./(tau/lambda);
NSS3=((1-exp(-tau/lambda))./(tau/lambda)-exp(-tau/lambda));
%ylim([0, 1.2])
plot(tau,NSS1,'-k*',tau,NSS2,'-k+',tau,NSS3,'-ko');
xlabel('t = 0 to 30y', 'FontSize',30)
ylabel('yield','FontSize',30)
The reason why ylim doesn't work if you put it before the plot command is that there is no axes object it can relate to.
So there are two options:
First, you create an axes object and hold it with hold on, so the upcoming plot is plotted on the same axis.
ax = axes; hold on;
ylim([0, 1.2])
plot(tau,NSS1,'-k*',tau,NSS2,'-k+',tau,NSS3,'-ko');
or second, you plot first, the command automatically generates an axes object and you can modify its y-limits afterwards:
plot(tau,NSS1,'-k*',tau,NSS2,'-k+',tau,NSS3,'-ko');
ylim([0, 1.2])
I want to make a Matlab GUI program.
When i use the axes to display the image, there are the axes number around the axes.
How to delete it?
so my GUI program will display the axes without any coordinates around the axes.
here is my code for display an image in axes.
axes(handles.axes16);
handles.image_gray = image_gray;
imshow(image_gray);
guidata(hObject, handles);
And here is the axes coordinates that i meant.
Remember that the axes is an handle object with many properties. I would recommend setting the axes properties 'xtick', and 'ytick', to an empty array. That way, you preserve the border, and background color of the axes. Simply turning the axes off will make your lines render on top of the background figure, which may or may not be the effect you are looking for.
Example:
set(handles.axes16,'xtick',[],'ytick',[])
A quick way to turn off the axis is, well, axis off:
Example
figure;
plot([-10:10],randn(21,1));
xlabel('x');
ylabel('y');
Now turn axis off:
axis off
I'm having a bit of a problem with what's happening to the axes of 9 plots that get subplotted together. I'm using subplot(3,3,x) to make a 3x3 grid of 9 plots, and custom labeling the ticks of the axes with
set(gca, 'XTickLabel', {'0,0','0,1','0,2','1,0','1,1','1,2','2,0','2,1','2,2'});
set(gca, 'YTickLabel', {'0,0','0,1','0,2','1,0','1,1','1,2','2,0','2,1','2,2'});
and the problem is that not all of the ticks specified show up on the subplots -- only about half of them, and they show up in the wrong places, at that.
I'm guessing this is matlab thinking that there isn't enough room to put all of the ticks and labels and showing a squeezed subset as a result, but it would look fine if it just did it. how do I make it all show up??
You can set the 'Xtick' & 'Ytick' property of the figure's axes. They define which ticks will be visible. In your case you want to show the first 9 xticks and the first 9 yticks - the following command will do it:
set(gca,'Xtick',1:9, 'Ytick',1:9)
In case you want to show every 2nd tick you would use:
set(gca,'Xtick',1:2:9,'Ytick',1:2:9)
Hope this helps.
You set custom tick labels with those commands, and they show up where the ticks are at that moment. You can see what the ticks are with
get(gca,'YTick');
For example:
plot(-2:2)
get(gca,'YTick');
returns [-2 -1.5 -1 -0.5 0 0.5 1 1.5 2]. If you now use
set(gca,'yticklabel',{'a','b','c','d','e'})
then those letters will appear at all ticks, starting from the first (-2) and since there are more ticks than ticklabels, the ticklabels will repeat, as you can see:
So these are ticks, but maybe you meant to just use labels, which I add with the following:
ylabel('this is the ylabel');
xlabel('and this the xlabel');
Play around with it and learn what's going, it's not that hard ;)
PS: with subplot, you can create different axes and set different ticks for each axes object separately. By default the axes are not linked or something, but completely independent! When you use gca, it returns the current axes, ie with subplot: the last one created or selected with subplot(3,3,x)!
So if you want to set ticks, labels are anything else on all the axes, you'll have to do it for all separately, ie:
subplot(3,3,1);
xlabel('x');
ylabel('y');
title('subplot (1,1)');
set(gca,'xticklabel',{'a','b','c'});
subplot(3,3,2);
xlabel('x');
ylabel('y');
title('subplot (1,2)');
subplot(3,3,1);
xlabel('x');
ylabel('y');
title('subplot (1,1)');
etc.
It's a matter of space. Matlab will show more ticks if you increase the size of the plot window, and viceversa. You can also reduce the font size in order to fit more ticks on the axes (try with set(gca,'FontSize',5) or any other font size value).
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.)