scatterplot with the datetime

Hello everyone,
I am learning Matlab from very basic. I am quite clear what to do but, I am not sure how to procced. I have a dataset with 8 columns. The first one is datetime and other are power consumption. I want to scatter plot the given data according to hours throughout the year with the second column. As the datetime column has hourly based data I am not sure how to extract that data and scatterplot it.
Your help would be highly appericiated.

 Risposta accettata

One approach —
Uz = unzip('https://www.mathworks.com/matlabcentral/answers/uploaded_files/630240/household_power_consumption_2007.zip')
Uz = 1×1 cell array
{'household_power_consumption_2007.csv'}
T1 = readtable(Uz{1})
T1 = 521669×8 table
DateTime Global_active_power Global_reactive_power Voltage Global_intensity Sub_metering_1 Sub_metering_2 Sub_metering_3 ____________________ ___________________ _____________________ _______ ________________ ______________ ______________ ______________ 01-Jan-2007 00:00:00 2.58 0.136 241.97 10.6 0 0 0 01-Jan-2007 00:01:00 2.552 0.1 241.75 10.4 0 0 0 01-Jan-2007 00:02:00 2.55 0.1 241.64 10.4 0 0 0 01-Jan-2007 00:03:00 2.55 0.1 241.71 10.4 0 0 0 01-Jan-2007 00:04:00 2.554 0.1 241.98 10.4 0 0 0 01-Jan-2007 00:05:00 2.55 0.1 241.83 10.4 0 0 0 01-Jan-2007 00:06:00 2.534 0.096 241.07 10.4 0 0 0 01-Jan-2007 00:07:00 2.484 0 241.29 10.2 0 0 0 01-Jan-2007 00:08:00 2.468 0 241.23 10.2 0 0 0 01-Jan-2007 00:09:00 2.486 0 242.18 10.2 0 0 0 01-Jan-2007 00:10:00 2.492 0 242.46 10.2 0 0 0 01-Jan-2007 00:11:00 2.5 0 242.88 10.2 0 0 0 01-Jan-2007 00:12:00 2.494 0 242.57 10.2 0 0 0 01-Jan-2007 00:13:00 2.492 0 242.41 10.2 0 0 0 01-Jan-2007 00:14:00 2.48 0 241.81 10.2 0 0 0 01-Jan-2007 00:15:00 2.478 0 241.73 10.2 0 0 0
figure
scatter(T1.DateTime, T1.Global_active_power, '.')
grid
MeanHourlyConsumption = groupsummary(T1, 'DateTime', 'hourofday', 'mean', 'Global_active_power')
MeanHourlyConsumption = 24×3 table
hourofday_DateTime GroupCount mean_Global_active_power __________________ __________ ________________________ 0 21741 0.74066 1 21720 0.5522 2 21719 0.46917 3 21720 0.42062 4 21720 0.41507 5 21720 0.42935 6 21720 0.80487 7 21720 1.4331 8 21699 1.502 9 21719 1.3085 10 21720 1.2181 11 21719 1.1809 12 21717 1.1473 13 21720 1.0948 14 21756 1.0827 15 21780 1.0167
figure
scatter(MeanHourlyConsumption{:,1}, MeanHourlyConsumption{:,3}, 's')
grid
.

6 Commenti

