Clear Filters
Clear Filters

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)
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
Kernisha
Kernisha on 25 Nov 2022
Edited: Jan on 26 Nov 2022
%Obtain data
date = EcoTotemBroadwayBicycleCountA2(:,1);
dateNum = datevec(date);
date_2017 = (dateNum(:,1)==2017);
year_2017 = date_2017
year_2017 = year_2017>0;
runT = EcoTotemBroadwayBicycleCountA2(:,5);
runTnum = cell2mat(runT);
runVec_year_2017=runTnum(year_2017);

Sign in to comment.

Answers (2)

Seth Furman
Seth Furman on 15 Oct 2024 at 13:47
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")
G = 12x3 table
month_Date GroupCount sum_BicycleCount __________ __________ ________________ Jan-2017 31 1525 Feb-2017 28 1609 Mar-2017 31 1550 Apr-2017 30 1300 May-2017 31 1369 Jun-2017 30 1454 Jul-2017 31 1942 Aug-2017 31 1678 Sep-2017 30 1605 Oct-2017 31 1691 Nov-2017 30 1663 Dec-2017 31 1564
bar(G.month_Date, G.sum_BicycleCount)
title("Total cyclist per month in 2017");
xlabel("Month");
ylabel("Total Cyclist");
grid on;

Arjun
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!

Products


Release

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!