Plotting bar graph from website

Greetings,
I need help with plotting the bar graph from: NAO in Matlab.
I have downloaded the data for the graph from: Data with:
NAO = table2array(readtable(websave('AO.csv','https://www.cpc.ncep.noaa.gov/products/precip/CWlink/daily_ao_index/monthly.ao.index.b50.current.ascii.table'),'ReadVariableNames',false));
I have tried plotting it with:
for i = 1:height(NAO)
for j = 2:13
bar(datetime(NAO(i),j-1,1),NAO(i,j),0.1,'b')
hold on
end
end
The plot then end up looking like 'graph.png'.
The first thing I would like to change is to remove the day from the datetime, but I don't know how to do that, especially in a for loop.
The second thing is the xaxis, why does only the first date show and not more dates?
Third thing is the color, why is it all black when I have set all bars to be blue? I want of course all the negative values to be red and all positive values blue but that's a problem for later.
So if anyone has any tips on how to proceed to plot the bar graph that would be great.
Thanks!

3 Kommentare

Les Beckham
Les Beckham am 7 Nov. 2023
Bearbeitet: Les Beckham am 7 Nov. 2023
I think you have an issue with the indexing, for one thing. Can you explain what the data is and what the columns and rows represent, and what you are trying to plot?
As far as the color is concerned, the bars are so narrow that you only see the EdgeColor (which is black by default), not the FaceColor. So, once you straighten out the indexing, change the bar command to something like
bar(..., 'FaceColor', 'b', 'EdgeColor', 'b')
Then the skinny bars will be blue.
Walter Roberson
Walter Roberson am 7 Nov. 2023
In my test, the edge color did not default to black ([0 0 0]) but rather to [33 33 33]/256 which is about [0.1294 0.1294 0.1294]
Les Beckham
Les Beckham am 7 Nov. 2023
Interesting. I didn't check, I just assumed it was black because it looks black. [0.1294 0.1294 0.1294] is a pretty dark grey.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Walter Roberson
Walter Roberson am 7 Nov. 2023

0 Stimmen

First:
ax = gca;
ax.XRuler.TickLabelFormat = 'mmm uuuu';
would change the format to (for example) "Jan 1960"
Third:
You have lots and lots and lots of bars. They are so thin (to fit them all) that all you can see of them is the outline. The default outline color appears to be [33 33 33]/255
Peter Perkins
Peter Perkins am 10 Nov. 2023

0 Stimmen

Marcus, it looks to me like what you have is a year-by-month array.
As others have said, those bars are mightily thin. Have you considered plotting lines?
T = readtable(websave('AO.csv','https://www.cpc.ncep.noaa.gov/products/precip/CWlink/daily_ao_index/monthly.ao.index.b50.current.ascii.table'),'ReadVariableNames',false);
T.Properties.VariableNames=["Date","Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"];
T2 = stack(T,2:13,"ConstantVariables",1,NewDataVariableName="X",IndexVariableName="Month");
T2.Date = datetime(T2.Date,repmat((1:12)',74,1),1);
T2.Month = [];
TT = table2timetable(T2);
plot(TT,"Date","X")
That is not exactly what you wanted, but not far. A stemplot seems useful:
stem(TT,"Date","X",Marker=".")
But if bar it must be:
bar(TT.Date,TT.X)

Kategorien

Mehr zu Networks finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2023a

Gefragt:

am 7 Nov. 2023

Beantwortet:

am 10 Nov. 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by