Matlab: Missing labels in bar chart - matlab

With Matlab 2012 and 2013, I found that setting XTickLabel on a bar chart only works with up to 15 bars. If there are more bars, labels are missing, as shown below.
Plotting 15 bars:
N = 15;
x = 1:N;
labels = num2str(x', '%d');
bar(x);
set(gca, 'XTickLabel', labels);
Plotting 16 bars:
N = 16;
x = 1:N;
labels = num2str(x', '%d');
bar(x);
set(gca, 'XTickLabel', labels);
For N > 15, it will always only display 10 labels.
Does anyone else experience this? Any work-arounds? I need all labels because I am plotting discrete categories and not a continuous function.

This happens because the tick labels have to match the ticks themselves. In the example you gave with N = 16; and x = 1:N;, MATLAB automatically makes the following XTicks (on your and my machines, at least):
>> xticks = get(gca,'xtick')
xticks =
0 2 4 6 8 10 12 14 16 18
>> numel(xticks)
ans =
10
Just 10 ticks for the 16 different bars. Thus, when you run set(gca, 'XTickLabel', labels); with labels = num2str(x', '%d'); (16 labels), it gives the second figure you showed with the wrong labels and ticks before/after the bars (at positions 0 and 18).
To set a tick label for each bar, you also need to set the ticks to match:
set(gca,'XTick',x) % this alone should be enough
set(gca,'XTickLabel',labels);
Then you will get the desired result:
For whatever reason, 16 seems to be the magic number at which MathWorks decided XTicks should not be drawn for each bar, leaving it up to the user to set them if needed.

Related

Equally spaced x-value for values that are not equally spaced

I'm trying to display a discrete plot with values on the x-axis that are not equally space but I want them to appear equally spaced. I would like a stem plot with the first stick not on the y-axis, and I'd also like to have an horizontal dashed line at y=1.
So far here is what I tried.
x = [10 50 150 3000];
y = [.6 .754 .853 .954];
xv = [1 2 3 4];
stem(xv,y);
set(gca,'XTickLabel',x);
Unfortunately, this is not what I expected. The value on the x-axis are not right and the sticks start on the y-axis and end on the figure edge.
How can I fix this?
EDIT: I initially forgot the horizontal dashed line. Added this.
You just need two tiny additions:
x = [10 50 150 3000];
y = [.6 .754 .853 .954];
xv = [1 2 3 4];
stem(xv, y);
xlim([min(xv)-1 max(xv)+1]); % <--
set(gca, 'xtick', xv); % <--
set(gca, 'xticklabel', x);
You (also) need to explicitly set the xtick option, so that only these ticks are drawn, and no other.
With xlim, you can manipulate the x-axis limits. (Left and right limit might be modified to your needs.)
To add the horizontal dashed line, just add the following at the end:
hold on;
plot([min(xv)-1 max(xv)+1], [1 1], 'k--');
hold off;
(Start and end points of the line might be modified to your needs.)
From Matlab R2018b on, you could also use yline.
The output then looks like this:
When you have a sequence of values that you want to plot equally spaced without any special treatment to what each value actually is, you're essentially defining a set of categories.
MATLAB is good at handling these nicely without any extra trickery to lay them out uniformly on your axes if you declare the values explicitly as categorical.
All you need, therefore, is:
x = [10 50 150 3000];
y = [.6 .754 .853 .954];
stem(categorical(x),y);
yline(1,'--');
ylim([0 1.5]) % Make some space on the y-axis so the horizontal line doesn't sit on the top edge

Control colorbar scale in MATLAB

Question: How do I specify color transitions in a custom MATLAB colorbar?
Specifically, I'd like to make the yellow (see below) cover more area of the colorbar (perhaps [19.5–21.5] or something close to that).
Using this answer, I was able to create a custom colorbar in MATLAB. I'm trying to understand this answer as it might be relevant.
I have attempted approaches from this answer and reviewed this answer & this one and was unable to accomplish my goal.
It is clear I am missing something.
Full representative example below
% MATLAB 2017a
% Data
X = [22.6 22.8 22.6 20.45 22.3 18.15 19.95 20.8].';
Y = [84 89 63 81 68 83 77 52].';
Z = [23.0 22.695 21.1450 21.5 22.09 20.5 22.075 20.915].';
% Create custom colormap
% Reference: https://stackoverflow.com/questions/24488378/how-to-map-a-specific-value-into-rgb-color-code-in-matlab/24488819#24488819
col3 = [0 1 0]; %G
col2 = [1 1 0]; %Y
col1 = [1 0 0]; %R
n1 = 20; n2 = 20;
cmap=[linspace(col1(1),col2(1),n1);linspace(col1(2),col2(2),n1);linspace(col1(3),col2(3),n1)];
cmap(:,end+1:end+n2)=[linspace(col2(1),col3(1),n2);linspace(col2(2),col3(2),n2);linspace(col2(3),col3(3),n2)];
cmap = cmap.';
% Plot
colormap(cmap), hold on, box on
p = scatter(X,Y,[],Z,'filled','DisplayName','Data3');
cb = colorbar;
cb.Limits = [18 23];
cb.Ticks = [18:1:23];
% Cosmetics
p.MarkerEdgeColor = 'k';
xlabel('X')
ylabel('Y')
cb.Label.String = 'Z';
I think all that you're missing is a call to caxis to specify the minimum and maximum values to map the color range to:
caxis([18 23]);
Note that the following line...
cb.Limits = [18 23];
... only changes the tick limits displayed on the colorbar, but doesn't change anything about how the data is mapped to the color range. The caxis function is how you control that (in the above case, mapping the value of 18 to one end and the value of 23 to the other). By default, your code was mapping the minimum and maximum values in Z to the color range (20.5 and 23, respectively). When you then set the tick limits on the color bar to a larger range, it just filled it in with the last color in the color map, in this case red. That's why you see so much of it.
Bonus
Just because you might be interested, you could also use interpolation via the interp1 function to easily generate your color map like so:
cmap = interp1([1 0 0; 1 1 0; 0 1 0], linspace(1, 3, 41));

Matlab bars: changing the color of the bar and the space between bars and axes

I have a tricky question, which I think cannot be simply answered.
I have a large data table and want to save bar charts for each 2 corresponding mean values. I already did that with "for loop", but I cannot set different colors to my bars and cannot set proper spaces between bars and axes.
That's the simplest example:
k = [2 5]
bar(k)
Matlab thinks about those data [2 5] as about one "group" and it does not give a possibility of changing color for only one bar.
Of course, when we have different "groups", colors are changeable.
y = bar([1 2 3; 4 5 6]);
y(2).FaceColor = 'red';
But what about my example? I need only two bars, and I need to color them differently. Also, I want to set a small space between each bar and the axes (and again with two bars it is not that simple, and I cannot use "bar width" because that doesn't give me what I really want).
Does anyone know how to get around this?
Thanks for any reply!
Mary
A good trick could be add zero column for each group:
k = [2 5;0 0];
bar(k,'grouped')
UPDATE
Another solution to have different color could be using location in bar:
k = [2 5];
figure(1);
hold on;
h=bar(1,k(1));
set(h,'FaceColor','k');
h=bar(2,k(2));
set(h,'FaceColor','b');
hold off
As far as I know, this requires the use of a custom bar plot, since Matlab's barplots use dynamic colormaps. Personally, I like the GRAMM toolbox for all these customizations: [GRAMM on file-exchange] [GRAMM on github].
Here's an example:
x = [1 2];
y = [1 1.5];
c = x;
custom_map = ...
[1 0.3673 0.4132;
0 0.7375 0.8344];
clear g
g(1,1)=gramm('x',x,'y',y,'color',c);
g(1,1).geom_bar('width',0.2);
g(1,1).set_title('Width = 0.2');
g(1,2)=gramm('x',x,'y',y,'color',c);
g(1,2).geom_bar('width',0.8);
g(1,2).set_title('Width = 0.8');
g.set_names('y','','x','');
g.set_color_options('map',custom_map);
g.axe_property('YTick',[],'LineWidth',1,'YColor',[1 1 1],'XLim',[0.5 2.5],'XTick',[1 2])
figure('Position',[100 100 600 300]);
g.draw();
The histogram function is one option that will easily handle the spacing and x-axis labeling, but you'll have to plot each bar separately to independently control the colors:
k = [2 5];
histogram('Categories', {'ONE'}, 'BinCounts', k(1), 'BarWidth', 0.8, ...
'FaceAlpha', 1, 'FaceColor', [0 0.4470 0.7410]);
hold on;
histogram('Categories', {'TWO'}, 'BinCounts', k(2), 'BarWidth', 0.8, ...
'FaceAlpha', 1, 'FaceColor', [0.8500 0.3250 0.0980]);
Here is an option to workaround this. You can use diag(k) to create a 2*2 matrix with all zeros except the main diagonal that will be with your values, and Matlab will read it as 2 different groups. Then you change the Xdata of the 'dummy' bars (which you don't see but take some space on the x axis to nan so Matlab will ignore it.
k = [2 5];
b = bar(diag(k));
set(b,{'XData'},{[1 nan],[nan 1]}); % remove the group you don't want
set(gca,{'xticklabel','XTick'},{{'ONE','TWO'},[0.85 1.15]});
b(2).FaceColor = 'r'; % choose a different color
The result:
And if you want to generalize this to more groups (here it's 5):
k = 1:5;
data = diag(k);
xdata = eye(numel(k))./eye(numel(k)); % a martix of nan with 1 on the main diagonal
b = bar(diag(k));
% remove all data except one bin in all groups:
set(b,{'XData'},mat2cell(xdata,ones(size(data,1),1),size(data,2)))
X = xlim; 5 get x-axis limits
w = (1-X(1))/(numel(k)/2); % calculate the width of one bin
set(gca,'XTick',X(1)+w/2:w:X(2)) % set the X ticks to the center of the bins
set(gca,'xticklabel',{'ONE','TWO','THREE','FOUR','FIVE'}); % set the labels
set(b,{'FaceColor'},mat2cell(lines(numel(k)),ones(size(data,1),1),3)); % set the colors
and you get:
And finally, if you want them to 'hover' a little above the x-axis, you can add the following lines:
b(1).BaseLine.Color = 'none'; % remove the base line
ylim([-0.1 max(k(:))]) % shift the bars up a little
and get:

How do I change the number of ticks in matlab histogram and change axis numbers font size

I only want
the x-axis to display 1 2 3 4 5 6
the y-axis to display 0 20 40 60 80 100
change the numbers font size to 14
I've tried by setting different axis property (ref. to the commented lines of code in the script below) however none of them affect the graph.
%Code to generate the diceSum
DiceSum = myDiceRoller(1,500);
figure(1)
%Create the histogram
hist(DiceSum,NDice*6)
%Label the axes
xlabel('Value of Roll','FontSize',16)
ylabel('Number of Times Rolled','FontSize',16)
%set(gca,'X','FontSize',14)
%set(gca,'YTickLabel',{'0' ;'100'})
%set('xtick','FontSize',14)
%set('Xlim',[0,6], 'Ylim',[0 ,100])
%set('xtick',[0:1:6],'ytick',[0:20:100])
%set(gca,'XLim',[0 6])
%set(gca,'XTick',[0 1 2 3 4 5 6])
%set(gca,'XTickLabel',str2mat{'0','1','2','3','4','5','6')
%xlim([0 6])
This is a separate function I'm using to create the data and the histogram
function [DiceSum] = myDiceRoller(NDice,NRolls)
DiceSum = zeros(1,NRolls);%
for i = 1:NRolls;% on roll 1...roll 2
for j = 1:NDice;% on roll 1 , roll #s of die
n = ceil(rand(1)*6);
DiceSum(1,i) = DiceSum(1,i) + n;
end
hist(DiceSum,NDice*6)
xlabel('Value of Roll')
ylabel('Number of Times Rolled')
end
To have the x-axis displaying 1 2 3 4 5 6 you have to two possibilities:
to change the way you call the hist function as follows:
% hist(DiceSum,1:NDice*6)
hist(DiceSum,1:6)
this because in the call to hist with 2 parameters, the second one should be a vector, in that case, hist returns the distribution of Y among length(x) bins with centers specified by x (being x the second parameter) - R2012b hist help
to directly set the x-axis xtick as follows:
set(gca,'xtick',[0:6])
To have the y-axis displaying 0 20 40 60 80 100 you have to set the y-axis ytick as follows:
set(gca,'ytick',[0:20:100])
To change the x and y axis tick font size to 14 you have to set the axis fontsize as follows:
set(gca,'FontSize',14)
Hope this helps.

MATLAB: how to customize non linear X axis (for example ticks at 1,2,3,4,5,20,100,'string')

I'm using the MATLAB plot feature to compare two vectors. I would like my X axis to represent 1 through 7, and then 14, 21, and then a category at the end for points with undetermined X values..(I'm also not sure how to represent these numberless point (they have Y values, just no X values) I could assign a large number outside any of my X values (1000) to these points, do the 1-7,14,21,1000 and then change the 1000 label to my 'string for un-numbered points'. ??
Here is a way to do it based on the example by The Mathworks here.
The trick is to create 2 x axis with different ranges and make one of them transparent. You can then play around with its properties. I modified a bit their code but kept most of their comments because they explain well the steps.
For the demo I used scatter to represent the points and colored the "good" points in red and the other point (here only 1) in green. You can customize all this of course. For instance I used a value of 100 and not 1000 for the x value of the 2nd axes, but I'll let you figure out how to modify it all to change the output as you wish.
clear
clc
close all
%// Create axes 1 and get its position
hAxes1 = axes;
axes_position = get(hAxes1, 'Position');
%// Create axes 2 and place it at the same position than axes 1
hAxes2 = axes('Position', axes_position);
%// Your data go here.
x = [1 7 14 21 100];
y = rand(1, length(x));
%// Plot the two sections of data on different axes objects
hPlot1 = scatter(hAxes1, x(1:4), y(1:4),40,'r','filled');
hold on
hPlot2 = scatter(hAxes2, x(end), y(end),40,'g','filled');
%// Link the y axis limits and fontsize property of the axes objects
linkaxes([hAxes1 hAxes2], 'y');
linkprop([hAxes1 hAxes2], 'FontSize');
%// Set the x range limits and tick mark positions of the first axes object
set(hAxes1, 'XLim', [1 25], ...
'XTick', [1 7 14 21])
%// Set the x range limits and tick mark positions for the second axes object.
%// Also set the background color to 'none', which makes the background
%// transparent.Add the label for the "super points".
set(hAxes2, 'Color', 'none', ...
'YTickLabel', [], ...
'XLim', [95 100], ...
'XTick', 100,'XTickLabel',{'Super Points'})
And the output:
EDIT
If you wish to add a fit line, I suggest adding a 3rd axes at the same position than the other 2, make it transparent and remove its X- and Y-ticks.
i.e. Add something like this:
hAxes3 = axes('Position', axes_position,'Color','none','YTick',[],'XTick',[]);
And in the call to polyfit/polyval, in my example you want to get only the first 4 elements (i.e. the red ones).
Hence:
p = polyfit(x(1:4),y(1:4),1);
y_poly = polyval(p,x(1:4));
And then plot the line on hAxes3:
hPlot3 = plot(x(1:4),y_poly)
Output: