Errorbars on scatter plot

791 visualizzazioni (ultimi 30 giorni)
Milana Asker
Milana Asker il 11 Apr 2019
I have 2 scatter plots in one figure and I have calculated errors associated with x and y directions for each point. How can I plot errorbars (both horizontal and vertical) on each point? I would greatly appreciate your help : )
x1 = [...]; %some data
errx1 = [...]; % some data
y1 = [...]; % some data
erry1 = [...]; % some data
scatter(x1, y1,170,'k','filled'); % scatter plot with log scale
set(gca,'FontSize',20,'xscale','log','yscale','log');
hold on
x2 = [...]; % some data
errx2 = [...]; % some data
y2 = [...]; % some data
erry2 = [...]; % some data
scatter(x2, y2,170,'k','filled','s');
set(gca,'FontSize',20,'xscale','log','yscale','log')

Risposta accettata

Adam Danz
Adam Danz il 11 Apr 2019
Modificato: Adam Danz il 12 Apr 2019
Follow this example. If you get stuck, feel free to ask a follow-up question in the comments below.
The error in this function is expressed as a distance from the marker. If your error bar data are expressed as end points rather than a distance, you'll need to convert them to positive distances from the marker.
  5 Commenti
Adam Danz
Adam Danz il 15 Apr 2019
Modificato: Adam Danz il 15 Apr 2019
Two mistakes, easy to fix.
1) your error bars were plotted correctly but when you called 'scatter', it cleared the axes. Solution: use " hold on "
2) plot the horizontal and vertical bars separately.
The code below is the correct way to plot the errorbars. I've formatted them as black with a line width of 2.
scatter(x1, y1,170,'k','filled');
hold on
eb(1) = errorbar(x1,y1,err_x1, 'horizontal', 'LineStyle', 'none');
eb(2) = errorbar(x1,y1,err_y1, 'vertical', 'LineStyle', 'none');
set(eb, 'color', 'k', 'LineWidth', 2)
Alternatively you could plot the errorbars first so they are behind the marker.
Milana Asker
Milana Asker il 15 Apr 2019
Thank you very much : )

Accedi per commentare.

Più risposte (2)

Reza Monadi
Reza Monadi il 28 Mar 2023
Modificato: Reza Monadi il 28 Mar 2023
You can use the errorbar but turn off the LineStyle by putting it to 'none':
errorbar(x,y,ye, 'LineStyle','none','Marker','.', 'MarkerSize',10)

