【问题标题】:GCA function doen't work to change axis-labelsGCA 功能无法更改轴标签
【发布时间】:2017-05-11 16:33:36
【问题描述】:

我尝试在我的 x 轴上显示 8 个(名称)标签。相反,我得到了 1 到 8 的数字。

问题:在我的previous asked question 中,我使用了gca 函数,它允许我更改轴标签。但是,同样的gca 函数在这里不起作用。

这是我的 MatLab 输出:

我想看到的是 Firm1...Firm8,而不是 1,...8!

这是我的代码:

figure(2);
%four variables: 
      %pi --> 8x1 vector
      %E_R_BL_Idzorek --> 8x1 vector
      %pi_star1 --> 8x1 vector
      %ER_100_TF1 --> 8x1 vector

ALL_DATA=[pi(1,1) E_R_BL_Idzorek(1,1) pi_star1(1,1) ER_100_TF1(1,1);pi(2,1) E_R_BL_Idzorek(2,1) pi_star1(2,1) ER_100_TF1(2,1);pi(3,1) E_R_BL_Idzorek(3,1) pi_star1(3,1) ER_100_TF1(3,1);pi(4,1) E_R_BL_Idzorek(4,1) pi_star1(4,1) ER_100_TF1(4,1);pi(5,1) E_R_BL_Idzorek(5,1) pi_star1(5,1) ER_100_TF1(5,1);pi(6,1) E_R_BL_Idzorek(6,1) pi_star1(6,1) ER_100_TF1(6,1);pi(7,1) E_R_BL_Idzorek(7,1) pi_star1(7,1) ER_100_TF1(7,1);pi(8,1) E_R_BL_Idzorek(8,1) pi_star1(8,1),ER_100_TF1(8,1)];

%plotting it with a bar function
bar(ALL_DATA);

%This is where I have problem with gca function
set(gca,'xticklabel',{'Firm1','Firm2','Firm3','Firm4','Firm5','Firm6','Firm7','Firm8'});

%this is the grid part:
grid on
ll = cell(1,4);
ll{1}='pi'; ll{2}='ERidz'; ll{3}='piTF'; ll{4}='ERTF';  
legend(bar(ALL_DATA),ll);

【问题讨论】:

    标签: matlab bar-chart


    【解决方案1】:

    您使用的是较新版本的 MATLAB,因此您应该使用较新的图形系统。较新的系统基于对象。这使得设置轴之类的属性更容易。例如:

    fh = figure; % creates the figure window save the figure handle to set it's properties
    ax = axes(fh); % creates the axes in the figure, again save the object
    x = rand(8,100);
    h = bar(ax, x); % create the bar graph in your axes object
    % now use the saved object to access the exact feature you want.  This way you always have the thing you want.  No searching.
    ax.XTickLabel = {'Firm1','Firm2','Firm3','Firm4','Firm5','Firm6','Firm7','Firm8'};
    

    保存对象对于跟踪图例和其他内容也很方便。例如:legend(ax,...您确切地知道您正在处理的是哪个图例。

    似乎正在发生的事情是您正在根据需要正确更改 XTick,但随后您使用 legend(bar(... 覆盖了您的图表。这将创建一个新的 bar 图表。尝试将该行更改为 legend(ll)。我仍然建议使用对象系统。

    【讨论】:

      【解决方案2】:

      看来问题是你在运行时重绘了条形

      legend(bar(ALL_DATA),ll);
      

      你应该这样做

      legend(ll);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-06
        • 1970-01-01
        • 2015-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-01
        相关资源
        最近更新 更多