string 如何使用字符串作为数据在 Matlab 中绘图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3672637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:50:05  来源:igfitidea点击:

How to use string as data for plotting in Matlab?

stringmatlabplot

提问by Amro

I want to use a words like, let's say, 'A', 'B' and 'C' on X-axis to show their corresponding properties on Y-axis. How can I write these strings on X-axis instead of numerical data?

我想使用诸如 X 轴上的“A”、“B”和“C”之类的词来显示它们在 Y 轴上的相应属性。如何在 X 轴而不是数字数据上写入这些字符串?

回答by Amro

Use 'XTick'and 'XTickLabel'properties of the axes handle.
Here's a simple example:

轴句柄的用途'XTick''XTickLabel'属性。
这是一个简单的例子:

x = 1:5;
y = rand(size(x));
plot(x, y, 'b')
set(gca, 'XTick',1:5, 'XTickLabel',{'A' 'B' 'C' 'D' 'E'})

alt text

替代文字

回答by Geodesic

Set yourself up a cell with your letters (mine's called labels), then use the XTick property to set the same amount of ticks on the x axis as your label number. Finally, the XTickLabel property will write your labels to the x axis.

为自己设置一个带有字母的单元格(我的称为标签),然后使用 XTick 属性在 x 轴上设置与标签编号相同数量的刻度。最后,XTickLabel 属性会将您的标签写入 x 轴。

x = yourXdata;
y = yourYdata;
labels = {'A' 'B' 'C'};
plot(x, y);
set(gca, 'XTick', 1:3, 'XTickLabel', labels);

回答by Adeldime M.S Abdelgader

How to use CHARACTER Values instead of Numerical values in X axis. to label x as T1 T2 T3 T4 just use this : set(gca,'XTick',1:4,'XTickLabel',{'T1', 'T2', 'T3', 'T4'},'FontSize',15)

如何在 X 轴上使用字符值而不是数值。将 x 标记为 T1 T2 T3 T4 只需使用: set(gca,'XTick',1:4,'XTickLabel',{'T1', 'T2', 'T3', 'T4'},'FontSize', 15)

this command can be used after the plot command followed by the xlabel and ylabel , legand commands. you can also adjust the font size.

此命令可以在 plot 命令后跟 xlabel 和 ylabel 、legand 命令之后使用。您还可以调整字体大小。

Practical Example:

实际例子:

    %% 50% Day 
    T1wSI=[54.17 115];
    T2wSI=[53.5 112];
    T3wSI=[52.2 110];
    T4wSI=[51.2 108];

    T1oSI=[50.25 94];
    T2oSI=[49.18 92];
    T3oSI=[48.2 90];
    T4oSI=[46.1 84];

    table1=[T1wSI;T2wSI;T3wSI;T4wSI;T1oSI;T2oSI;T3oSI;T4oSI ];
    season2012=table1(:,1);
    season2013=table1(:,2);
    Tr1=[1 2 3 4];

Treatment1 =['T1wSI' 'T2wSI' 'T3wSI' 'T4wSI' 'T1oSI' 'T2oSI' 'T3oSI' 'T4oSI'];
    %Tre1=['T1' 'T2' 'T3' 'T4'];
    %set(gca,'FontSize',14)
    figure(1)
    set(gca,'XTick',1:4,'XTickLabel',{'T1', 'T2', 'T3', 'T4'},'FontSize',14)
    plot(Tr1,table1(1:4,1),'--bs','LineWidth',3);% 2012
    hold on;
    plot(Tr1,table1(1:4,2),'-go','LineWidth',3);% 2013
    plot(Tr1,table1(5:8,1),'--r*','LineWidth',3); % 2012
    plot(Tr1,table1(5:8,2),'-m^','LineWidth',3);% 2013
    set(gca,'XTick',1:4,'XTickLabel',{'T1', 'T2', 'T3', 'T4'},'FontSize',15)
    xlim=[1 5];
    xlabel('Treatments')
    ylabel('Days to 50 % Flowering')
    legend('With -Season 2012','Without -Season 2013','With -Season 2012','Without - Season 2013','Location','NorthEast');

回答by BajajG

You can also do this using the GUI.

您也可以使用 GUI 执行此操作。

1) Click on the figure axes to to open the Axes Property Editor.

1) 单击图形轴以打开轴属性编辑器。

2) Click on the "More properties" button on the right side of the window. This will open the inspector window of the axes.

2) 单击窗口右侧的“更多属性”按钮。这将打开轴的检查器窗口。

3) Click on the small button next to "XTickLabel" property to open the dialogue box as shown below. enter image description here

3) 点击“XTickLabel”属性旁边的小按钮,打开如下图所示的对话框。 在此处输入图片说明

4) Enter the labels you want and click on "OK".

4) 输入您想要的标签,然后单击“确定”。