Thankyou so much Star strider. I want to ask you another question as well. Can you please help me in finding the total Global active power for every single hour of each day throughout the year.
As always, my pleasure!
Can you please help me in finding the total Global active power for every single hour of each day throughout the year.’
I believe that is in the first plot.
the given data is taken for every single minute but for my problem i am trying it to convert it to hour.
Also, The above first plot gives me the total mean for every single hour.
My apologies — I missed that detail.
The code needs only a small modification —
Uz = unzip('https://www.mathworks.com/matlabcentral/answers/uploaded_files/630240/household_power_consumption_2007.zip');
T1 = readtable(Uz{1})
T1 = 521669×8 table
DateTime Global_active_power Global_reactive_power Voltage Global_intensity Sub_metering_1 Sub_metering_2 Sub_metering_3 ____________________ ___________________ _____________________ _______ ________________ ______________ ______________ ______________ 01-Jan-2007 00:00:00 2.58 0.136 241.97 10.6 0 0 0 01-Jan-2007 00:01:00 2.552 0.1 241.75 10.4 0 0 0 01-Jan-2007 00:02:00 2.55 0.1 241.64 10.4 0 0 0 01-Jan-2007 00:03:00 2.55 0.1 241.71 10.4 0 0 0 01-Jan-2007 00:04:00 2.554 0.1 241.98 10.4 0 0 0 01-Jan-2007 00:05:00 2.55 0.1 241.83 10.4 0 0 0 01-Jan-2007 00:06:00 2.534 0.096 241.07 10.4 0 0 0 01-Jan-2007 00:07:00 2.484 0 241.29 10.2 0 0 0 01-Jan-2007 00:08:00 2.468 0 241.23 10.2 0 0 0 01-Jan-2007 00:09:00 2.486 0 242.18 10.2 0 0 0 01-Jan-2007 00:10:00 2.492 0 242.46 10.2 0 0 0 01-Jan-2007 00:11:00 2.5 0 242.88 10.2 0 0 0 01-Jan-2007 00:12:00 2.494 0 242.57 10.2 0 0 0 01-Jan-2007 00:13:00 2.492 0 242.41 10.2 0 0 0 01-Jan-2007 00:14:00 2.48 0 241.81 10.2 0 0 0 01-Jan-2007 00:15:00 2.478 0 241.73 10.2 0 0 0
% figure
% scatter(T1.DateTime, T1.Global_active_power, '.')
% grid
HourlyConsumption = groupsummary(T1, 'DateTime', 'hour', 'mean', 'Global_active_power')
HourlyConsumption = 8698×3 table
hour_DateTime GroupCount mean_Global_active_power ____________________ __________ ________________________ 01-Jan-2007 00:00:00 60 2.5506 01-Jan-2007 01:00:00 60 2.5234 01-Jan-2007 02:00:00 60 2.5823 01-Jan-2007 03:00:00 60 2.5417 01-Jan-2007 04:00:00 60 2.4757 01-Jan-2007 05:00:00 60 2.4762 01-Jan-2007 06:00:00 60 2.4558 01-Jan-2007 07:00:00 60 2.4472 01-Jan-2007 08:00:00 60 2.4417 01-Jan-2007 09:00:00 60 3.1461 01-Jan-2007 10:00:00 60 2.6617 01-Jan-2007 11:00:00 60 2.576 01-Jan-2007 12:00:00 60 2.6159 01-Jan-2007 13:00:00 60 2.1624 01-Jan-2007 14:00:00 60 1.2944 01-Jan-2007 15:00:00 60 1.9092
figure
scatter(HourlyConsumption{:,1}, HourlyConsumption{:,3}, '.')
grid
xlim([HourlyConsumption{1,1} HourlyConsumption{481,1}]) % Temporarily Limit For Readability
Ax = gca;
Ax.XTick = HourlyConsumption{1:24:end,1};
Ax.XTickLabelRotation = 90;
Ax.XMinorTick = 'on';
% pos = Ax.Position;
% Ax.Position = pos+[-500 0 500 0];
Changing the code was straightforward, however making the plot readable was not. (Plotting tick values only for the days works to an extent.) It turns out that ‘hour_DateTime’ is a categorical vector, and it is not obvious that changing its format is an option. It would have to be re-created as a categorical array with a different format, and then substituted for the existing vector to change it. (Changing the datetime Format in ‘T1’ does not change the format in the ‘HourlyConsumption’ table.) Experiment with the 'Position' property to change the width of the plot to improve the readability. (It works in the online Run feature, however it does not do everything I would like it to do here, so I commented it out.)
.
Thankyou so much. Really appreciate your help.
As always, my pleasure!

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by