Stack three area plots - MATLAB
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi
I have the attached Excel file which contains the data for 1-base emissions 2-post REEE 3-Abatement.
I want to plot the base emissions on y-axis first versus the years on x-axis.
Then, the post-REEE and the Abatement emisisons should be plotted such that they 'eat' from the 'base' as shown in the figure for the similar data below:
Is there a way I can do that in MATLAB please ?
many thanks

採用された回答
Star Strider
2021 年 4 月 12 日
This approaches what you want, however there must be some sort of conservation metric or other sort of scaling necessary to get the plot to appear as you want it to appear. I must leave that to you, since I do not understand how the various emissions values are related. (I left the ‘TotalEmissions’ vector that I created in the code, in the event you want to use it.)
T1 = readtable('CCC_data_emissions.xlsx');
T2 = T1; % Duplicate & Save Original
TF1 = ismember(T2.years, T2.years_1); % Matching Years
T2.emissions_1(TF1) = T1.emissions_1(~isnan(T1.emissions_1)); % Matching Values
T2.emissions_2(TF1) = T1.emissions_2(~isnan(T1.emissions_2)); % Matching Values
TotalEmissions = T2.emissions+T2.emissions_1+T2.emissions_2;
figure
area(T1.years, [T2.emissions-T2.emissions_1-T2.emissions_2, T2.emissions_1, T2.emissions_2])
legend('Emissions','Emissions_1','Emissions_2', 'Location','NE')
.
12 件のコメント
AHMED FAKHRI
2021 年 4 月 12 日
編集済み: AHMED FAKHRI
2021 年 4 月 12 日
Many thanks for your solution @Star Strider
You are right that I need to tweak the data a bit. I did as attached. However, I still do not get similar to the graph attached in my post. Let me explain to you what are these data:
1- The base emissions data represent the annual carbon emissions till 2050 without any intervention from us. However, that is not what we want, we want to reduce the emissions further beyond 85 million tonne at 2050 (last data point). This represents actually the total emissions expected till 2050.
2- Therefore, we use energy efficincy measures called 'post-REEE' to reduce the base line emisisons which then reduces the base from 85 Mt in 2050 to 59 Mt. However, that is stil not enough.
3- We use technlogies such as Hydrogen to reduce the emissions further form 59 to reach just 4 Mt by 2050 as seen in my attached 'updated' file. Now, we are good since 4Mt is very low compared to 85.
Therefore, I want to plot the base first, then post-REEE reduces the base, then the technologies reduces the post-REEE.
However, the code does not work with my new data yet.
many thanks
Star Strider
2021 年 4 月 12 日
I can’t make it worked with the area function, since I can’t figure out how to calculate the necessary values from the data provided, however I can make it work with patch, so this will have to do —
T1 = readtable('CCC_data_emissions.xlsx');
T2 = T1; % Duplicate & Save Original
TF1 = ismember(T2.years, T2.years_1); % Matching Years
T2.emissions_1(TF1) = T1.emissions_1(~isnan(T1.emissions_1)); % Matching Values
T2.emissions_2(TF1) = T1.emissions_2(~isnan(T1.emissions_2)); % Matching Values
figure
patch([T2.years; flipud(T2.years)], [T2.emissions; flipud(T2.emissions_1)], 'r', 'FaceAlpha',0.5)
hold on
patch([T2.years; flipud(T2.years)], [T2.emissions_1; flipud(T2.emissions_2)],'g', 'FaceAlpha',0.5)
patch([T2.years; flipud(T2.years)], [T2.emissions_2; zeros(size(T2.years))],'b', 'FaceAlpha',0.5)
grid
xlim([min(T2.years) max(T2.years)])
xlabel('Years')
ylabel('Annual Emissions [MtCO_2e]')
legend('Emissions','Emissions_1','Emissions_2', 'Location','NE')
producing —

.
AHMED FAKHRI
2021 年 4 月 12 日
Many thanks @Star Strider
That is good also, I don't yet quite understand from where the flutucations came in the output circled in red
plotting the same data in Excel gives normal lines as below.


