Remove xticks but keep xticklabels in MATLAB - matlab

I have a plot in MATLAB from which I would like to remove the xticks but keep the xticklabels. If I just remove the xtick like so:
set(gca, 'XTick', []);
...then the labels also disappear. Is there a way to keep the labels, without having to manually recreate them with text boxes? I thought about trying to make the length of the xticks zero, but this answer suggests that xtick properties cannot be independently controlled.

Try modifying the TickLength property:
set(gca, 'Ticklength', [0 0])

Related

How to remove xticks and yticks from all axes?

I have three axes in figure and I want to remove xtick and ytick from all of them.
I wrote below code but it works just on current axes, not all of them:
set(gca,'xtick',[],'ytick',[]);
How to remove xticks and yticks from all axes?
As a more general solution inspired by #Luis Mendo's answer, use findobj to get the axes. This will avoid getting all children of the parent figure which could include "non-axes" elements:
set( findobj( gcf, 'Type', 'axes' ), 'XTick', [], 'YTick', [] );
This should work:
set(get(gcf,'Children'),'Xtick',[],'Ytick',[]);

Matlab and XTickLabel

I've been trying to get Matlab to change the labelling on my contourf plots for about an hour now. When I go to change XTickLabel or XTick, it simply removes my x-axis altogether! The frustrating and infuriating thing is that I'm doing exactly what all the help pages and help forums are asking me to do - I honestly don't understand why this is not working.
Hence, I am here.
My plotting code (knowledge of the function shouldn't be required - the code is rather intense. It is, however, a 2D contourf plot with valid data and ranges - the axes are the issue, not the graph):
contourf(time,f,power,levels)
colormap(jet(levels))
set(gca,'XTickLabelMode','manual')
set(gca, 'XTick', 0:23);
set(gca, 'XTickLabel', {'0';'1';'23'});
xlabel('Time (UT)')
ylabel('Frequency (Hz)')
caxis([0,8])
axis([0 StopTime 0 0.1])
Any help would be greatly appreciated!
Solved:
I realized that the 'XTick' relied on current values of the array I was using to define the x-axis. I can't just assume matlab will evenly space a new array (at least, if there's a way to do that, I don't know). So, since I have 85,680 data points on my X-axis, I simply rescaled it by:
set(gca, 'XTick', 0:3570:85680)
set(gca, 'XTickLabel', num2cell(0:24))
Moral of the story: Matlab doesn't let you arbitrarily stick a new axis over an old one using these two functions.
You have a final axis([0 StopTime 0 0.1])) command which clears your plot, by creating a fresh new axis. That's why all your existing plots are gone. Try removing it:
contourf(time,f,power,levels)
colormap(jet(levels))
set(gca,'XTickLabelMode','manual')
set(gca, 'XTick', 0:23);
set(gca, 'XTickLabel', {'0';'1';'23'});
xlabel('Time (UT)')
ylabel('Frequency (Hz)')
caxis([0,8])
Now the question becomes: are your ticks sensibly placed for the data you are representing? Without knowing the data I cannot answer this for you. So the ball is in your court now. ;)
You can use cell arrays to define the ticks and tick-labels and then use them with set function call, to make it more elegant -
xtick_label_cellarr = num2cell(0:24)
xtick_cellarr = linspace(0,85680,numel(xtick_label_cellarr))
set(gca, 'XTick',xtick_cellarr)
set(gca, 'XTickLabel',xtick_label_cellarr)

X-Axis labeled in a scatter plot in Matlab

