How to remove xticks and yticks from all axes? - matlab

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',[]);

Related

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

Remove xticks but keep xticklabels in 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])

Different right and left axes in a MATLAB plot?

I plot a single trace in MATLAB with plot(). I'd like to add a right-y axis with a different set of tick marks (scaled linearly). Is this possible?
There are a number of good suggestions on this closely related question, although they deal with a more complicated situation than yours. If you want a super-simple DIY solution, you can try this:
plot(rand(1, 10)); % Plot some random data
ylabel(gca, 'scale 1'); % Add a label to the left y axis
set(gca, 'Box', 'off'); % Turn off the box surrounding the whole axes
axesPosition = get(gca, 'Position'); % Get the current axes position
hNewAxes = axes('Position', axesPosition, ... % Place a new axes on top...
'Color', 'none', ... % ... with no background color
'YLim', [0 10], ... % ... and a different scale
'YAxisLocation', 'right', ... % ... located on the right
'XTick', [], ... % ... with no x tick marks
'Box', 'off'); % ... and no surrounding box
ylabel(hNewAxes, 'scale 2'); % Add a label to the right y axis
And here's what you should get:
You may try this submission to MATLAB File Exchange - PLOT2AXES.
PLOT2AXES example http://www.mathworks.com/matlabcentral/fx_files/7426/2/plot2axes.png
Jiro's solution is good (file Exchange function), however, it does not allow to use Matlab's built-in plot functions (bar, scatter, etc.), and you have to use plot2axes instead. Matlab's own help gives the solution to have two axes on any type of plots:
ax2 = axes('Position',get(ax1,'Position'),...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none',...
'XColor','k','YColor','k');
Look at: http://www.mathworks.com/help/techdoc/creating_plots/f1-11215.html
From matlab 2016 and onwards there is an option to define on what axis one plots:
yyaxis left
plots...
yyaxis right
plots...
source:
https://se.mathworks.com/help/matlab/ref/yyaxis.html
Open MATLAB Help with F1 and take a look at the functions below function plot which you mentioned, there you will see plotyy. This is what you probably need.
UPDATE: actually plotyy is NOT the answer to the question as pointed by gnovice.
I was able to do it with the following after plotting the left axis graph:
yyaxis right
ylabel('Right axis label')
plot(x,y1) % plot your right axis graph
Hope it helps.