使用条形函数时如何在 x 轴上显示分类数据?

How to show categorical data on x-axis when using bar function?

我正在尝试模拟 MATLAB 官方网站上的代码,但无法获得相同的输出。

这是代码:

c = categorical({'apples','oranges','pears'});
prices = [1.23 0.99 2.3];
bar(c,prices)

这是 MATLAB 网站上的正确输出:

这是我在 MATLAB 中得到的输出:

c 数组,即 appleorangepears 未显示在我的 MATLAB 输出中。为什么我得不到相同的输出?

我的 MATLAB 版本是 R2016a。

您可以尝试以下解决方法(如 here 所述):

prices = [1.23 0.99 2.3];
bar(prices)
set(gca,'xticklabel',{'apples','oranges','pears'});

因此,您摆脱了 categorical 并切换到 gca 函数,它允许您更改轴标签。