I would like to do a scatter plot with two populations A and B. I am currently using zeros and ones to generate the scatter plot. So A is line up at x=0 and B and x=1. Is it possible to delete numbers from the x-axis and just add a string? So that it looks like a histogram?
Yes. if you get a handle to the axes you can use the XTick and XTickLabel properties e.g.
set(gca, 'XTick', [], 'XTickLabel', []);
to remove them entirely, or
set(gca, 'XTick', [0 1], 'XTickLabel', {'this one', 'that one'});
Or just play with properties until you find something you like ;)
(you can also fiddle with things via the figure property editor in the GUI if you don't want to do it programmatically)
Something like this should do the trick:
scatter(x,y);
labels = {'A', 'B'}
set(gca,'XTick',0:1)
set(gca,'XTickLabel',labels)
set(gca,'XTick',0:1) is used to only place ticks on 0 and 1. Similarly, for all integers within range: 0:max(x).
set(gca,'XTickLabel',labels) is used to change the name of the ticks. Note that the length of labels must be equal to the number of ticks.

Plot overrides axes property 'XTick'

I am creating a GUI in Matlab. I have several axes in which I plot different graphs. I have set in some of the axes the property XTick to []. However, each time I plot a new graph in the same axes, the xticks appear again. I know I can delete them by using set:
set(handles.axes_0, 'XTick', []);
However, this creates a "flickering" effect: you see the ticks appearing and then dissapearing each time I plot something new.
Do you know how could I have an axes with the XTick disabled avoiding the flickering effect?
Some basic code:
figure(1); %create new figure
set(gca, 'XTick', []); %Disable xtick
plot([1 2 ], [2, 3]); %Plot something. Xtick appears again
set(gca, 'XTick', []); %Disable xtick until next plot
As Shai pointed out in a comment, when using hold on the ticks don't reappear. As I want to clean the previous plot before drawing the new one, I search for its identifier using findobj and then delete it. Finally, I draw the new plot with hold on. Example (suppose the axes handle is called handles.axes_0):
h = findobj(handles.axes_0,'Type','line');
if ~isempty(h)
delete(h);
end
hold on
plot(handles.axes_0,x,y);
hold off

MATLAB: Plotting Time on Xaxis - overlapping label

I am having difficulties on plotting time on the xaxis. I have some overlapping labels. See below:
This is my code:
time=datenum(0,0,0,0,0,timeinseconds);
labs=1:10:length(time);
figure(3);
plotyy(time,xvalue,time,dens);
datetick('x','HH:MM');
set(gca,'XTick',time(labs),'XTickLabel',time(labs));
legend('xval','CDF');
title('Crash on Oct.10 2008 at 15:59pm');
xlabel('Time');
First, why are the labels overlapping with the old ones? And secondly, how I can get the label to rotate 90degrees? I tried some other matlab functions to turn the labels but none seem able to tackle time format labels.
Calling plotyy you create two axis objects. Your overlap problem does probably come from the fact that you modify only one set of those axis, while leaving the other as it was originally set up.
One option is handling both of the created axis when you call plotyy by:
[AX, H1, H2] = plotyy( time, xvalue, time, dens);
Your first option here is setting up both of the axis, contained within the array of handlers AX, via changing the'XTick' propriety as:
set( AX(1), 'XTick', time(labs), 'XTickLabel', time(labs));
set( AX(2), 'XTick', time(labs), 'XTickLabel', time(labs));
But you also have the option of leaving the labels for the second axis empty, replacing the second line above:
set( AX(1), 'XTick', time(labs), 'XTickLabel', time(labs));
set( AX(2), 'XTick', time(labs), 'XTickLabel', []);
The official documentation of plotyy and Using Multiple X- and Y-Axes can be of further help for you.
If you take a look on the example there, namely, plotyy documentation:
figure
x = 0:0.01:20;
y1 = 200*exp(-0.05*x).*sin(x);
y2 = 0.8*exp(-0.5*x).*sin(10*x);
[AX,H1,H2] = plotyy(x,y1,x,y2,'plot');
and as you did before, try to modify only the AX(2), which is equivalent to what you got writing gca:
set(AX(2),'XtickLabel',1:0.1:20)
you will observe that the same overlapping error takes place.
With respect to rotating the labels 90 deg, I'm afraid that's not currently supported by Matlab. However, you can probably get that done using one of the available packages on FileExchange. Either xticklabelrotate or Rotate Tick Label could be the one.