在MatLab中绘制X轴 [英] Plotting x-axis in MatLab
问题描述
使用以下变量:
-
SP
:包含随机数的已知8x1行向量. -
YP
:包含随机数的已知8x1行向量.
SP
: a known 8x1 row Vector containing random numbers.YP
: a known 8x1 row Vector containing random numbers.
我有八个数据点.对于x轴上的每8个数据点,我有以下标签:
I have eight data points. For every 8 data points on x-axis, I have the following labels:
资产1-资产2-资产3-资产4-资产5-资产6-资产7-资产8
Asset1 - Asset2 - Asset3 - Asset4 - Asset5 - Asset6 - Asset7 - Asset8
问题:实施代码时,我得到了无数的上述标签.
这是我从MatLab得到的输出:
This is my output from MatLab:
这是我的代码:
SP = rand(8,1)/100;
YP = (rand(8,1)/100)*2;
plot(YP,'DisplayName','YP');
hold on;
plot(SP,'DisplayName','SP');
hold off;
title('SP and YP monthly returns');
xlabel('Monthly time series');
MY = 'Siemens SAP Daimler Allianz DEU.Telekom Adidas BMW DEU.Bank';
set(gca, 'xTickLabels', 'Asset1 Asset2 Asset3 Asset4 Asset5 Asset6 Asset7 Asset8');
xticklabel_rotate('Asset1 Asset2 Asset3 Asset4 Asset5 Asset6 Asset7 Asset8');
ylabel('Percentage of prices discounts');
set(gca, 'yTickLabels', num2str(100.*get(gca,'yTick')','%g%%'));
推荐答案
您需要使用 'XTickLabel'
,而不是当您像以前那样传递字符数组时,MATLAB只会为每个刻度标签回收整个对象.您必须将单元格数组传递为 xticklabel_rotate
如果您仍然想旋转标签,也可以.但是,较新版本的MATLAB允许您通过修改 'XTickLabelRotation'
属性:
When you pass a character array like you did, MATLAB just recycles the whole thing for every tick label. You'll have to pass a cell array to xticklabel_rotate
as well if you still want to rotate the labels. However, newer versions of MATLAB allow you to do this by modifying the 'XTickLabelRotation'
property:
set(gca, 'XTickLabelRotation', 45); % Rotate by 45 degrees
这篇关于在MatLab中绘制X轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!