X-Axis labeled in a scatter plot in Matlab - 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.

Related

Changing the tick color only in Matlab figures

I have a figure and I'd like to be able to show the ticks positions (in white) but keep the tick labels (in black). For example, if you try:
imagesc(abs(peaks(10))); colormap('bone');
set(gca,'XTick',0:pi:2*pi,'XTickLabel',{'0', 'p', '2p'},'fontname','symbol');
You can see that the tick positions can't be seen. Matlab's documentation tells that the handle YColor and XColor can be used, but they also control the color of the tick labels. For example:
I have tried to get the tick out, but it doesn't look good. I tried playing with an approach similar to the one discussed here, but without success. The last way I can think of is to "manually" rewrite the labels as text objects... Would appreciate your input.
Since there are no independent attributes for the ticks, only tailor-made tricks spring to mind.
The result of this
imagesc(abs(peaks(10)));
colormap('bone');
set(gca, 'XTick', 0:pi:2*pi, 'XTickLabel', {'0', 'p', '2p'}, 'fontname','symbol');
a = gca;
b = copyobj(a, gcf)
set(b, 'Xcolor', [1 1 1], 'YColor', [1 1 1], 'XTickLabel', [], 'YTickLabel', [])
is this

Plotting ticks and custom grid lines at the same time

I am trying to make a figure in Matlab that has grid lines at some custom places, but I also want to write ticks at regular intervals. Currently I produced the following graph, with the grid lines in the right position:
plot(myData);
xlabel('Frequency');
ylabel('Maginute');
set(gca, 'XTick', listOfTheoreticalValues);
set(gca,'XGrid', 'on');
set(gca, 'XTickLabel', '');
The problem I am facing now, however, is that I can't put normal, equally spaced ticks on the x-axis, let alone with marking values, because that would immediately add extra grid lines too. Is there a way to separate the two things from each other?
As Hugh Nolan suggested, manually adding grid lines is one way to solve the problem. The following code will do the trick:
%Grid line locations
x_lines = listOfTheoreticalValues;
y_limits = [lower_y_limit; upper_y_limit]; %Insert desired y-limits here
y_grid = repmat(y_limits, 1, numel(x_lines));
x_grid = [x_lines; x_lines];
plot(x_grid, y_grid, ':', 'color', [1,1,1]/2); %First plot grid lines
hold on
plot(myData); %Then plot data to draw data on top of grid lines
xlabel('Frequency');
ylabel('Maginute');

Using xticklabel with strange results

I have created a scatter plot and am attempting to set the xticklabel property of the set function but not all of the labels are printing on the plot. As you can see in the attached, there are 11 x values. I am including 11 string values in the set function, but only 7 are appearing on x-axis of the plot.
What am I doing wrong?
x is a 242x1 vector
haz is a 242x1 vector
ls is a 242x1 vector
scatter(x,haz,30,ls,'filled');
set(gca,'xticklabel',{'6M';'1Y';'2Y';'3Y';'4Y';'5Y'; ...
'7Y';'10Y';'15Y';'20Y';'30Y'});
title(['Implied hazard rates']);
xlabel('Tenor')
colormap('Summer');
colorbar;
hold on;
Your problem is that your tick labels don't match up with where you think the ticks should be. You need to tell the axes to put ticks at each of the x-locations in your plot, probably like this:
set(gca, 'XTick', unique(x));
% Now set your tick labels...

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

Matlab Subplot Axes

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