【问题标题】:How to get string value centered above bars in bar chart如何在条形图中的条形上方居中获取字符串值
【发布时间】:2019-05-14 00:56:56
【问题描述】:

我无法将以下条形图中正确条形上方的文本对齐,我不知道哪里出了问题?

代码:

bar(two_weeks,zAxis);
text(1:length(two_weeks),zAxis,num2str(zAxis),'vert','bottom','horiz','center');
box off
ylabel('Z Axis')

条形图:

箭头是在后期制作中添加的,并显示了它们应该对齐的位置。另请注意,我懒得画所有的箭头。


数据:

two_weeks = 
 1×14 datetime array
 [ 21-Nov-2018, 22-Nov-2018, 23-Nov-2018, 24-Nov-2018, 25-Nov-2018, 26-Nov-2018, 27-Nov-2018, ...
   28-Nov-2018, 29-Nov-2018, 30-Nov-2018, 01-Dec-2018, 02-Dec-2018, 03-Dec-2018, 04-Dec-2018 ]

zAxis = 
 [ 5, 12, 1, 7, 13, 24, 2, 27, 62, 0, 3, 17, 74, 4 ].'

【问题讨论】:

  • 也许text(0:length(two_weeks)-1, ... 修正了对齐方式。
  • @rinkert 是的,这行得通,我认为 MatLab 中的所有内容都是 1 索引而不是 0 索引。为什么这个实例会发生变化?
  • @SardarUsama 我的问题不是问如何在我的 matlab 图上添加值标签,我已经能够做到这一点
  • @SPYBUG96,确实如此,但在这种情况下,您没有编制索引!您正在为文本指定 x 值。显然第一个日期对应于 x 位置 0。

标签: matlab bar-chart matlab-figure


【解决方案1】:

您的 x 轴是使用 datetime 数组指定的。然后,您使用的是猜测来为您的 text 项目的 x 坐标对齐索引 (1:length(two_weeks))。

相反,只需使用相同的datetime 数组作为text 的位置!

bar( two_weeks, zAxis );
text( two_weeks, zAxis, arrayfun(@num2str,zAxis,'uni',0) );

正如您在问题中所做的那样,我们希望将 'VerticalAlignment' 设置为 'bottom' 并将 'HorizontalAlignment' 设置为 'center' 以整理栏杆上方的东西:

bar( two_weeks, zAxis );
text( two_weeks, zAxis, arrayfun(@num2str,zAxis,'uni',0), ...
      'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'center' );

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多