Delete axis, keep legend

7 ビュー (過去 30 日間)
Alexandre Cucatti dos Santos
Alexandre Cucatti dos Santos 2019 年 8 月 5 日
コメント済み: sofia fellini 2024 年 1 月 5 日
I need to have a subplot with an empty axis, but with a legend. My workaround to put a title on an empty subplot was:
if isempty(data) % Failsafe for empty section
pie(1); % Plots any pie chart
title(data_title);
cla; % Clears axis
end
But it does not work for the legend, it vanishes with it.
How can I deal with it?
  1 件のコメント
Alexandre Cucatti dos Santos
Alexandre Cucatti dos Santos 2019 年 8 月 6 日
So, I have this figure with 6 subplots. The legend for the 3 upper and the 3 lower are the same, so it appears only in the rightmost subplots. However, sometimes the rightmost plot can be like the upper left for this sample (an empty plot created with cla()). I need to make it so that the legend appears with all 3 entries even for cases like this.
For reasons of confidenciality, the titles and legend entries must be censored

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

採用された回答

Adam Danz
Adam Danz 2019 年 8 月 5 日
編集済み: Adam Danz 2019 年 8 月 6 日
To create a legend without any visible data using plot().
plot(nan, 'r-', 'DisplayName', 'MyLegendName');
legend();
Note: cla() will still clear the legend but you won't need to call cla() because nothing is plotted.
To create a legend without any visible data using pie()
% Create a dummy pie chart and its legend
h = pie([1,1,1]);
lh = legend('a','b','c')
% Delete non-patch objects
hPatch = h(strcmp(get(h,'type'),'patch'));
hNotPatch = h(~strcmp(get(h,'type'),'patch'));
delete(hNotPatch)
% Remove Faces of patches
set(hPatch,'Faces', NaN)
  3 件のコメント
Adam Danz
Adam Danz 2019 年 8 月 6 日
編集済み: Adam Danz 2019 年 8 月 6 日
I updated my answer to show how this is done with pie charts.
First you must create a pie chart and its legend. Then you must remove the text and then remove the faces of each patch by replacing their values with NaN. That makes the entire pie chart go away and retains the legend.
sofia fellini
sofia fellini 2024 年 1 月 5 日
Thank you @Adam Danz !! I was looking for this today!

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

その他の回答 (0 件)

カテゴリ

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