Hatched bars and standard deviation errors in bar charts with Matlab - matlab

I'm trying to create a bar plot. On the x axis I have 4 different conditions: HFd, HFi, LFd, LFi. On the y axis Reaction time is displayed.
I need the bar representing HFd to be hatched and blue, HFi to be blue, LFd to be hatched and red, LFi to be red. In addition, I need the standard deviation to be displayed in black on each bar.
Here are my attempts. Although they work, they don't give me the bar plot I need. Any help would be very much appreciated.
%% This code creates the plot, but without the standard deviation errors
and the different colours.
data1 = [228, 222, 229, 218];
sdev1 = [29, 30, 29, 31];
hold on;
figure;
bar(data1);
%errorbar(data1, sdev1); %it gives me a line that links the different standard deviation errors.
set(gca, 'XTickLabel',{'HFd', 'HFi', 'LFd', 'LFi'});
ylabel('Reaction time (ms)');
ylim([180 300]);
%% This code creates the plot, with different colours but without the hatched
patterns and the standard deviation errors.
dataHFdeg = [228, 0, 0, 0];
sdev11 = [29, 0, 0, 0]
dataHFid = [0, 222, 0, 0];
sdev12 = [0, 30, 0, 0]
dataLFdeg = [0, 0, 229, 0];
sdev13 = [0, 0, 29, 0]
dataLFid = [0, 0, 0, 218];
sdev14 = [0, 0, 0, 31]
bar(dataHFd,'b');
set(gca, 'XTickLabel',{'HFd', 'HFi', 'LFd', 'LFi'});
ylabel('Reaction time (ms)');
ylim([180 300]);
hold on;
bar(dataHFi, 'b');
hold on;
bar(dataLFd, 'r');
hold on;
bar(dataLFi, 'r');
UPDATE:
%% This code creates the plot, with different colours and with error bars, but without the hatched patterns
y = [228; 222; 229;218]; %The data.
s = [29;30;29;31]; %The standard deviation.
fHand = figure;
aHand = axes('parent', fHand);
hold(aHand, 'on')
bar(1, y(1), 'parent', aHand, 'facecolor', 'b');
bar(2, y(2), 'parent', aHand, 'facecolor', 'b');
bar(3, y(3), 'parent', aHand, 'facecolor', 'r');
bar(4, y(4), 'parent', aHand, 'facecolor', 'r');
colors = copper(numel(y) - 4);
for i = 5:numel(y)
bar(i, y(i), 'parent', aHand, 'facecolor', colors(i-4,:));
end
set(gca, 'XTick', 1:numel(y), 'XTickLabel', {'HFd','HFi','LFd','LFi'})
ylim([180 300]);
errorbar(y,s,'.');
%% To create for example the hatched pattern for the second bar - but it doesn't work
applyhatch_pluscolor(bar(2,y(2)), '/', 1);

Related

grouped bar chart with 2 y-axes is displayed as stacked bar

