在图上用数据名替换x轴

时间:2022-10-18 23:57:42

There are three data points in column vector SP that I am trying to plot. Since I am new to MatLab, I still face following issue with plotting:

我试图绘制列向量SP中有三个数据点。由于我是MatLab的新手,我仍然面临以下绘图问题:

Problem 1: How do I replace X-axis with the name of each data point?

问题1:如何用每个数据点的名称替换X轴?

Problem 2: How do I add the value of each data point on the top of every point on the plot?

问题2:如何在绘图上的每个点的顶部添加每个数据点的值?

Note: From a reply to my previous question, I entered the name of each data point as a cell array (not as a character), but that does not work.

注意:从对上一个问题的回复中,我将每个数据点的名称输入为单元格数组(而不是字符),但这不起作用。

This is my code:

这是我的代码:

SP = [4.3376 4.4005 5.0925];
plot(SP,'-o');
title('SP and YP monthly returns');
xlabel('Monthly time series');
axis([0 4 0 7]);
labelCell = {'' '' 'Asset1' '' 'Asset2' '' 'Asset3' ''};
set(gca, 'xTickLabel', cellstr(MMM));
ylabel('Sharpe Ratio');

My output in MatLab:

我在MatLab中的输出:

在图上用数据名替换x轴

My desired output: 在图上用数据名替换x轴

我想要的输出:

1 个解决方案

#1


1  

  1. It isn't working because you aren't using the correct variable when you are setting the XTickLabels. You are using MMM. Instead, it should be:

    它不起作用,因为在设置XTickLabel时没有使用正确的变量。您正在使用MMM。相反,它应该是:

    labelCell = {'' '' 'Asset1' '' 'Asset2' '' 'Asset3' ''};
    set(gca, 'xTickLabel', labelCell);
    

    A better option that would allow you to not have to create the '' labels would be to manually specify the XTick locations as well

    一个更好的选项可以让您不必创建''标签,也可以手动指定XTick位置

    set(gca, 'XTick', 1:numel(SP), 'XTickLabel', {'Asset1', 'Asset2', 'Asset3'})
    
  2. You can add text objects to your plot to place the labels above your data

    您可以在绘图中添加文本对象,将标签放在数据上方

    for k = 1:numel(SP)
        text(k, SP(k), {num2str(SP(k)), ''}, ...
            'HorizontalAlignment', 'center', ...
            'verticalalignment', 'bottom')
    end
    

    Note that for the String, I use a cell array with an empty second element. This inserts an empty line between the label and the datapoint rather than manually having to compute the y value.

    请注意,对于String,我使用带有空第二个元素的单元格数组。这会在标签和数据点之间插入一条空行,而不是手动计算y值。

#1


1  

  1. It isn't working because you aren't using the correct variable when you are setting the XTickLabels. You are using MMM. Instead, it should be:

    它不起作用,因为在设置XTickLabel时没有使用正确的变量。您正在使用MMM。相反,它应该是:

    labelCell = {'' '' 'Asset1' '' 'Asset2' '' 'Asset3' ''};
    set(gca, 'xTickLabel', labelCell);
    

    A better option that would allow you to not have to create the '' labels would be to manually specify the XTick locations as well

    一个更好的选项可以让您不必创建''标签,也可以手动指定XTick位置

    set(gca, 'XTick', 1:numel(SP), 'XTickLabel', {'Asset1', 'Asset2', 'Asset3'})
    
  2. You can add text objects to your plot to place the labels above your data

    您可以在绘图中添加文本对象,将标签放在数据上方

    for k = 1:numel(SP)
        text(k, SP(k), {num2str(SP(k)), ''}, ...
            'HorizontalAlignment', 'center', ...
            'verticalalignment', 'bottom')
    end
    

    Note that for the String, I use a cell array with an empty second element. This inserts an empty line between the label and the datapoint rather than manually having to compute the y value.

    请注意,对于String,我使用带有空第二个元素的单元格数组。这会在标签和数据点之间插入一条空行,而不是手动计算y值。