Hi All
I have the atatched Excel sheet and I want please to plot a stacked bar chart in every year (2025, 2030, 2040, 2050) according to the three techs (electrics, H2, CCS+BECCS). I want the legends to be unique in terms of the colour and do not change if the year or the tech is changed which is where I mostly struggeling.
I don't mind using other plotting method if easier.
I tried using the following code but the legends will be different for each sector.
bar(1,[E25{:,2}],'stacked')
tech=['Electric'];
set(gca,'xticklabel',tech)
legend (E25{:,1})
hold on
bar(2,[H25{:,2}],'stacked')
tech2=['Hydrogen'];
set(gca,'xticklabel',tech2)
hold on
bar(3,[CCS25{:,2}],'stacked')
tech3=['CCS+BECCS'];
set(gca,'xticklabel',tech3)
The final graph should looks like this:

 채택된 답변

Adam Danz
Adam Danz 2021년 5월 22일
편집: Adam Danz 2021년 5월 22일

0 개 추천

Legend demo
Create the bar plots and assign the bar colors. Combine the bar handles within the same legend and specify the legend strings. This is done with tiledlayout where you can more easily control the position of a global legend.
rng('default')
x1 = rand(3,8);
x2 = rand(4,10);
fig = figure();
fig.Position(3:4) = [725,420];
tiledlayout(1,2)
nexttile
bh1 = bar(x1,'stacked');
colors1 = mat2cell(lines(numel(bh1)),ones(numel(bh1),1), 3);
set(bh1, {'CData'}, colors1)
nexttile
bh2 = bar(x2,'stacked');
set(bh2,'FaceColor','flat');
colors2 = mat2cell(colorcube(numel(bh2)),ones(numel(bh2),1), 3);
set(bh2, {'CData'}, colors2)
% Define one label per bar handle
legHandles = [bh1, bh2];
labels = ["Cement","Ethylene","Glass","Lime","Gas","Oil","Paper","Vehicles","Coal",...
"Ammonia","Shale Gas","NRMM","Foo","Bar","Other","Gluposti","Waste","Refining"];
lg = legend(legHandles,labels,'Orientation','Horizontal','NumColumns',6);
lg.Layout.Tile = 'North';
Colorbar demo
rng('default')
x1 = rand(3,8);
x2 = rand(4,10);
fig = figure();
fig.Position(3:4) = [725,420];
tiledlayout(1,2)
nexttile
bh1 = bar(x1,'stacked');
colors1 = mat2cell(lines(numel(bh1)),ones(numel(bh1),1), 3);
set(bh1, {'CData'}, colors1)
nexttile
bh2 = bar(x2,'stacked');
set(bh2,'FaceColor','flat');
colors2 = mat2cell(colorcube(numel(bh2)),ones(numel(bh2),1), 3);
set(bh2, {'CData'}, colors2)
% Define one label per color
allColors = [vertcat(colors1{:}); vertcat(colors2{:})];
labels = ["Cement","Ethylene","Glass","Lime","Gas","Oil","Paper","Vehicles","Coal",...
"Ammonia","Shale Gas","NRMM","Foo","Bar","Other","Gluposti","Waste","Refining"];
% Combine bar color and use them to define the colormap
ax = gca;
ax.Colormap = allColors;
cb = colorbar();
cmapInterval = 1/size(allColors,1);
cb.Ticks = cmapInterval/2 : cmapInterval : 1;
cb.TickLabels = labels;

댓글 수: 8

