【问题标题】:Matlab scatter and histogram plotMatlab散点图和直方图
【发布时间】:2015-08-27 22:09:14
【问题描述】:

我有 20 个带有 x 和 y 坐标的数字数据点。我想将它们绘制在二维图中。它们将集中在 x 和 y 坐标周围。为了更好地可视化这种行为,我想在 x 轴的 2D 散点图顶部添加直方图条,并在 y 轴的 2D 图右侧添加直方图条,这样它们不会干扰轴标签.现在,我的 20 个数字点实际上是两组 10 个,我希望两组都用不同的颜色绘制。像这样的东西: python plot

我该怎么做?

更新:

FWHM11Avg    = [3.88,3.43,3.16,3.22,3.73,2.43,2.88,3.01,3.59,2.17];
FWHM11Med    = [4.4,3.1,3,3.15,3.9,2,3.00,2.85,3.85,2.2];
FWHM12Avg    = [3.50,2.30,2.97,2.97,2.98,2.28,2.94,2.36,3.51,1.7];
FWHM12Med    = [3.3,2.1,2.9,2.8,2.9,2.1,2.8,2.30,3.5,1.7];

minx = min([FWHM11Avg; FWHM11Med]);
maxx = max([FWHM11Avg; FWHM11Med]);
miny = min([FWHM12Avg; FWHM12Med]);
maxy = max([FWHM12Avg; FWHM12Med]);

% make figure
figure(1)
clf
% first subplot -- y-data histc
ah1 = subplot(2, 2,  1);
y_bins = 1.5:.25:4.5;
n = hist(FWHM12Avg, y_bins);
bar(y_bins, n, 'vertical', 'on')
hold on
hist(FWHM12Med, y_bins)
bar(y_bins, n, 'vertical', 'on')
% x-data histc
ah2 = subplot(2, 2, 4);
x_bins = 1.5:.25:4.5;
n = hist(FWHM11Avg, x_bins);
bar(x_bins, n, 'horizontal', 'on')
hold on
n = hist(FWHM11Med, x_bins);
bar(x_bins, n, 'horizontal', 'on')
% scatterplot
ah3 = subplot(2, 2, 2);
hold on
scatter(FWHM11Avg, FWHM11Med)
scatter(FWHM12Avg, FWHM12Med)

% link axes, adjust histc orientation
linkaxes([ah1, ah3], 'y')
linkaxes([ah3, ah2], 'x')
set(ah3,'XLim',[minx, maxx]);
set(ah3,'YLim',[miny, maxy]);
ah1.Box = 'off';
ah1.View = [180, -90];
ah1.Visible = 'off';
ah2.Visible = 'off';
ah2.Box = 'off';
ah2.View = [0, -90];

此外,似乎没有可用于将数值轴添加到直方图中以查看条形图中有多少点的选项 - 至少在文档中我没有看到任何选项。是这样吗?

对上述语法应用建议的第二次更新:

FWHM11Avg    = [3.88,3.43,3.16,3.22,3.73,2.43,2.88,3.01,3.59,2.17];
FWHM11Med    = [4.4,3.1,3,3.15,3.9,2,3.00,2.85,3.85,2.2];
FWHM12Avg    = [3.50,2.30,2.97,2.97,2.98,2.28,2.94,2.36,3.51,1.7];
FWHM12Med    = [3.3,2.1,2.9,2.8,2.9,2.1,2.8,2.30,3.5,1.7];

minx = min([FWHM11Avg; FWHM11Med]);
maxx = max([FWHM11Avg; FWHM11Med]);
miny = min([FWHM12Avg; FWHM12Med]);
maxy = max([FWHM12Avg; FWHM12Med]);

% make figure
figure(1)
clf
% first subplot -- y-data histc
ah1 = subplot(2, 2,  1);
y_bins = 1.5:.25:4.5;
n = hist(FWHM12Avg, y_bins);
bar(y_bins, n, 'vertical', 'on')
hold on
hist(FWHM12Med, y_bins)
bar(y_bins, n, 'vertical', 'on')
% x-data histc
ah2 = subplot(2, 2, 4);
x_bins = 1.5:.25:4.5;
n = hist(FWHM11Avg, x_bins);
bar(x_bins, n, 'horizontal', 'on')
hold on
n = hist(FWHM11Med, x_bins);
bar(x_bins, n, 'horizontal', 'on')
% scatterplot
ah3 = subplot(2, 2, 2);
hold on
scatter(FWHM11Avg, FWHM11Med)
scatter(FWHM12Avg, FWHM12Med)

% link axes, adjust histc orientation
linkaxes([ah1, ah3], 'y')
linkaxes([ah3, ah2], 'x')
set(ah3,'XLim',[minx, maxx]);
set(ah3,'YLim',[miny, maxy]);
set(ah1,'Box','off');
set(ah1,'View',[180, -90]);
set(ah1,'Visible','off');
set(ah2,'Visible','off');
set(ah2,'Box','off');
set(ah2,'View',[0, -90]);

