Legend- if plot is empty?

조회 수: 19 (최근 30일)
Meera
Meera 2015년 6월 26일
답변: Kwin 2016년 2월 3일
Hello,
I would like to how we can write legend for empty plots also. I have set of 4 to 5 plots. But some times among these plots, few plots values may be empty (x=NaN, y=NaN). if the plot is empty, then that the legend is not displaying. Rather this legend is displaying for next plots. How can I display legend for 5 plots even if 2nd or 3rd plot is empty.
Thanking you.
Meera

답변 (2개)

Kwin
Kwin 2016년 2월 3일
I ran into a similar problem when using contour plots, which can also be empty.
The solution I came up with is:
clear all
figure, hold all
fig{1} = contour(rand(10), [0.5 0.5], 'r');
fig{2} = contour(rand(0), [0.5 0.5], 'g');
fig{3} = contour(rand(10), [0.5 0.5], 'b');
name = {'fig1', 'fig2', 'fig3'};
used = {};
for k = 1 : numel(fig)
if ~isempty(fig{k})
used = {used{:}, name{k}};
end
end
legend(used)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 6월 26일
x=nan and y=nan is not an empty plot.
x1=[1 2 3]
x2=[]
y1=[1 2 3]
y2=[]
x={x1,x2}
y={y1,y2}
for k=1:numel(x)
if isempty(x{k})
plot(nan,nan)
else
plot(x{k},y{k})
end
hold on
end
legend({'a' 'b'})
hold off
  댓글 수: 1
Meera
Meera 2015년 6월 29일
Hello,
I even did the same way. But in legend it is always taking top first legend even for 2nd data
if isempty(ref_zero) == true
x_axes = NaN;
y_axes = NaN;
plot(x_axes,y_axes,'cs');
else
x_axes = [];
y_axes = [];
for z = 1:length(ref_zero)
x_axes = [x_axes xdata(ref_zero(z))];
y_axes = [y_axes ydata(ref_zero(z))];
end
if volt_data(1)== 7
plot(x_axes,y_axes,'bs');
elseif volt_data(1)== 13.5
plot(x_axes,y_axes,'gs');
elseif volt_data(1)== 18
plot(x_axes,y_axes,'ks');
end
hold on;
end
plot(x,y_LL,'-r','LineWidth',2)
hold on;
plot(x,y_UL,'-r','LineWidth',2)
grid on;
l = legend('@empty','@ 7.0V','@ 13.5V','@ 18.0V','LL','UL','Location','NorthEastOutside');
hold off;
but the plot get as follows.

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

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by