GCA功能无法改变轴标签

时间:2017-05-11 16:33:37

标签: matlab bar-chart

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

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

这是我的MatLab输出:

enter image description here

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

这是我的代码:

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);

2 个答案:

答案 0 :(得分:1)

看来问题是你在运行

时重绘了标尺
legend(bar(ALL_DATA),ll);

你应该只做

legend(ll);

答案 1 :(得分: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,...您确切知道您正在处理哪个传奇。

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