Bjorn Gustavsson
Bjorn Gustavsson il 14 Apr 2019
Modificato: Bjorn Gustavsson il 15 Apr 2019
You could use something like this function:
function h = scatter_ellipse(X,Y,C,Cov,varargin)
%SCATTER_ELLIPSE - colored ellipse-plot
% SCATTER(X,Y,C,Cov) displays colored ellipses at the locations
% specified by the vectors X and Y (which must be the same size).
%
% C determines the color-hue of the markers. C should be a vector the
% same length as X and Y, the values in C are linearly mapped
% to the colors in the current colormap.
%
% Cov determines the ellipse-size and shape each marker. Cov
% should be a 2 x 2 x nP array with the covariances of the
% C-values at points [X, Y]. For convenience the input arrays X,
% Y and C will be converted to column arrays with the (:)
% operation. In order to avoid sorting confusion it is strongly
% preferable to arrange these input arrays into column (or row)
% arrays so that the covariance matrices will be used for the
% correct points [X, Y, C].
%
% H = SCATTER_ELLIPSE(...) returns handles to the patch objects created.
%
% Calling:
% h = scatter_ellipse(X,Y,C,Cov[,'covclim',clim,'pclr','r','edgecolor','g'])
% Input:
% X - double array [nP x 1], x-coordinates of points
% Y - double array [nP x 1], y-coordinates of points
% C - double array [nP x 1], value of point, linearly mapped
% between min and max of current colourmap.
% COV - covariance matrix for each [Xi, Yi]-point, double array
% [2 x 2 x nP]
% Optional input arguments (property-value pairs):
% Covclims - scaling of covariance-ellipse-area (CEA) colour between
% RGB-triplet (smallest area) towards white (larges
% area) for color of C. Default value [0.1 1], 1 -
% RGB-tripplet for the smallest CEA, 0.1 -
% RGB*0.1+0*[1,1,1] for the point with the largest
% CEA.
% pclr - plot-colour, standard matlab-color specification, but
% with 'rgb' function will plot point with RGB-colour
% corresponding to C.
% edgecolor - edgecolor of ellipse, defaults to 'none'.
% Output:
% h - array with graphics handle to covariance ellipses (plotted
% with fill)
% Example:
% X = 12*randn(31,1);
% Y = 12*randn(31,1);
% for i1 = 1:31,
% sx = 1+3*rand(1);
% sy = 1+3*rand(1);
% x = sx*randn(41,1);
% y = sy*randn(41,1) + randn(1)*x;
% CvM(:,:,i1) = cov([x,y]);
% C(i1) = mean(x)*mean(y);
% end
% clf
% h = scatter_ellipse(X,Y,...
% medfilt1(C),CvM/3,...
% 'pclr','rgb',...
% 'Covclim',[0.15 1],...
% 'edgecolor','none');
%
% The idea is that points with larger covariance ellipse should get
% reduced graphical weight while points with smaller covariance
% ellipses should be more clearly visible. The RGB-blending towards
% white could be seen as each point have a fixed amount of pigment
% spread out to color the area of the ellipse.
% Copyright © Bjorn Gustavsson 20190410, bjorn.gustavsson@uit.no
% This is free software, licensed under GNU GPL version 2 or later
dOPS.Covclim = [0.1 1];
dOPS.pclr = 'rgb';
dOPS.edgecolor = 'none';
for i1 = 1:2:numel(varargin)
curr_option = varargin{i1};
switch lower(varargin{i1})
case 'covclim'
dOPS.Covclim = varargin{i1+1};
case 'pclr'
dOPS.pclr = varargin{i1+1};
case 'edgecolor'
dOPS.edgecolor = varargin{i1+1};
otherwise
end
end
dOPS.Covclim(2) = max(0,min(1,dOPS.Covclim(2)));
dOPS.Covclim(1) = max(0,min(1,dOPS.Covclim(1)));
X = X(:);
Y = Y(:);
C = C(:);
Cmap = colormap;
nCmap = size(Cmap,1);
for i3 = numel(X):-1:1,
[Xe(i3,:),Ye(i3,:),Aellipse(i3)] = ellipse_xy(Cov(:,:,i3));
end
[Aellipse,iS] = sort(Aellipse);
Xe = Xe(iS,:);
Ye = Ye(iS,:);
X = X(iS);
Y = Y(iS);
C = C(iS);
if max(C) ~= min(C)
rgbCp = interp1(linspace(0,1,nCmap),...
Cmap,...
(C-min(C))/(max(C)-min(C)),...
'pchip');
else
rgbCp = repmat(Cmap(end,:),size(C));
end
AeMax = max(Aellipse);
Aemin = min(Aellipse);
Covclim = dOPS.Covclim;
for i1 = numel(X):-1:1,
resaturation = Covclim(2) - (Aellipse(i1)-Aemin)/(AeMax-Aemin)*diff(Covclim);
rgbC(i1,:) = rgbCp(i1,:)*resaturation + ...
[1 1 1]*(1-resaturation);
end
hold_state = get(gca,'nextplot');
hold on
for i1 = numel(X):-1:1;
h(i1) = fill(X(i1)+Xe(i1,:),Y(i1)+Ye(i1,:),rgbC(i1,:));
if strcmp(dOPS.pclr,'rgb')
plot(X(i1),Y(i1),'.','color',rgbCp(i1,:))
else
plot(X(i1),Y(i1),'.','color',dOPS.pclr)
end
end
set(h,'edgecolor',dOPS.edgecolor)
set(gca,'nextplot',hold_state);
function [x,y,Aellipse] = ellipse_xy(C)
p = linspace(0,2*pi,361); % one-degree steps around 2*pi
[eigvec,lambda] = eig(C); % Eigenvalues and eigen-vectors
xy = [cos(p'),sin(p')] * sqrt(lambda) * eigvec'; % Transformation
x = xy(:,1);
y = xy(:,2);
Aellipse = pi*prod(diag(eigval));
HTH
  2 Commenti
Milana Asker
Milana Asker il 15 Apr 2019
Thank you a lot for your answer. Unfortunately, this code seems a bit complicated to me and I would like you to elaborate on it a bit more if it is not too hard for you? ; )
p.s. like where do I put my values for x- and y-coordinates and error values?
Bjorn Gustavsson
Bjorn Gustavsson il 15 Apr 2019
You put your x-coordinates into X, and your y-coordinates into Y. Since you used scatter with constant size (170), you can set C to something constant:
C = 170*ones(size(X));
Then you need to put your errorx1 and errory1 into a stack of covariance-matrices with the same number of levels as number of points in x and y:
for i3 = numel(X):-1:1
CovM(:,:,i3) = [errorx1(i3)^2,0;0,errory1(i3)^2];
end
That shold give you a single-hued scatter-plot with whitening corresponding to the size of your error-ellipse:
scatter_ellipse(X,Y,C,CovM)
HTH

Accedi per commentare.

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Prodotti


Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by