Star Strider
2021 年 4 月 12 日
My final effort —
T1 = readtable('CCC_data_emissions.xlsx');
T2 = T1; % Duplicate & Save Original
NCD = ~ismember(T2.years, T2.years_1); % No Common Data Logical Vector
yearsNCD = T2.years(NCD); % No Common Data Years
emissionsNCD = T2.emissions(NCD); % No Common Data Emissions
Lv = ~isnan(T2.years_1);
years_1 = T2.years_1(Lv);
years_2 = T2.years_2(Lv);
emissions_1 = T1.emissions_1(Lv); % Matching Values
emissions_2 = T1.emissions_2(Lv); % Matching Values
figure
patch([T2.years; flipud(years_1)], [T2.emissions; flipud(emissions_1)], 'r', 'FaceAlpha',0.5)
hold on
patch([years_1; flipud(years_1)], [emissions_1; flipud(emissions_2)],'g', 'FaceAlpha',0.5)
patch([years_2; flipud(years_2)], [emissions_2; zeros(size(years_1))],'b', 'FaceAlpha',0.5)
% patch([yearsNCD; flipud(yearsNCD)], [emissionsNCD; zeros(size(emissionsNCD))], 'r', 'FaceAlpha',0.1)
grid
xlim([min(T2.years) max(T2.years)])
xlabel('Years')
ylabel('Annual Emissions [MtCO_2e]')
legend('Emissions','Emissions_1','Emissions_2', 'Location','NE')
producing —

There are no data prior to 2022 for some of the columns, so that area is blank. There is no logical way to fill it.
AHMED FAKHRI
2021 年 4 月 12 日
Many thanks @Star Strider for the efforts, I really appreictae it. I will certainly accept this answer.
I will also try to delete the data prior to 2022 or add for others from 2017.
Thanks again.
Star Strider
2021 年 4 月 12 日
As always, my pleasure!
It may not be necessary to delete data prior to 2022, simply to decide on how to plot it. One option may be to use the ‘emissions’ data for ‘emissions_1’ and ‘emissions_2’ prior to 2022 (since the abatement efforts had not yet begun). I must leave that to you, since this is your project.
AHMED FAKHRI
2021 年 4 月 12 日
Many thanks That’s a very good suggestion. I will try that the first thing in the morning
Star Strider
2021 年 4 月 12 日
As always, my pleasure!
AHMED FAKHRI
2021 年 4 月 14 日
編集済み: AHMED FAKHRI
2021 年 4 月 14 日
Hi @Star Strider, after some data tidying, I have finally produced nearly the same figure with the 'area' function using the below simple code. Thanks also to you.
Can I ask if you know please how to add the 'arrows' or the 'numbers' as in the original figure?
Many thanks
Years=Finalbalancedscenzip{:,1};
Base=Finalbalancedscenzip{:,2};
post_REEE=Finalbalancedscenzip{:,3};
post_DEC=Finalbalancedscenzip{:,4};
figure
area(Years,Base)
xlim([min(Years) max(Years)])
xlabel('Years')
ylabel('Annual Emissions [MtCO_2e]')
hold on
area(Years,post_REEE)
hold on
area(Years,post_DEC)
legend('REEE Reductions','Deep Dec. Abatement','Remaining Emissions ', 'Location','NE')
Star Strider
2021 年 4 月 14 日
Those are annotation objects, and notoriously difficult to work with because of the ways they are addressed in the plot using normalised coordinates. (Those coordinates and calculations creating them are absolutely not obvious, at least to me.)
I have been struggling with this for a while and cannot figure out how to correctly scale all the locations from the data in the plot so that they will appear where they should be.
I have been able to figure out for this figure only (as I plotted it) to place the x-position of the objects that scales correctly for all the years (here using 2020):
Ax = gca;
pos = Ax.Position;
x = 0.77*(2020 - min(Years)) / (max(Years)-min(Years)) + pos(1)
that works for all the year values in this data set, at least in the plot I created, and for 2020 only the y-values:
y1 = [0.550 0.850];
y2 = [0.500 0.110];
so the annotation for that year would be:
annotation('textarrow', [1 1]*x, y1, 'String',sprintf('%.1f',Base(Years==2020)))
annotation('arrow', [1 1]*x, y2)
I must leave it to you to experiment with the ‘y1’ and ‘y2’ values for the other years you want to plot. I will continue to work on this to see if I can come up with something more general (I cannot promise anything), however in the interests of time, will stop here for now.
AHMED FAKHRI
2021 年 4 月 14 日
That is brilliant, many thanks for the efforts, I will also work from my side and update you if I managed to do it.
Star Strider
2021 年 4 月 14 日
I very much appreciate your compliment!
As always, my pleasure!
その他の回答 (1 件)
Jithin Nambiar J
2021 年 4 月 12 日
If you can plot the y values and x values for the 3 different ranges. You can use
hold on
after the first plot and at the end of last plot use
hold off
1 件のコメント
AHMED FAKHRI
2021 年 4 月 12 日
Hi, thanks I know how to use hold on and off but I need to plot the y values first using one scale or be able to hide other scales
カテゴリ
ヘルプ センター および File Exchange で 2-D and 3-D Plots についてさらに検索
製品
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
