Change bar colors based on date
Show older comments
Hi. I am plotting a bar chart for each month showing the maximum tidal height for that day. I would like to color each phase of the moon with its own color for each month of the year. For example, full moon on 10. jan = green bar, new moon on 24. jan = red bar. first quarter on 3. jan = brown bar and last quarter on 17. jan = orange bar (see first fig below).
The data is read in from an excel fil from the "edited data and charts" tab (see attached excel file).
The code I have generated plots the tidal heights for each month but not with specified colors for specified bars (see second fig below).


Question 1: How can I specify the color for the bars based on the date of the phase of the moon? The dates can be read in from the excel fil from the "Moon phases" tab.
Question 2: My title goes over two lines instead of one line. How can I force the title to be on one line? i.e. the year next to the month.
%% Setup the Import Options
opts = spreadsheetImportOptions("NumVariables", 2);
% Specify sheet and range
opts.Sheet = "Edited data and charts";
opts.DataRange = "D2:E367";
% Specify column names and types
opts.VariableNames = ["Date", "HighWater"];
opts.VariableTypes = ["datetime", "double"];
% Import the data
Tides2020 = readtable("Tidal Predic 2020\Tides2020_KSU.xlsx", opts, "UseExcel", false);
%% Clear temporary variables
clear opts
%%
Data=Tides2020.HighWater;
Date=Tides2020.Date;
%Days=[31 28 31 30 31 30 31 31 30 31 30 31];
Days=[31 29 31 30 31 30 31 31 30 31 30 31];
Arstall=2020;
Months={'januar' 'februar' 'mars' 'april' 'mai' 'juni' 'juli' 'august' 'september' 'oktober' 'november' 'desember'};
Start=1;
End=Days(1);
Month=Months(1);
figure1=figure
for i=1:12
subplot(6,2,i)
h=bar(Data(Start:End),'stacked');
%h.FaceColor = 'flat';
title([Months(i) num2str(Arstall)]);
%xlabel('Dag');
xticks([1:1:End]);
xlim([0.3 Days(i)+0.7]);
ylim([140 270])
set(gca,'YTickLabel',[]);
yticks([140:20:270]);
Start=End+1;
if End <366 % change to 366 for leapyear
End=End+Days(i+1);
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Errorbars in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!