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...
Related
I used the semiologx command to generate these two plots, but I need the plot to show more reference values on the X axis.
How can this be accomplished in MATLAB?
It seems your plot was generated by semilogx, not semilogy. Anyway you can set the values on the x axis (called "ticks") using (for example):
set(gca, 'xtick', [1 10 100 1000])
The 1st argument is the axis handle you wish to change, in this example I just use gca which stands for "get current axes", and returns the currently active axes.
Quick version
How can I control the x- and y-values for a 3-d bar plot in Matlab?
Details
Say we have an 10 x 20 data matrix and we plot it using bar3, and we want to set the x- and y-values. For instance:
foodat = rand(10,20);
xVals = [5:14];
yVals = [-3:16];
bar3(xVals, foodat);
xlabel('x'); ylabel('y');
Is there a way to feed it the yVals as well? Otherwise the y axes always defaults to [1:N].
Note I don't just want to change the labels using XTickLabel and YTickLabel. I need to change the actual values on the axes, because I am plotting multiple things in the same figure. It isn't enough to just change how the (wrong) axis ticks are labeled. So this is different from issues like this:
How can I adjust 3-D bar grouping and y-axis labeling in MATLAB?
Other things I have tried
When I try changing the xvals with:
set(gca,'XTick', xVals)
set(gca,'YTick', yVals)
The values are taken in, but actually show up on the wrong axes, so it seems x and y axes are switched using bar3. Plus, it is too late anyway as the bar graph was already plotted with the wrong x- and y-values, so we would end up giving ticks to empty values.
Note added
Matlab tech support just emailed me to let me know about the user contributed function scatterbar3, which does what I want, in a different way than the accepted answer:
http://www.mathworks.com/matlabcentral/fileexchange/1420-scatterbar3
I found a way of doing it. Ill give you a piece of code, then you'll need to "tidy up" , mainly the axis limits and the Xticks, as bar3 does set up the Xticks inside, so if you want others you'll need to set them manually yourself.
So the trick here is to get the Xdata from the bar3 handle. The thing here is that it seems that there is a handle for each row of the data, so you need to iterate for each of them. Here is the code with the current output:
foodat = rand(20,10);
xVals = [5:14];
yVals = [-3:16];
% The values of Y are OK if called like this.
subplot(121)
bar3(yVals, foodat);
subplot(122)
h=bar3(yVals, foodat);
Xdat=get(h,'XData');
axis tight
% Widdth of barplots is 0.8
for ii=1:length(Xdat)
Xdat{ii}=Xdat{ii}+(min(xVals(:))-1)*ones(size(Xdat{ii}));
set(h(ii),'XData',Xdat{ii});
end
axis([(min(xVals(:))-0.5) (max(xVals(:))+0.5) min(yVals(:))-0.5, max(yVals(:))+0.5])
Note: Y looks different but is not.
As you can see now the X values are the ones you wanted. If you'd want other size than 1 for the intervals between them you'd need to change the code, but you can guess how probably!
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.
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).
How can I make plots in MATLAB like in below?
I won't need labels, so you can ignore them. I tried using normal 2D plot, by giving 0 to y parameter for each data points. It does help, but most of the plot remains empty/white and I don't want that.
How can I solve this problem?
Edit:
This is how I plot(playing with values of ylim does not help):
hold on
for i=1:120
if genders(v_labels(i)) == CLASS_WOMAN
plot(v_images_lda(i,:) * w_lda,0,'r*');
else
plot(v_images_lda(i,:) * w_lda,0,'b.');
end
end
title('LDA 1D Plot');
ylim([-0.2 0.2]);
hold off
One way to do this would be to adjust the 'XLim', 'YLim', and 'DataAspectRatio' properties of the axes so that it renders as essentially a single line. Here's an example:
data1 = rand(1,20)./2; %# Sample data set 1
data2 = 0.3+rand(1,20)./2; %# Sample data set 2
hAxes = axes('NextPlot','add',... %# Add subsequent plots to the axes,
'DataAspectRatio',[1 1 1],... %# match the scaling of each axis,
'XLim',[0 1],... %# set the x axis limit,
'YLim',[0 eps],... %# set the y axis limit (tiny!),
'Color','none'); %# and don't use a background color
plot(data1,0,'r*','MarkerSize',10); %# Plot data set 1
plot(data2,0,'b.','MarkerSize',10); %# Plot data set 2
And you will get the following plot:
Here's one way to reproduce your figure using dsxy2figxy and annotate. dsxy2figxy can be hard to find the first time, as it is not really in your path. It is part of the MATLAB package and is provided in the example functions. You can reach it by searching for it in the help docs and once you find it, open it and save it to a folder in your path.
h1=figure(1);clf
subplot(4,1,1);
hold on
xlim([0.2,1]);ylim([-1,1])
%arrow
[arrowX,arrowY]=dsxy2figxy([0.2,1],[0,0]);
annotation('arrow',arrowX,arrowY)
%crosses
x=[0.3,0.4,0.6,0.7,0.75];
plot(x,0,'kx','markersize',10)
%pipes
p=[0.5,0.65];
text(p,[0,0],'$$\vert$$','interpreter','latex')
%text
text([0.25,0.5,0.65],[1,-1,-1]/2,{'$$d_i$$','E[d]','$$\theta$$'},'interpreter','latex')
axis off
print('-depsc','arrowFigure')
This will produce the following figure:
This is sort of a hackish way to do it, as I've tricked MATLAB into printing just one subplot. All rasterized formats (jpeg, png, etc) will not give you the same result, as they'll all print the entire figure including where the non-declared subplots should've been. So to get this effect, it has to be an eps, and it works with it because eps uses much tighter bounding boxes... so all the meaningless whitespace is trimmed. You can then convert this to any other format you want.
Ok so the closest I have come to solving this is the following
hax = gca();
hold on
for i=1:120
if genders(v_labels(i)) == CLASS_WOMAN
plot(v_images_lda(i,:) * w_lda,0,'r*');
else
plot(v_images_lda(i,:) * w_lda,0,'b.');
end
end
set(hax, 'visible', 'off');
hax2 = axes();
set(hax2, 'color', 'none', 'ytick', [], 'ycolor', get(gcf, 'color');
pos = get(hax, 'position');
set(hax2, 'position', [pos(1), pos(2)+0.5*pos(4), pos(3), 0.5*pos(4)]);
title('LDA 1D Plot');
hold off
So in short, I hid the original axis and created a new one located at 0 of the original axis, and as I couldn't remove the y axis completely I set it's color to the background color of the figure.
You can then decide if you also want to play with the tick marks of the x-axis.
Hope this helps!
Very naive trick but a useful one.
Plot in 2d using matlab plot function. Then using edit figure properties compress it to whichever axis you need a 1D plot on !! Hope that helps :)