I would like to plot the grouped bar chart for the comparison of three methods. I tried the following code and it is displayed as stacked bar chart.can you please help
dice =[0, 3, 5];
no_of_region=[42, 12, 5];
figure;
bar(dice',.2,'grouped','FaceColor',[0 .5 .5],'EdgeColor',[0 .9 .9],'LineWidth',1.5)
ylabel('Dice Similarity index')
yyaxis right
bar(no_of_region, .2,'grouped','EdgeColor', 'r', 'LineWidth', 2);
legend('Dice Similarity Index','Number of regions')
legend('Location','northwest')
XTickLabel={'a' ; 'b';'c'};
XTick=[1 2 3]
set(gca, 'XTick',XTick);
set(gca, 'XTickLabel', XTickLabel);
set(gca, 'XTickLabelRotation', 45);
xlabel('Different Methods', 'Fontsize', 16)
ylabel('Number of Regions', 'Fontsize', 16)
title('Comparison of Algorithms', 'fontsize', 20);
set(gcf,'color','w');
OUTPUT I GOT AS:
One easy solution is to shift the bars left and right by half of their width (or more if you would like space between them). Here is the new code, which is also cleaned a bit stylistically:
% parameters
dice = [0, 3, 5];
no_of_region = [42, 12, 5];
X = [1, 2, 3]; % common x values for plot
bar_width = 0.2; % width of bars
% initialize figure
figure;
% left plot
bar(X.' - bar_width/2, dice', bar_width, 'grouped', ...
'FaceColor', [0 .5 .5], ...
'EdgeColor', [0 .9 .9], ...
'LineWidth', 1.5);
ylabel('Dice Similarity index');
% right plot
yyaxis right;
bar(X.' + bar_width/2, no_of_region, bar_width, 'grouped', ...
'EdgeColor', 'r', ...
'LineWidth', 2);
% plot formatting
legend({'Dice Similarity Index', 'Number of regions'}, ...
'Location', 'northwest');
XTickLabel = {'a' ; 'b'; 'c'};
XTick = X;
set(gca, ...
'XTick', XTick, ...
'XTickLabel', XTickLabel, ...
'XTickLabelRotation', 45);
xlabel('Different Methods', 'FontSize', 16);
ylabel('Number of Regions', 'FontSize', 16);
title('Comparison of Algorithms', 'FontSize', 20);
set(gcf, 'color', 'w');
Depending on desired behavior, you also might consider replacing figure; by clf;, which clears the existing figure or opens a new figure window if there is not already one open.

imagesc with multiple axis and ticklines

I have a [12 x 6] matrix A that I represent with imagesc and on which I impose some thick tick-lines:
figure
imagesc(A)
set(gca,'xtick', linspace(0.5,size(A,2)+0.5,C+1),...
'ytick', linspace(0.5,size(A,1)+0.5,B*al+1),'linewidth',3);
set(gca,'xgrid', 'on', 'ygrid', 'on', 'gridlinestyle', '-', 'xcolor',
'k', 'ycolor', 'k');
set(gca, 'XTickLabelMode', 'manual', 'XTickLabel', [],'YTickLabel', []);
colorbar
I then want to impose some thinner tick-lines to split in two each box delimited by the thicker lines:
ax1 = gca;
ax1_pos = get(ax1,'Position'); % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
set(gca,'xtick', linspace(0.5,size(A,2)+0.5,2*C+1),'linewidth',1);
The result is clearly not satisfying. How could I fix it? I thought that 'Color','none' would have done the trick...
Instead of trying to modify tick lengths and adding a second axes, I would just plot some extra lines on top of your image. This should achieve what you want:
% Some sample data:
A = rand(12, 6);
% Plot image:
imagesc(A);
set(gca, 'Visible', 'off');
hold on;
colorbar;
% Plot horizontal lines:
yLines = repmat((0:size(A, 1))+0.5, 2, 1);
xLines = repmat([0.5; size(A, 2)+0.5], 1, size(yLines, 2));
line(xLines, yLines, 'LineWidth', 3, 'Color', 'k');
% Plot thick vertical lines:
xLines = repmat((0:2:size(A, 2))+0.5, 2, 1);
yLines = repmat([0.5; size(A, 1)+0.5], 1, size(xLines, 2));
line(xLines, yLines, 'LineWidth', 3, 'Color', 'k');
% Plot thin vertical lines:
xLines = repmat((1:2:size(A, 2))+0.5, 2, 1);
yLines = repmat([0.5; size(A, 1)+0.5], 1, size(xLines, 2));
line(xLines, yLines, 'LineWidth', 1, 'Color', 'k');
And here's the plot:

Plot second y axis using plot and fill (without plotyy)

Here is my code
clear all;clc
x = linspace(0, 10, 100);
axes('Position', [.075,.075,.9,.2], ...
'XColor', 'k', ...
'YColor', [0 0 0]);
fill(x, circshift(sin(x), 50), 'red', 'EdgeColor','None');
ylabel('Fancy Sine', 'FontSize', 11);
% Adding this the upper vanishes and the y axes is on left not right side
axes('Position', [.075,.075,.9,.2], ...
'XColor', 'k', ...
'YAxisLocation', 'Right', ...
'Color', 'none');
plot(x, x, 'Color', [.2 .4 .8]);
ylabel('Line Graph', 'FontSize', 11);
xlabel('X', 'FontSize', 11);
The single axis works fine
But when I want to add the second axis, the first disappears and the new axis is not on the right, but left side..
But both plots should be in one axis and the second y axis should be on the right side.
How to achieve this?
plot automatically sets the axis properties to the default. Use hold to stop this or specify the axis properties after your plot call.
An example of the former:
clear all;clc
x = linspace(0, 10, 100);
ax1 = axes('Position', [.075,.075,.9,.2], ...
'XColor', 'k', ...
'YColor', [0 0 0]);
p1 = fill(x, circshift(sin(x), 50), 'red', 'EdgeColor','None','Parent',ax1);
ylabel('Fancy Sine', 'FontSize', 11);
% Adding this the upper vanishes and the y axes is on left not right side
ax2 = axes('Position', [.075,.075,.9,.2], ...
'XColor', 'k', ...
'YAxisLocation', 'Right', ...
'Color', 'none');
hold on
p2 = plot(ax2,x, x, 'Color', [.2 .4 .8]);
hold off
ylabel('Line Graph', 'FontSize', 11);
xlabel('X', 'FontSize', 11);
Edit1: The reason you don't need to do this for your fill call is because it creates a patch object in your axes and does not invoke a plot command.

How to display two overlapping lines in matlab

I have to plot 5 lines that overlap in some regions, and I need to be able to see all the lines.
I can think of shifting the lines a bit to allow them to be displayed, but this doesn't seem a very elegante solution. Even so, how could I code such a thing?
Is there any other way to plot multiple overlapping lines while being able to distinguish them at every point?
For exemple, here is one exemple with 3 overlapping lines:
Thank you in advance!
Another way is to use transparency.
Unfortunatelly, line objects do not obey
transparency commands :(
A workaround is to:
1. download patchline (<-- link to Matlab Central)
2. use it to plot patchline with transparency
Once you have patchline, you can try something like:
% create some lines:
l1 = [1, 1, 1, 0, 0, 0.25, 1, 1, 0, 0, 0, 0, 1 1];
l2 = [0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1];
l3 = [1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0];
% plot with patchline (notice the use of 'EdgeAlpha'):
figure;
patchline(1:length(l1), l1, [], 'EdgeColor', [0.8, 0.2, 0.35],...
'LineWidth', 5, 'EdgeAlpha', 0.5 );
hold on;
patchline(1:length(l1), l2, 'EdgeColor', [0.2, 0.7, 0.55],...
'LineWidth', 5, 'EdgeAlpha', 0.5 );
patchline(1:length(l1), l3, 'EdgeColor', [0.1, 0.2, 0.95],...
'LineWidth', 5, 'EdgeAlpha', 0.5);
% change y limits to see line overlap clearly
set(gca, 'YLim', [-0.5, 1.5]);
Not an ideal way to do it - the rough 'cracks' will stay this way,
but you can experiment with different line widths or moving the
lines in y axis by a value that would correspond to an image with each
line covering only half of it closest neighbour...
You may play with EraseMode property of the plot line. The following code example shows how to shift the lines and EraseMode effect:
% First we generate some data
NLines = 2;
NPoints = 50;
LineWidth = 3;
ShiftStep = 1.1;
XData = linspace(0,1,NPoints);
YData = rand(NPoints,NLines);
for k=1:NLines
YData(:,k) = YData(:,k) > (k/(NLines+1));
end
% Then we create plot
figure('color','w');
subplot(3,1,1); plot(XData,YData, 'LineWidth',LineWidth);
ylim([-0.1 1.1]);
title('simple')
subplot(3,1,2); plot(XData,YData+repmat((0:NLines-1)*ShiftStep,NPoints,1), 'LineWidth',LineWidth, 'EraseMode','xor');
ylim([-0.1 1.1+ShiftStep*(NLines-1)]);
title('Shifted vertically')
subplot(3,1,3); plot(XData,YData, 'LineWidth',LineWidth, 'EraseMode','xor');
ylim([-0.1 1.1]);
title('EraseMode = xor')
In my opinion if you have more than three lines similar to your plot, shifting is visually more attractive. Also you may create several axes (Like I did) and plot each line in its own axes, so they will have y-labels set accordingly, but their X-labels will be essentially the same.
You can use plot3 and assign different Z values to different overlapping lines. However, it'll look more like you expect (Z being the "up" direction) if you swap the Y and Z axes:
Example:
Y1 = randn(1,100);
Y2 = randn(1,100);
X = 1:100;
Z1 = 1*ones(size(X));
Z2 = 2*ones(size(X));
plot3(X,Z1,Y1);
hold on;
plot3(X,Z2,Y2);

Plotting a line above/below existing lines created with plotyy

I'm running this code:
t = linspace(0, 10, 1000);
y1 = 2*t;
y2 = 3*t;
figure;
[ax, h1, h2] = plotyy(t, y1, t, y2);
set(h1, 'LineWidth', 4);
set(h2, 'LineWidth', 4);
hold on;
h3 = plot([5, 5], [0, 3000], 'LineWidth', 6, 'Color', [0.6, 0.6, 0.6]);
Which creates this plot:
Notice how the vertical grey line appears on top of the blue line (y1) but below the green line (y2).
How do I plot the grey line either on top of the other two lines, or below the other two lines?
I see two options:
A. Bring the gray line forward by moving it to the second axes created by the plotyy command
set(h3,'parent',ax(2));
B. Place the gray line on the bottom by rearranging the order of the blue and grey lines on
the axes.
chld = [h1 h3];
set(ax(1),'children',chld); %# reorders the two lines so that the gray line is in back.
To make the gray line bottom, you can also change the order of drawing.
t = linspace(0, 10, 1000);
y1 = 2*t;
y2 = 3*t;
figure;
h3 = plot([5, 5], [0, max(y1)], 'LineWidth', 6, 'Color', [0.6, 0.6, 0.6]);
hold on;
[ax, h1, h2] = plotyy(t, y1, t, y2);
set(h1, 'LineWidth', 4);
set(h2, 'LineWidth', 4);
There's a trick in h3 = plot(...) to make sure the left scale is correct, though.