How to draw a 2D histogram from a time series?
조회 수: 17 (최근 30일)
이전 댓글 표시
Hello,
I have a data from a meteorological station measuring temperature each hour from 1990 to 2020 and I have the data as table of datetime and double. I would like to draw a histogram like this:
Do you have any ideas? I have found the hist3 function, but the documentation does not explain the usage of the function clearly for me.
EDIT: The data are included
댓글 수: 4
darova
2020년 4월 4일
Maybe you want imagesc? Can you upload the data in another format? I have older version, can't read
채택된 답변
추가 답변 (2개)
darova
2020년 4월 4일
My proposition
A = importdata('temp_data.csv',',');
temp = A.data;
time = A.textdata;
time(:,2) = [];
time(1) = [];
% calculate size for matrix
n = (year(time(end))-year(time(2))+1)*12; % number of columns
m = 24*31; % numer of rows
P = nan(m,n); % preallocate matrix
% calculate appropriate position in matrix
ii = hour(time) + (day(time)-1)*24 + 1; % rows
jj = month(time) + (year(time)-year(time(1)))*12; % columns
ind = sub2ind(size(P),ii,jj);
P(ind) = temp; % fill matrix
imagesc(P)
colorbar
xlabel('months')
ylabel('hour + 24*day')
axis xy
Those short blue lines at the top are months with 30 days
Long blue lines - february 28/29 days
댓글 수: 3
Image Analyst
2020년 4월 4일
Your image doesn't really make sense. You can't have both the y axis and the color both indicate the frequency (counts of each temperature). So, try this:
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
fileName = 'temp_data.mat'
s = load(fileName)
tbl = s.temperature;
dateTimes = tbl.Zeitstempel;
temperatures = tbl.AirTemp;
hFig = figure;
subplot(2, 1, 1);
plot(dateTimes, temperatures, 'b.');
grid on;
title('Temperatures by Date', 'FontSize', fontSize);
xlabel('Date', 'FontSize', fontSize);
ylabel('Temperature', 'FontSize', fontSize);
% Get the month for each date/time stamp
theMonths = month(dateTimes);
% Make up a histogram of 100 bins for each month
edges = [-inf, -10, 4, 4, 8, 13, 18, 23, 29, 35, 41, inf];
% Make up the output image.
tempHistImage = zeros(length(edges) - 1, 12); % 100 bins, 12 months. Each month is a column.
for k = 1 : 12
rowsForThisMonth = theMonths == k;
tempsForThisMonth = temperatures(rowsForThisMonth);
counts = histcounts(tempsForThisMonth, edges);
% Load into an image
tempHistImage(:, k) = counts;
end
% Replicate the image to make it wider.
% tempHistImage = imresize(tempHistImage, [100, 12*10], 'nearest');
subplot(2, 1, 2);
imshow(tempHistImage, [], 'InitialMagnification', 800);
colormap(jet(length(edges)));
colorbar;
axis('on', 'image');
title('Temperature Histogram, by Month', 'FontSize', fontSize);
xlabel('Month', 'FontSize', fontSize);
ylabel('Temperature', 'FontSize', fontSize);
hFig.WindowState = 'maximized'; % Maximize the window.
fprintf('Done running %s.m ...\n', mfilename);
You can fancy it up further if you want, like with tick labels, etc.
댓글 수: 2
Image Analyst
2020년 4월 5일
Well let's look at your plot for the first week of August. The orange strip where the temperature is between 23 and 29 degrees. Can you tell me, looking at your y axis, what percentage of days in August have a termperature between 23 and 29 degrees? Is it 45% of the days? 50%? 65% How can you tell from the graph? Now, there really is an answer. If you looked at all the values, you might be, say 58%, but from your graph there's no telling.
참고 항목
카테고리
Help Center 및 File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!