【问题讨论】:

    标签: matlab matplotlib histogram scatter-plot


    【解决方案1】:

    请在询问之前进行研究。 Matlab scatterhist 中有一个函数可以做到这一点

    x0 = 6.1;
    y0 = 3.2;
    n = 50;
    r = rand(n ,1 );
    theta = 2*pi*rand(n, 1);
    x = x0 + r.*cos(theta);
    y = y0 + r.*sin(theta);
    scatterhist(x,y, 'Direction','out', 'Location', 'NorthEast')
    

    编辑:使用您提供的数据。这是你想要的吗?

    FWHM11Avg    = [3.88,3.43,3.16,3.22,3.73,2.43,2.88,3.01,3.59,2.17];
    FWHM11Med    = [4.4,3.1,3,3.15,3.9,2,3.00,2.85,3.85,2.2];
    FWHM12Avg    = [3.50,2.30,2.97,2.97,2.98,2.28,2.94,2.36,3.51,1.7];
    FWHM12Med    = [3.3,2.1,2.9,2.8,2.9,2.1,2.8,2.30,3.5,1.7];
    
    % make figure
    figure(1)
    clf
    
    FWHM11Avg = FWHM11Avg(:);
    FWHM11Med = FWHM11Med(:);
    FWHM12Avg = FWHM12Avg(:);
    FWHM12Med = FWHM12Med(:);
    
    minX = min([FWHM11Avg; FWHM12Avg]);
    maxX = max([FWHM11Avg; FWHM12Avg]);
    minY = min([FWHM11Med; FWHM12Med]);
    maxY = max([FWHM11Med; FWHM12Med]);
    
    resX = 0.25;
    resY = 0.25;
    
    nBinsX = ceil((maxX - minX) / resX);
    nBinsY = ceil((maxY - minY) / resY);
    
    label = vertcat( ...
                num2cell(repmat('FWHM11', size(FWHM11Avg)),2), ...
                num2cell(repmat('FWHM12', size(FWHM11Avg)),2));
    
    Avg = vertcat(FWHM11Avg, FWHM12Avg);
    Med = vertcat(FWHM11Med, FWHM12Med);
    
    % scatterplot
    scatterhist(Avg, Med, 'Group', label, 'Direction','out', ...
        'Location', 'NorthEast', 'NBins', [nBinsX, nBinsY])
    

    【讨论】:

    • 感谢您提出的解决方案。不幸的是,在我看来,之前的尝试需要较少的调整才能让它按照我需要的方式工作。就像带有数字的条形高度轴、数据标签、轴标签(应该是 x->11 和 y->12)。在其他语法中,我还看到了在哪里调整一些东西,由于我在 Matlab 中的经验非常有限,我在您建议的语法中看不到这些。因此,非常感谢您对其他语法的任何帮助。
    【解决方案2】:

    这是我最近一直在使用的东西:

    % generate some random data
    mu = [1 2];
    sigma = [1 0.5; 0.5 2];
    R = chol(sigma);
    my_data1 = repmat(mu,100,1) + randn(100,2)*R;
    mu = [2 1];
    sigma = [3 -0.5; -0.5 2];
    R = chol(sigma);
    my_data2 = repmat(mu,100,1) + randn(100,2)*R;
    
    % find limits
    minx = min([my_data1(:, 1); my_data2(:, 1)]);
    maxx = max([my_data1(:, 1); my_data2(:, 1)]);
    miny = min([my_data1(:, 2); my_data2(:, 2)]);
    maxy = max([my_data1(:, 2); my_data2(:, 2)]);
    
    % make figure
    figure(1)
    clf
    % first subplot -- y-data histogram
    ah1 = subplot(2, 2,  1);
    histogram(my_data1(:, 2), 'Orientation','horizontal', 'Normalization', 'probability', 'BinWidth', 0.5)
    hold on
    histogram(my_data2(:, 2), 'Orientation','horizontal', 'Normalization', 'probability', 'BinWidth', 0.5)
    % x-data histogram
    ah2 = subplot(2, 2, 4);
    histogram(my_data1(:, 1), 'Normalization', 'probability', 'BinWidth', 0.5)
    hold on
    histogram(my_data2(:, 1), 'Normalization', 'probability', 'BinWidth', 0.5)
    % scatterplot
    ah3 = subplot(2, 2, 2);
    hold on
    scatter(my_data1(:, 1), my_data1(:, 2))
    scatter(my_data2(:, 1), my_data2(:, 2))
    
    % link axes, adjust histogram orientation
    linkaxes([ah1, ah3], 'y')
    linkaxes([ah3, ah2], 'x')
    ah3.XLim = [minx, maxx];
    ah3.YLim = [miny, maxy];
    ah1.Box = 'off';
    ah1.View = [180, -90];
    ah1.Visible = 'off';
    ah2.Visible = 'off';
    ah2.Box = 'off';
    ah2.View = [0, -90];
    

    制作这个情节 此代码假定使用最新版本的 MATLAB(我使用 2014b),但可以使用旧的直方图函数(histhistc)和图形对象的 set(..) 语法轻松调整。

    【讨论】:

    • 我有 Matlab R2013a。我想histogram 将被histhistc 替换。但即使在谷歌搜索图形对象和set() 的定义之后,我也不确定这将如何在您的语法中引入。请给个提示好吗?
    • 当然,使用ah3.XLim = [minx, maxx];,在旧语法中这将是set(ah3, 'XLim', [minx, maxx])。 HTH
    • 转换为histc 将类似于y_bins = -2.5:.5:2; n = histc(my_data1(:, 2), y_bins); bar(y_bins, n, 'horizontal', 'on')
    • ah1.Box = 'off';ah2....等)的所有实例更改为set(ah1, 'Box', 'off')等。如果仍然失败,请发布您的错误。
    • 所以这些是错误消息。只需从我的更新代码中删除注释掉的行(解释性 cmets 除外)。 Error using specgraph.barseries/set: The name 'vertical' is not an accessible property for an instance of class 'barseries'. Error in specgraph.barseries (line 64): set(h,args{:}) Error in bar (line 101): h = [h specgraph.barseries('YData',datachk(y(:,k)), ... Error in Untitled (line 19): bar(y_bins, n, 'vertical', 'on')
    猜你喜欢
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 2019-12-27
    • 1970-01-01
    相关资源
    最近更新 更多