【问题标题】:Labeling a string on x-axis vertically in MatLab在 MatLab 中垂直标记 x 轴上的字符串
【发布时间】:2017-10-09 13:42:35
【问题描述】:

使用以下变量:

  • SP:一个已知的 196x1 行向量,包含随机数。
  • YP:一个已知的 196x1 行向量,包含随机数。
  • Names:一个已知的 196x1 列字符串,包含 196 个名称。

问题在 x 轴内。 Names 的数组实际上包含来自“Jan 2000 Feb, 2000, March 2000,..., Dec 2016”。由于我无法在这里模拟这 196 个月,因此我只是在我的代码中使用“for”函数创建了 196 个名称(请参阅编码部分)。

问题:我的 MatLab 输出仅采用 Names 字符串的前九个值。我希望Names 字符串中的所有 196 个数据都垂直发布在 x 轴上。

这是我的 MatLab 输出(见 x 轴):

这就是我希望输出的样子:

这是我的代码:

%define variables
SP = rand(196,1)/100;
YP = (rand(196,1)/100)*2;
%plotting two vectors of SP and YP
plot(YP,'DisplayName','YP');
hold on;
plot(SP,'DisplayName','SP');
hold off;
title('SP and YP monthly returns');
%This is x-axis with creating a string of 196x1 dimensions
xlabel('Monthly time series');
for i=1:196
Names(i,:)='Sample Text'
end
set(gca, 'xTickLabels', Names); 
%y-axis
ylabel('Percentage of prices discounts');
set(gca, 'yTickLabels', num2str(100.*get(gca,'yTick')','%g%%'));

【问题讨论】:

  • 标签轮换在Gnovice's answer 到您一小时前的问题。
  • 这与您现在删除的your previous question 有何不同?正如 excaza 所指出的,您已经在大约一个小时前询问了 your post 中的标签轮换问题。您在已删除的帖子中也提出了这两个问题
  • @excaza 感谢您的链接,但根据最后一个问题我无法弄清楚。感谢下面的回答,我的问题解决了

标签: matlab plot


【解决方案1】:

绘制数据时,您在 x 轴上只有 11 个 xtick,所以当您更改它们的名称时,您有 11 个 xtick 是正常的,因此您可以检查以下代码

%define variables
SP = rand(196,1)/100;
YP = (rand(196,1)/100)*2;
%plotting two vectors of SP and YP
plot(YP,'DisplayName','YP');
hold on;
plot(SP,'DisplayName','SP');
hold off;
title('SP and YP monthly returns');
%This is x-axis with creating a string of 196x1 dimensions
xlabel('Monthly time series');

xTicks=1:5:196;            % EDITED 
                           % number of xticklabel control by xticks variable
set(gca,'xTick',xTicks);   % EDITED

for i=1:196
  Names(i,:)='Sample Text';
end
set(gca, 'xTickLabels', Names);
rotateXLabels(gca,90); %for using this command you must download it or if % 
                       %your matlab version is 2015 or higher you have this 
                       %function in your toolbox

1:5:196 的输出如下图

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    相关资源
    最近更新 更多