matlab plot errorbar,如何为MATLAB errorbar plot的点和垂直线设置不同的图例?
這建立在
Jens Boldsen’s answer之上,它添加了以下內(nèi)容:
>可以旋轉(zhuǎn)表示圖例中錯誤欄的線條使其垂直,或保持默認的水平方向;
>該行的末尾用短線“關(guān)閉”.
該方法非常通用,因為它支持:
>任意顏色,線型和標記作為錯誤欄的參數(shù).請注意,實際條形圖始終繪制為實線,沒有標記(使用指定的顏色).這是根據(jù)errorbar行為.
>新創(chuàng)建的短線隨圖例移動.
>這些線的寬度是可配置的參數(shù).此外,errobar的長度是垂直情況下的參數(shù).兩者都以相對于圖例的標準化單位定義.
根據(jù)Matlab版本,該方法略有不同,因為R2014b中引入的圖形對象發(fā)生了變化.此外,圖例中的errobar線可以是水平的或垂直的.
所以有四種情況.
案例I:Pre-R2014b,圖例中的水平誤差條
繪制數(shù)據(jù)和圖例后,代碼將遵循以下步驟:
>獲得傳奇的孩子們
>在這些孩子中找到合適的路線
>獲取其x和y坐標
>利用這些坐標,在每一端創(chuàng)建兩條短線.將這些線條作為傳奇的子項,以便它們隨之移動.
該代碼已在Matlab R2010b中測試過.
%// Define graph aspect
color_spec = 'r'; %// color of data and bars.
linestyle_spec = '--'; %// linestyle for data. The bars are always solid
marker_spec = 'o'; %// marker for data
wid = .12; %// width of bar in legend. Normalized units
%// Plot errobar
h_er = errorbar([1 2], [2 3], [0.1 0.2], [color_spec linestyle_spec marker_spec]);
%// Add dummy line for legend (as per Jens Boldsen) and create legend
hold on;
plot(0,0,color_spec); %// linestyle is always solid with no marker
h_le = legend('Mean','Standard deviation','Location','north');
%// Add short lines at each end of line in the legend
c_le = get(h_le, 'Children'); %// step 1
h_li = findobj(c_le, 'linestyle','-', 'color',color_spec, 'marker','none'); %// step 2
h_li = h_li(1); %// in case there's more than one
li_x = get(h_li,'XData'); %// step 3
li_y = get(h_li,'YData');
line(li_x([1 1]), li_y+wid*[-.5 .5], 'parent',h_le, 'color',color_spec); %// step 4
line(li_x([2 2]), li_y+wid*[-.5 .5], 'parent',h_le, 'color',color_spec);
案例II:R2014b,圖例中的水平誤差條
在Matlab R2014b中,圖例不再是軸對象,也沒有子對象.因此,必須針對案例I修改步驟1和2:
>創(chuàng)建圖例時獲取圖例的圖標
>在這些圖標中查找相應(yīng)的行
>獲取其x和y坐標
>利用這些坐標,在每一端創(chuàng)建兩條短線.使這些線與初始線共享父線,以便它們與圖例一起移動.
此外,此Matlab版本中的新語法有助于簡化代碼.
%// Define graph aspect
color_spec = 'r'; %// color of data and bars.
linestyle_spec = '--'; %// linestyle for data. The bars are always solid
marker_spec = 'o'; %// marker for data
wid = .12; %// width of bar in legend. Normalized units
%// Plot errobar
h_er = errorbar([1 2], [2 3], [0.1 0.2], [color_spec linestyle_spec marker_spec]);
%// Add dummy line for legend (as per Jens Boldsen)
hold on;
plot(0,0,color_spec); %// linestyle is always solid with no marker
%// Create legend and add short lines at each end of line in the legend
[~, icons] = legend('Mean','Standard deviation','Location','north'); %// 1
li = findobj(icons, 'type','line', 'linestyle','-'); %// 2
li = li(1); %// in case there's more than one
li_x = li.XData; %// 3
li_y = li.YData;
line(li_x([1 1]), li_y+wid*[-.5 .5], 'parent',li.Parent, 'color',color_spec); %// 4
line(li_x([2 2]), li_y+wid*[-.5 .5], 'parent',li.Parent, 'color',color_spec);
案例III:Pre-R2014b,圖例中的垂直錯誤欄
這與情況I類似,但需要擺弄線條的x和y坐標.此外,引入了一個新參數(shù)來控制圖例中錯誤欄的長度.
%// Define graph aspect
color_spec = 'r'; %// color of data and bars.
linestyle_spec = '--'; %// linestyle for data. The bars are always solid
marker_spec = 'o'; %// marker for data
wid = .04; %// width of bar in legend. Normalized units
len = .45; %// length of main bar in legend. Normalized units
%// Plot errobar
h_er = errorbar([1 2], [2 3], [0.1 0.2], [color_spec linestyle_spec marker_spec]);
%// Add dummy line for legend (as per Jens Boldsen) and create legend
hold on;
plot(0,0,color_spec); %// linestyle is always solid with no marker
h_le = legend('Mean','Standard deviation','Location','north');
%// Add short lines at each end of line in the legend
c_le = get(h_le, 'Children');
h_li = findobj(c_le, 'linestyle','-', 'color',color_spec, 'marker','none');
h_li = h_li(1); %// in case there's more than one
set(h_li,'XData', repmat(mean(get(h_li,'XData')),1,2));
set(h_li,'YData', mean(get(h_li,'YData'))+len*[-.5 .5]);
li_x = get(h_li,'XData');
li_y = get(h_li,'YData');
line(li_x+wid*[-.5 .5], li_y([1 1]), 'parent',h_le, 'color',color_spec);
line(li_x+wid*[-.5 .5], li_y([2 2]), 'parent',h_le, 'color',color_spec);
案例IV:R2014b,圖例中的垂直錯誤欄
同樣,這與案例II類似,但有一些擺弄線的坐標.
%// Define graph aspect
color_spec = 'r'; %// color of data and bars.
linestyle_spec = '--'; %// linestyle for data. The bars are always solid
marker_spec = 'o'; %// marker for data
wid = .05; %// width of small bars in legend. Normalized units
len = .45; %// length of main bar in legend. Normalized units
%// Plot errobar
h_er = errorbar([1 2], [2 3], [0.1 0.2], [color_spec linestyle_spec marker_spec]);
%// Add dummy line for legend (as per Jens Boldsen)
hold on;
plot(0,0,color_spec); %// linestyle is always solid with no marker
%// Create legend, modify line and add short lines
[~, icons] = legend('Mean','Standard deviation','Location','north');
li = findobj(icons, 'type','line', 'linestyle','-');
li = li(1); %// in case there's more than one
li.XData = repmat(mean(li.XData),1,2);
li.YData = mean(li.YData)+len*[-.5 .5];
li_x = li.XData;
li_y = li.YData;
line(li_x+wid*[-.5 .5], li_y([1 1]), 'parent',li.Parent, 'color',color_spec);
line(li_x+wid*[-.5 .5], li_y([2 2]), 'parent',li.Parent, 'color',color_spec);
總結(jié)
以上是生活随笔為你收集整理的matlab plot errorbar,如何为MATLAB errorbar plot的点和垂直线设置不同的图例?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql协议重传,MySQL · 源码
- 下一篇: 虎牙直播如何看回放(直播个人中心)