How can i produce a plot that represents each month of the year showing the total of the different values?
9 views (last 30 days)
Show older comments
I have a dataset with different days,months and years that all produce the total of the different individual values. If i was to represent the total value for each month in that year.
cyclist_M = zeros(12,1);
for i=1:1:12
start_P=i+(i-1)*3; %start pt of the month
end_P=start_P+3;
cyclist_M(i,1)=sum(cyclistVec_year_2017(start_P:end_P,1));
end
%Now to visualize the data
month =(1:11)';
figure(1);bar(month,cyclist_M);title('Total cyclist per month in 2017');xlabel('Month');ylabel('Total Cyclist');grid on;
4 Comments
Answers (2)
Seth Furman
on 15 Oct 2024
Adding to Arjun's answer, you can use groupsummary to accomplish this task even more succinctly.
dates = datetime(2017,1,1) + caldays(0:364);
dailyCounts = randi([0, 100], size(dates));
EcoTotemBroadwayBicycleCountA2 = table(dates', dailyCounts', VariableNames=["Date", "BicycleCount"]);
EcoTotemBroadwayBicycleCountA22017 = EcoTotemBroadwayBicycleCountA2(year(EcoTotemBroadwayBicycleCountA2.Date) == 2017,:);
G = groupsummary(EcoTotemBroadwayBicycleCountA22017,"Date","month","sum")
bar(G.month_Date, G.sum_BicycleCount)
title("Total cyclist per month in 2017");
xlabel("Month");
ylabel("Total Cyclist");
grid on;
0 Comments
Arjun
on 8 Oct 2024
As I understand it, you want to plot the months of the year 2017 against the number of cyclists in those months, given the daily data.
To do so, the code should efficiently filter the data to include only entries from the year 2017. To calculate monthly totals, the program iterates over each month, using a logical mask to identify and sum the counts for that month. This results in an array of monthly totals. Finally, a bar plot is generated to visually represent the total counts for each month, with the x-axis labelled with month names and the y-axis displaying the total counts.
Kindly refer to the code below for better understanding:
% Create sample data and put in a table
dates = datetime(2017,1,1) + caldays(0:364);
dailyCounts = randi([0, 100], size(dates));
EcoTotemBroadwayBicycleCountA2 = table(dates', dailyCounts', 'VariableNames', {'Date', 'BicycleCount'});
% Extract the year from the dates and filter for 2017
dateNum = datevec(EcoTotemBroadwayBicycleCountA2.Date);
isYear2017 = (dateNum(:,1) == 2017);
dates_2017 = EcoTotemBroadwayBicycleCountA2.Date(isYear2017);
counts_2017 = EcoTotemBroadwayBicycleCountA2.BicycleCount(isYear2017);
% Initialize an array to store monthly totals
monthlyTotals = zeros(12, 1);
% Loop through each month and sum the daily counts
for monthIdx = 1:12
% Find the indices for the current month
monthMask = month(dates_2017) == monthIdx;
monthlyTotals(monthIdx) = sum(counts_2017(monthMask));
end
% Plotting the results
figure;
bar(1:12, monthlyTotals);
title('Total Bicycle Counts per Month in 2017');
xlabel('Month');
ylabel('Total Bicycle Count');
xticks(1:12);
xticklabels({'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'});
grid on;
I hope this will help!
0 Comments
See Also
Categories
Find more on Calendar 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!