How remove overlapping spaces between positive and negative above and below the x-axis respectively using an area plot in MATLAB?

4 ビュー (過去 30 日間)
S_G
S_G 2020 年 10 月 26 日
回答済み: Mathieu NOE 2020 年 10 月 27 日
I am trying to prep a figure for a publication of monthly values of the North Atlantic oscillation for Januaty between 1950-2018. I plot two separate series relfecting positive and negative phases of NAO over this time period. When one year is positive I assign a zero for the corresponding negative value for that year. When I plot it out, I get a bunch of overlap between the positive/negative values when zeroes are used. I want crisp transitions and no over lap. Code below.
set(0,'defaultfigurecolor','w');
colormap jet
%load PosNegNOAarea.xlsx as column vectors before running code
figure(3)
posarea = area(Year2,PosNAO2);
hold on
negarea = area(Year3,NegNAO3);
hold on
zeroline2 = refline(0,0);
hold off
set(posarea,'FaceColor','r','EdgeColor','r');
set(negarea,'FaceColor','b','EdgeColor','b');
set(zeroline2,'Color','k','LineWidth',0.35);
ylim([-2.8 2.8]);
set(gca,'FontSize',11);
set(gca,'ytick',[-2.5 -2.0 -1.5 -1.0 -0.5 0 0.5 1.0 1.5 2.0 2.5]);
ytix = get(gca,'ytick');
ytixlbl = regexp(sprintf('%.1f\n',ytix), '\n', 'split');
set(gca,'yticklabel',ytixlbl(1:end-1));
ylabel('January NAO','FontSize',14,'FontWeight','bold');
xlim([1948.8 2019.2]);
set(gca,'xtick',[1950 1952 1954 1956 1958 1960 1962 1964 1966 1968 1970 1972 1974 1976 1978 1980 1982 1984 1986 1988 1990 1992 1994 1996 1998 2000 2002 2004 2006 2008 2010 2012 2014 2016 2018 2020]);
xlabel('Year','FontSize',14,'FontWeight','bold');
set(gca,'FontSize',11);
xtickangle(90);
title('NAO Index for January between 1950 and 2018');
plotleg = legend('Postive NAO','Negative NAO','Location','bestoutside','Orientation','horizontal');
legend('boxoff');
set(plotleg,'FontSize',12).
Input file, outputted figure, and desired figure format is attach for folks to review.
Thank for any and all helo.

回答 (1 件)

Mathieu NOE
Mathieu NOE 2020 年 10 月 27 日
hi
so this is my submission - it took me a while to finalize it, I am not really the top notch expert in figure rendering.
Anyway , this is my code - if it can help
I admit it not 100% perfect , because (IMHO) there is not solution that fulfill your wishes without distorting a bit your data
see fig 1 picture : my final result is based on the black line , which is simply the sum of bot positive and negative values, so this forces a better transition bet pos and neg values.
I first resample the raw data , then I sum them. this reduces the overlap segement lenghth (the higher the upsample factor , the lesser the overlap). I let you go through the code to see the rest of the modifications.
I also used another method to fill the areas.
Hope it helps !
set(0,'defaultfigurecolor','w');
colormap jet
%load PosNegNOAarea.xlsx as column vectors before running code
[outdata,head] = readclm('PosNegNAOarea2.txt',4); % my code for txt data loading : %Year2 PosNAO2 Year3 NegNAO3
Year2 = outdata(:,1);
PosNAO2 = outdata(:,2);
Year3 = outdata(:,3);
NegNAO3 = outdata(:,4);
% NB : Year2 = Year3
% % this figure for debug / explanation only
% figure(1),plot(Year2,PosNAO2,'b',Year3,NegNAO3,'r',Year3,PosNAO2+NegNAO3,'k');grid
% legend('Positive NAO','Negative NAO','sum');
% resample data to a refined time axis to reduce overlapping section
% yearly base => monthly base
upsample_factor = 12;
old_x_axis = Year2;
new_x_axis = linspace(min(Year2),max(Year2),length(Year2)*upsample_factor);
% upsample data to reduce areas of overlap (the higher the
% over sampling the better the result
PosNAO22 = interp1(old_x_axis,PosNAO2,new_x_axis);
NegNAO33 = interp1(old_x_axis,NegNAO3,new_x_axis);
new_data = PosNAO22+NegNAO33;
% threshold to creat a small blank area
threshold = 0;% well, not really useful; anyway the dark line at y = 0 will be overlayed by the blank line (see "zeroline2" code below)
PosNAO22 = threshold*ones(size(new_data));
NegNAO33 = -threshold*ones(size(new_data));
ind = find(new_data>threshold);
PosNAO22(ind) = new_data(ind);
ind = find(new_data<-threshold);
NegNAO33(ind) = new_data(ind);
% Pos curve preparation for fill plot
y1=threshold*ones(size(new_x_axis)); %#create first curve (bottom line)
y2=PosNAO22; %#create second curve (data curve)
X_pos=[new_x_axis,fliplr(new_x_axis)]; %#create continuous x value array for plotting
Y_pos=[y1,fliplr(y2)]; %#create y values for out and then back
% Neg curve preparation for fill plot
y1= -threshold*ones(size(new_x_axis)); %#create first curve (bottom line)
y2=NegNAO33; %#create second curve (data curve)
Y_neg=[y1,fliplr(y2)]; %#create y values for out and then back
figure(2),fill(X_pos,Y_pos,'b',X_pos,Y_neg,'r'); %#plot filled area
zeroline2 = line(new_x_axis,zeros(size(new_x_axis)));
set(zeroline2,'Color','w','LineWidth',1);
ylim([-2.8 2.8]);
set(gca,'FontSize',11);
set(gca,'ytick',[-2.5:0.5:2.5]);
ytix = get(gca,'ytick');
ytixlbl = regexp(sprintf('%.1f\n',ytix), '\n', 'split');
set(gca,'yticklabel',ytixlbl(1:end-1));
ylabel('January NAO','FontSize',14,'FontWeight','bold');
xlim([1948.8 2019.2]);
set(gca,'xtick',[1950 1952 1954 1956 1958 1960 1962 1964 1966 1968 1970 1972 1974 1976 1978 1980 1982 1984 1986 1988 1990 1992 1994 1996 1998 2000 2002 2004 2006 2008 2010 2012 2014 2016 2018 2020]);
xlabel('Year','FontSize',14,'FontWeight','bold');
set(gca,'FontSize',11);
xtickangle(90);
title('NAO Index for January between 1950 and 2018');
plotleg = legend('Positive NAO','Negative NAO','Location','bestoutside','Orientation','horizontal');
legend('boxoff');
set(plotleg,'FontSize',12);

カテゴリ

Help Center および File ExchangeColormaps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by