How do I plot data labels alongside my data in a bar graph - matlab

I have a bar graph in which i would like to plot data labels alongside my data points. I have looked at this documentation and they don't seem to have what i need. This is all done using MATLAB.
Below is an example of what i'd like, although for a bar graph instead of a scatter plot.

Use TEXT function to label the bars. STRCAT function can be used to create custom labels.
x = (1:5)';
y = rand(5,1);
bar(x,y)
%# show X and Y coordinates
text(x,y,strcat('(',num2str(x),',',num2str(y,2),')'),...
'horiz','center','vert','bottom')
You can also add some small gap to y coordinates to make text a little higher.

Use the code below and customize in your case.
for ii = 1:numel(X)
text(X(ii)+.02, Y(ii)+.02,textCell{ii},'FontSize',8)
end

Related

How to switch Matlab plot tick labels to scientific form?

I have a semilogarithmic plot which works so far with semilogx. Now I would like to change the tick labels. Now it says 10^8 10^9 ... 10^13, but I would like to see 1e8, 1e9, ... 1e13 on the x-axis. How can I change that?
Cheers
Manuel
You can change the XTickLabels property using your own format:
set(gca,'XTickLabels',sprintfc('1e%i',0:numel(xt)-1))
where sprintfc is an undocumented function creating cell arrays filled with custom strings and xt is the XTick you have fetched from the current axis in order to know how many of them there are.
Example with dummy data:
clear
clc
close all
x = 0:100000;
y = log(x);
figure
semilogx(x,y)
xt = get(gca,'XTick');
set(gca,'XTickLabels',sprintfc('1e%i',0:numel(xt)-1))
Output:

include legend in matlab compass plot

I am using the compass command in matlab to plot wind speeds and direction. I would like to alter the default version to (1) remove the labels within the compass, and (2) draw a legend outside the compass plot to demonstrate the magnitude of each arrow.
Specifically, using the compass is it possible to include a legend which describes the magnitude of the arrows instead of having the values defined on the figure? For example:
rng(0,'twister') % initialize random number generator
M = randn(20,20);
Z = eig(M);
figure
compass(Z)
This is a normal compass plot where the magnitude of each entry is shown by labels on the figure, here they are 1:5. I can remove the labels with:
h = findall(gca,'type','text'); % Find all handles to text labels
legit = {'0','30','60','90','120','150','180','210','240','270','300','330','360',''}; % Define what to keep
idx = ~ismember(get(h,'string'),legit); % Take the others and set them to empty string
set(h(idx),'string','');
However, I would now like to include a legend which demonstrates the length of an arrow with a size of say 2 would be. Any ideas on how to do this?
Try some version of this -- after creating the data:
u=abs(Z)
figure(1)
compass(Z)
set(findobj(gcf,'type','text','-and','fontsize',10),'string','')
legend({['z_1 = ',num2str(u(1)),' units'],...
['z_2 = ',num2str(u(2)),' units']},...
'location','southoutside')
I used compass() a lot, but actually i'm not so sure that the font size of the labels are 10, in my (many) compass plots i have used this as a parameter and it worked fine. If it don't, just use the parameter text that findobj() will get all of the text in the figure as well.

Making an accurate colorbar for a simple plot

I am trying to make a simple plot (for this example doing a plot of y=x^2 will suffice) where I want to set the colors of the points based on their magnitude given some colormap.
Following along my simple example say I had:
x = 1:10;
y = x.^2;
Use gscatter(x,y,jet(10)); legend hide; colorbar which produces a plot with the points colored but the colorbar does not agree with the colored values. (Can't post picture as this is my first post). Using a caxis([1,100]) command gives the right range but the colors are still off.
So I have two questions:
(1) How can I fix the colors to fit to a colorbar given a range? In my real data, I am looking at values that range from -50 to 50 in some instances and have many more data points.
(2) I want to create a different plot with the same points (but on different axes) and I want the colors of each point on this new plot to have the same colors as their counterparts in the previous plot. How can I, programmatically, extract the color from each point so I can plot it on two different sets of axes?
I would just move the points into a matrix and do an imagesc() command but they aren't spaced as integers or equally so simple scaling wouldn't work either.
Thanks for any help!
Regarding you first question, you need to interpolate the y values into a linear index to the colormap. Something like:
x = 1:10;
y = x.^4;
csize = 128;
cmap = jet(csize);
ind = interp1(linspace(min(y),max(y),csize),1:csize,y,'nearest');
scatter(x,y,14,cmap(ind,:),'filled')
colorbar
caxis([min(y) max(y)])
Using interp1 in this case is an overkill; you could calculate it directly. However, I think in this way it is clearer.
I think it also answers your 2nd question, since you have the index of the color of each data point, so you can use it again in the same way.

Matlab bar plot grouped but in different y scales

I have two sets of data, and I want to plot using bar graph. But the problem is these two sets of data are at quite different scale. If I just use the bar(A), it would look like this: grouped but the second data set is barely visible because the scale.
However, if I use the plotyy(x,y1,x,y2), the plot will be like this: two sets of data are in different scale, but the bar graphs are not grouped, the second data sets overlaps to the first.
So I am wondering if there is a way to plot the bar graph grouped like the first figure, but the two datasets are using separate y scales? Or is there a way to adjust the horizontal offset of the bar graph in second plot so it looks like "grouped".
Thanks!
This uses the plotyy(x1,y1,x2,y2,fun1,fun2) variant of plotyy:
%// Set these three variables as desired
offset = (x(2)-x(1))/8;
width = (x(2)-x(1))/4;
colors = {'b','g'};
%// Do the plot
plotyy(x-offset,y1,x+offset,y2, #(x,y) bar(x,y,width,colors{1}), #(x,y) bar(x,y,width,colors{2}));
If you prefer x-ticks to appear only on used x values:
h = plotyy(x-offset,y1,x+offset,y2, #(x,y) bar(x,y,width,colors{1}), #(x,y) bar(x,y,width,colors{2}));
set(h,'xtick',x)

change yTicklabel in matlab plot

I want to draw a plot that displays y tick labels are similar to the below image. How can I do that?
thanks for your attentions
The y axis is setup as logarithmic.
you can create plots similar to this using
semilogy(xData,yData)
If you wanted the plot to look the same, you would of course need to use the semilogy to plot the data and then also add x and y axis labels using something like the following
xlabel('Fitness Evaluations');
ylabel('Error');
If you truly want to only show the powers of 10 on the y axis, meaning the power number, you can do something like as follows
labels = sort(str2num(get(gca,'YTickLabel')));
set(gca,'YTickLabel',labels);
labels = [' 1 ';'10 '; '100'];
set(gca,'YTickLabel',lbls);
This will set the YtickLabels to 1, 10 and 100. If you want LaTeX to interpret your ticklabels such that you will see (10⁰,10¹,10²), you can download this file from matlab exchange.
http://www.mathworks.com/matlabcentral/fileexchange/15986