How to draw a graph with a colormap from a txt file?
3 次查看(过去 30 天)
显示 更早的评论
I have a txt file consisting of 10 columns. The first 3 columns are year/month/day and the next 7 columns are my moisture data in 7 different depths. (this data set is for 3 years) 2014 1 1 10.2 13.4 14 15.1 17 . . . 2017 12 30 12.4 14 15.6 17 19.4
I wrote a code to plot a graph using datenume and plot function and it look likes image1. It is correct but now I am asked to make the same graph in the format of image2. I dont know what function draws a graph like image2 from my data set. Can anyone help me? Thanks
0 个评论
采纳的回答
jonas
2018-10-9
编辑:jonas
2018-10-10
You can do that with e.g. surf or contourf . I would suggest surf because it works with datetime format, which you should use instead of datenum.
Here's a code adapted to your data
% Load data
data=xlsread('Soil misture.xlsx')
% Save depths, d, and remove this row
d=data(1,~isnan(data(1,:)));
data(1,:)=[];
% Save times, t, as datetime format
t=datetime(data(:,1),data(:,2),data(:,3));
% Save moisture content, u, as 2d matrix
u = data(:,4:end);
% Interpolate over time
ti = t(1):days(1):t(end);
ui = interp1(t,u,ti);
% Interpolate over depth
di = min(d):1:max(d);
uii = interp1(d,ui',di);
% plot
surf(ti,di,uii,'edgecolor','interp');
% some aestethics
ax=gca;
axis tight
view([0 90]);
cb = colorbar(gca,'location','southoutside');
cb.Label.String = 'Soil water content (cm^3/cm^3)';
ylabel('Soil depth (cm)');
xlabel('Time','fontweight','bold');
xtickformat('yyyy/MM/dd');
ax.XRuler.TickLabelRotation = 40;
ax.XRuler.TickDirection = 'out';
ax.YRuler.TickDirection = 'out';
ax.Layer = 'top';
ax.Color = 'none';
grid off
colormap(jet(20))
17 个评论
jonas
2018-10-10
Attached is an updated script. The function I linked before was quite annoying, so I used another one. Here is the link . Download it, unzip and put the .m-file in the in the MATLAB search path, for example the same folder as your script, and then execute the attached script. I'm fairly sure it will work without additional issues.
You should take a look at the Getting started with MATLAB links. This forum is great, but you need to be able to do some basic coding yourself.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Colormaps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!