How to adjust the legend of pie chart

43 ビュー (過去 30 日間)
Chao Zhang
Chao Zhang 2021 年 7 月 25 日
編集済み: Chunru 2021 年 7 月 26 日
The following is my code
figure
%----------------------------------------------------------------------
subplot(2,3,1)%Figure1
pre_ton = [ore_ton waste_ton];
t_labels = {'ORE','WASTE'};
pie(pre_ton)
title('Percentage of tonnage')
legend(t_labels,'Location','southoutside','Orientation','horizontal','FontSize',7)
%----------------------------------------------------------------------
subplot(2,3,2)%Figure2
pre_energy = [drill_energy blast_energy load_energy haul_energy crush_energy grind_energy];
Labels = {'Drill','Blast','Load','Haul','Crush','Grind'};
pie(pre_energy)
title('Energy consumption(%) of each stage')
legend(Labels,'Location','westoutside','Orientation','vertical','FontSize',7)
%----------------------------------------------------------------------
subplot(2,3,3)%Figure3
pre_GWP = [drill_gwp blast_gwp load_gwp haul_gwp crush_gwp grind_gwp];
Labels = {'Drill','Blast','Load','Haul','Crush','Grind'};
pie(pre_GWP)
title('GWP(%) of each stage')
legend(Labels,'Location','westoutside','Orientation','vertical','FontSize',7)
%----------------------------------------------------------------------
subplot(2,3,4)%Figure4
tonnage = diag([ore_ton waste_ton total_ton]);
TONNAGE = bar(tonnage,'stacked');
title('Tonnage of open pit mine')
ylabel('tonnage')
legend('ORE', 'WASTE', 'TOTAL')
%----------------------------------------------------------------------
subplot(2,3,5)%Figure5
energy = diag([ore_energy waste_energy total_energy]);
ENERGY = bar(energy,'stacked');
title('Energy consumption')
ylabel('Energy consumption(MJ)')
legend('ORE', 'WASTE', 'TOTAL')
%----------------------------------------------------------------------
subplot(2,3,6)%Figure6
gwp = diag([ore_GWP waste_GWP total_GWP]);
GWP = bar(gwp,'stacked');
title('Global warming potential')
ylabel('GWP(t CO2 eq)')
legend('ORE', 'WASTE', 'TOTAL')
and the figure is presented below
How to make Figure 2 and Figure 3 share a common legend, and place the legend below the middle of the two figures
After that, how to make the numbers of the pie chart do not overlap and display more clearly
Thanks in advance for your help!

採用された回答

Chunru
Chunru 2021 年 7 月 26 日
編集済み: Chunru 2021 年 7 月 26 日
Adjust the parameters if necessary. The plot in this web livescript is for illustration only.
subplot(232); pie([51 2 45 2]);
subplot(233); pie([24 6 30 40]);
h = legend('drill', 'blast', 'load', 'haul');
h.Location = "south";
h.FontSize = 6;
h.Position(1) = .6;
h.Position(3:4) = h.Position(3:4)*.7; % reduce size if necessary
subplot(234); bar(diag([2 6 8]), 'stacked');
h1 = legend;
h1.FontSize = 6;
h1.Location = "northwest";
  2 件のコメント
Chao Zhang
Chao Zhang 2021 年 7 月 26 日
Thanks for your help!
Can i ask a question? is it possible to change the name of 'data1' 'data2' 'data3' 'data4' in the legend to 'drill' 'blast' 'load' 'haul'?
Chunru
Chunru 2021 年 7 月 26 日
See above.

サインインしてコメントする。

その他の回答 (1 件)

dpb
dpb 2021 年 7 月 26 日
legend is limited in its flexibility, unfortunately. A given axis can have only one legend and the second axis/figure "knows nuthink!" about the other so there's not any way to refer to the properties of the other from whichever one you choose to add the legend to.
The new tiledlayout has shared title and axes labels properties, but not, again, the legend.
The best I can think of would entail drawing the area for the legend by fitting in another subplot into the mix and creating a dummy figure with the various colors assigned but all data NaN so only the legend would end up being visible. Whether you can create enough room in the right place without squishing everything else too much and leaving a bunch of empty space on the left third is, I expect, doubtful.
I just don't see much way to manage what you want.
HINT: Using the 'location','best' pair will go a long ways on your barplots to avoid the data with those legends.
Read through the doc for legend carefully for all the available properties to see what options are for placement and orientation.
As for the pie label placement, the pie chart returns an array of handles to the patch and text objects; the even-numbered ones in the array are those of the labels; you can modify the .Position property to suit.
  1 件のコメント
Chao Zhang
Chao Zhang 2021 年 7 月 26 日
Thanks for your help!

サインインしてコメントする。

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by