![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/285336/image.png)
How to plot z values against year in x-axis and months in y-axis?
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
shravani banerjee
il 17 Apr 2020
Modificato: dpb
il 17 Apr 2020
I have a CSV file with rainfall values of a place. The rainfall values are places with respect to year (rows) and month(columns). See the attached file. I need to plot a contour plot of rainfall values against year and months with years in x-axis and months in y-axis. KINDLY HELP ME OUT. Thanks in advance!
0 Commenti
Risposta accettata
Ameer Hamza
il 17 Apr 2020
Modificato: Ameer Hamza
il 17 Apr 2020
try this
T = readtable('RAINFALL_1980.csv');
years = T.YEAR;
months = T.Properties.VariableNames(2:end);
values = T{:,2:end}';
y = years;
m = 1:12;
[Y,M] = meshgrid(y,m);
f = figure();
ax = axes();
surf(Y,M,values);
xlim([min(years) max(years)]);
ylim([1 12]);
ax.YTickLabel = months(ax.YTick);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/285336/image.png)
0 Commenti
Più risposte (1)
dpb
il 17 Apr 2020
Modificato: dpb
il 17 Apr 2020
[Y X]=meshgrid([1:12],tRAIN.YEAR);
contour(X,Y,tRAIN{:,2:end})
yticklabels(tRAIN.Properties.VariableNames(2:end))
Just for comparison, the contour plot originally asked for looks like...
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/285386/image.jpeg)
months() must postdate R2019b have here...
ADDENDUM:
NB: One can also use just the two vectors and dispense with the explicit meshgrid call...just have to transpose so X variable matches number of columns in Z.
contour(tRAIN.YEAR,[1:12],tRAIN{:,2:end}.')
produces same plot as above. I'm pretty sure same trick works with surf.
The transpose is same thing as reversing [X Y] in meshgrid (as note both Answers do). A more user-friendly implemenation could take care of that internally at the cost of more input-processing overhead.
0 Commenti
Vedere anche
Categorie
Scopri di più su Surface and Mesh Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!