0

以下代码生成显示的图像:

    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,但这没有用。我怎样才能生产那条线?

4

1 回答 1

1

您没有线,因为您不绘制向量,而是每次都绘制单个值,这就是为什么您会得到更接近scatter绘图的东西的原因。

以下是解决此问题的两种方法:

  1. Solution 1是一种解决方法,可以最小限度地更改您现有的代码。
  2. 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.
于 2016-11-25T07:29:00.697 回答