Matlab 中缺少误差线
Errorbar line missing in Matlab
以下代码生成显示的图像:
probabilities = datasetlist(1,:);
avgscores = datasetlist(2,:);
x = probabilities;
y = probabilities;
err = avgscores;
hold on
for k = 1:length(x)
e1 = errorbar(x(k),y(k),err(k),'-');
if err(k) == min(err)
set(e1,'Color','r')
set(e1,'MarkerEdgeColor','r')
set(e1,'Marker','*')
else
set(e1,'Color','k')
set(e1,'MarkerEdgeColor','k')
set(e1,'Marker','.')
end
end
hold on
e1.LineStyle = '-';
但是,应该有一条线连接数据点。我什至设置了 e1.LineStyle
,但这没有用。我怎样才能生产那条线?
你没有线,因为你没有绘制向量,而是每次绘制单个值,这就是为什么你得到的东西更接近 scatter
图。
以下是解决此问题的两种方法:
Solution 1
是一种对现有代码进行最少更改的解决方法。
Solution 2
是更短的代码,通过直接绘制向量来获得相同的结果。 (推荐)。
function q40765062
%% Prepare the data:
datasetlist = [0.4:0.05:0.7; abs(randn(1,7))];
probabilities = datasetlist(1,:);
avgscores = datasetlist(2,:);
x = probabilities;
y = probabilities;
err = avgscores;
%% Solution 1:
figure();
hold on
for k = 1:length(x)
e1 = errorbar(x(k),y(k),err(k),'-');
if err(k) == min(err)
set(e1,'Color','r')
set(e1,'MarkerEdgeColor','r')
set(e1,'Marker','*')
else
set(e1,'Color','k')
set(e1,'MarkerEdgeColor','k')
set(e1,'Marker','.')
end
end
plot(x,y,'-k'); % < The main difference in this solution.
%% Solution 2:
figure();
[~,I] = min(err); % < We compute the index of the minimal value before plotting anything.
errorbar(x,y,err,'-*k'); hold on; % < Notice how we use the entire vectors at the same time.
errorbar(x(I),y(I),err(I),'-*r'); % < We only plot one value this time; in red.
以下代码生成显示的图像:
probabilities = datasetlist(1,:);
avgscores = datasetlist(2,:);
x = probabilities;
y = probabilities;
err = avgscores;
hold on
for k = 1:length(x)
e1 = errorbar(x(k),y(k),err(k),'-');
if err(k) == min(err)
set(e1,'Color','r')
set(e1,'MarkerEdgeColor','r')
set(e1,'Marker','*')
else
set(e1,'Color','k')
set(e1,'MarkerEdgeColor','k')
set(e1,'Marker','.')
end
end
hold on
e1.LineStyle = '-';
但是,应该有一条线连接数据点。我什至设置了 e1.LineStyle
,但这没有用。我怎样才能生产那条线?
你没有线,因为你没有绘制向量,而是每次绘制单个值,这就是为什么你得到的东西更接近 scatter
图。
以下是解决此问题的两种方法:
Solution 1
是一种对现有代码进行最少更改的解决方法。Solution 2
是更短的代码,通过直接绘制向量来获得相同的结果。 (推荐)。
function q40765062
%% Prepare the data:
datasetlist = [0.4:0.05:0.7; abs(randn(1,7))];
probabilities = datasetlist(1,:);
avgscores = datasetlist(2,:);
x = probabilities;
y = probabilities;
err = avgscores;
%% Solution 1:
figure();
hold on
for k = 1:length(x)
e1 = errorbar(x(k),y(k),err(k),'-');
if err(k) == min(err)
set(e1,'Color','r')
set(e1,'MarkerEdgeColor','r')
set(e1,'Marker','*')
else
set(e1,'Color','k')
set(e1,'MarkerEdgeColor','k')
set(e1,'Marker','.')
end
end
plot(x,y,'-k'); % < The main difference in this solution.
%% Solution 2:
figure();
[~,I] = min(err); % < We compute the index of the minimal value before plotting anything.
errorbar(x,y,err,'-*k'); hold on; % < Notice how we use the entire vectors at the same time.
errorbar(x(I),y(I),err(I),'-*r'); % < We only plot one value this time; in red.