AHMED FAKHRI
AHMED FAKHRI 2021년 5월 22일
편집: AHMED FAKHRI 2021년 5월 22일
Many thanks @Adam Danz for your help, I really appreciate your time.
i think the bar graphs produced not corresponding to what I want. It is may be my fault for not explaining it well.
There should be 4 bar graphs:
1- 2025 for electric, H2, CCS+BECCS on x-axis. y-axis represents the stacked values by each sector.
2- 2030 for electric, H2, CCS+BECCS on x-axis.
3- 2040 for electric, H2, CCS+BECCS on x-axis.
4- 2050 for electric, H2, CCS+BECCS on x-axis.
The legends in all of the four graphs should be the same colour per sector.
Please see the 4 bar graphs required:
How are you producing the bar plots? If you included the lines of code that read-in the data and produce the input variables in your question I could run the code but I don't know what those values are.
The order of the stacked colors should be the same for each stacked-bar unless you're manually changing the colors. If there are any missing data (NaN) those colors should automatically be excluded. For example, the 2nd bar below is missing the 2nd sector (orange).
x = rand(4,3);
x(2,2) = NaN;
bar(x,'stacked')
AHMED FAKHRI
AHMED FAKHRI 2021년 5월 22일
Many thanks @Adam Danz again.
Let me please explain to you what each bar graph represents.
Let us simplify the issue by taking the data for 2025 only.
In this year, 2025, there are a number of industrial sectors ( coal mine, food&drink, etc) that use technologies to decarbonise its processes. these technologies actually are using electricity (electric), or using Hydrogen (H2) or using carbon capture and storage (CCS).
Therefore, looking at the data (Excel sheet ) for 2025, the electric bar should aggregate the emissions for:
'Coal Mine (open)' , 'Food & Drink' , , Gas Platform' , 'Gas Terminal' , 'Glass' , NRMM' , 'Oil Terminal'
'Other Chemicals' , 'Other Iron and Steel' , 'Other industry' , 'Paper' , 'Vehicles'
the total sum is 0.603913 MtCO2e
Similarly, for 2025, the H2 bar should aggreagte: NRMM, 'Other Chemicals', and 'Vehicles',
the CCS+BECCS should aggregate: 'Paper, 'ammonia' and 'Refining'.
If we see now the 2025 total stacked bar graph below:, it is clear that the Electric bar reaches around 0.6 as a total from 12 sectors (as above) with each sector has a different colour.
Now when plotting for the year 2030, 2040, 2050, similar bar graphs should appear however the colour for the sectors used in 2025 should not change, otherwise it causes confusion.
AHMED FAKHRI
AHMED FAKHRI 2021년 5월 22일
Oh, I just realised that your code does not take into account the Excel sheet (data I want to plot) which I attached in the post?
Adam Danz
Adam Danz 2021년 5월 22일
편집: Adam Danz 2021년 5월 22일
The code in your example doesn't include code needed to read in the file and doesn't define variables E25, H25, etc. I've learned not to invest too much time into guessing what someone is doing, especially when the problem appears to be with that process.
You mentioned that the 2025 Electric bar should sum to 0.603913 but clearly it isn't higher than ~.58 according to the image you shared in the comment above.
That indicates that the estimate of 0.603913 is off or the bar input data are not correct.
The colors in your plots also indicate that you are changing the bar colors which isn't shown in your code either. For example, why are the first stack of bars for Electric and Hydro light green but CCS is darker green? That's not default Matlab behavior.
Adam Danz
Adam Danz 2021년 5월 22일
편집: Adam Danz 2021년 5월 22일
Your table consists of 26 rows or sectors.
This demo quickly reads in the table (improvements should be made to preserve the header names).
It then quickly plots the first years of data (2025, 2030, 2040) (improvements can be made to use dot indexing rather than hard-coded column numbers).
Then it applies a random color map to each axes containing a different color for each sector. Improvements should be made here because random colors may not produce visually distinctive colors.
T = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/626403/emissions_sectors2.xlsx');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
fig = figure();
fig.Position(3:4) = [755,420];
tiledlayout(1,3)
ax1 = nexttile;
b1 = bar(T{:,2:4}','stacked');
ax2 = nexttile;
b2 = bar(T{:,5:7}','stacked');
ax3 = nexttile;
b3 = bar(T{:,8:10}','stacked');
rng(386)
cmap = rand(height(T),3);
set([ax1, ax2, ax3], 'ColorOrder', cmap)
legHandles = [b1 b2 b3];
lg = legend(legHandles,T.Sector,'Orientation','Horizontal','NumColumns',4);
Warning: Ignoring extra legend entries.
lg.Layout.Tile = 'North';
Your data has a lot of missing values (NaNs in matlab.! Those values will naturally not appear in the stacked bars. Notice that the first vlaue of H2 in 2025 is NRMM which is peach-ish colored in the legend. This is also the first color that appears in bar #2 in plot 1.
AHMED FAKHRI
AHMED FAKHRI 2021년 5월 22일
To be clear, the graphs I quoted are not plotted by me, these are plotted by the orignal produced and I am trying to replicate them. Off course there will be differences in terms of the values because how they interepret electric techs or H2 techs is different to what I interepret.
The code I included just as an example of my old why which did not work of course, So I shared the Excel data instead but it was just indicative.
Many thanks for the last code, it seems this is what I want, I will check now and accept your answer, I really thanks you and apologize for the misunderstanding
Adam Danz
Adam Danz 2021년 5월 23일
I see. I hope you make better color choices than they did 😄.
There are a bunch of colormap functions on the file exchange that may help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Annotations에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2021년 5월 22일

댓글:

2021년 5월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by