How to create a contour plot with frequencies on the y axis and time on the x axis but with the time in hh:mm:ss format?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello, I am trying to create a contour plot with frequencies on the y axis and time on the x axis but I want the time in hh:mm:ss format. The graph is generated well with the code below (time is in seconds, simple vector from 0 to 500) but I want the x ticks in hh:mm:ss with a specified start time (and that it adjusts when I zoom in). Thank you in advance. Marc
figure ('OuterPosition',[200 300 1000 600]); contour(time,freq_vector,log(freq_matrix),20); xlabel('time [s]'); ylabel('Frequency [Hz]');
2 Kommentare
Akzeptierte Antwort
jonas
am 26 Sep. 2018
Bearbeitet: jonas
am 27 Sep. 2018
Try this,
t is a double (seconds)
t=t/(60*60*24);
contour(t,freq,z)
datetick('x','hh:mm:ss','keepticks')
This is a bit hacky and will not work if your time-vector exceeds 24h, whereafter the xticks will return to 00:00. In other words, the xaxis shows the time of day, not the time elapsed.
Note that surf works with datetime, if that works for your application. I suppose contourf does not work because the xy-values of the contours are stored in the same matrix where datetime and doubles cannot be mixed.
I've also written an ugly but easy-to-use script for plotting contours with datetime because, quite honestly, it should be possible. Of course, some functionality of normal contour plots (such as clabel) are unavailable.
%%Some data
t=1:100;
y=1:100;
z=peaks(100);
%%Get contours
C=contourc(t,y,z);
cnt=[0];nc=[];idc=[];
while (sum(nc)+cnt)<size(C,2)
cnt=cnt+1;
idc=[idc sum(nc)+cnt];
nc=[nc C(2,idc(end))];
end
%%Save levels and remove the corresponding cols
lvls=C(1,idc);
C(:,idc)=NaN;
So now we have a list of levels and a matrix with all contours saved, each contour enclosed by NaNs. We can easily plot the contours as a single line.
%%Plot
plot(seconds(C(2,:)),C(1,:))
xtickformat('hh:mm:ss')
However, we do not utilize the z-data, so all contours share the same colors. There are some solutions, but the most practical one is probably to split the matrix up in cell arrays. In hindsight, I should have stored them like this from the get go. Here's a piece of code written by Stephen Cobeldick:
%%Split matrix into cell array
C=C(:,2:end)';
idx = all(isnan(C),2);
idy = 1+cumsum(idx);
idz = 1:size(C,1);
C = accumarray(idy(~idx),idz(~idx),[],@(r){C(r,:)});
Now we can plot separate lines and access their individual colors in the handle. Let's define the linecolors by scaling their corresponding z-data to a colormap.
%%Plot
hold on
h=cellfun(@(x)plot(seconds(x(:,1)),x(:,2)),C)
xtickformat('hh:mm:ss')
cb=colorbar(gca)
set(gca,'clim',[min(lvls),max(lvls)])
%%Define colors
cmap=parula(300);
colormap(cmap)
%%Normalize and round for indexing
LevelColors = round(normalize(lvls,'range',[1 size(cmap,1)]))
%%Change color of handles
for j=1:numel(lvls)
h(j).Color = cmap(LevelColors(j),:);
end
The result is a quite nice contour plot with dateformat on the x-axis. The colorbar is fake, in the sense that it is not scaled automatically.

0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Contour Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!