scatterplot with the datetime
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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.
채택된 답변
Star Strider
2021년 5월 26일
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
Ram Basnet
2021년 5월 27일
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.
Star Strider
2021년 5월 27일
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.
Ram Basnet
2021년 5월 27일
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.)
.
Ram Basnet
2021년 5월 27일
Thankyou so much. Really appreciate your help.
Star Strider
2021년 5월 27일
As always, my pleasure!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Scatter Plots에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
