如何在MATLAB图的顶部添加(值的)标签? [英] How can I add labels (of values) to the top of my MATLAB plot?

查看:1244
本文介绍了如何在MATLAB图的顶部添加(值的)标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用hist命令(可以将命令的输出分配给两个矩阵)手动获取计数并在其中使用数据集上的plot命令在MATLAB中手动"创建直方图之后中点.我真的很想做的是在直方图上每个条形上方添加一个标签,指明该列的中心点值. 由于我已经有了一个包含所有这些中心值的向量,因此我的问题在于弄清楚如何实际创建标签并将其放置在每个条形上方.添加这些内容的任何帮助将不胜感激!

I'm creating a histogram "manually" in MATLAB using the plot command on a dataset after using the hist command (where I can assign the output of the command to two matricies) to manually get the counts and midpoints. What I would really love to do is add a label above each of the bars on my histogram stating the centerpoint value of that column. Since I already have a vector containing all of those center values, my issue lies in figuring out how to actually create the labels and place them above each of the bars. Any help in adding these would be greatly appreciated!

到目前为止我尝试过的事情:

根据另一篇StackOverflow帖子,我看到了以下几行的命令

Based on another StackOverflow post, I saw a command along these lines

for b = 1:nBins
    text(bins(b),counts(b)*2,num2str(a(b==binIdx,1)),'VerticalAlignment','top')
end

我想到可能在循环内使用text命令在每个小节上方放置一个标签,但是当我尝试将上面的text命令修改为我拥有的数据时,我看不到我的情节上的标签.

I get the idea that I probably use the text command inside a loop to place a label above each bar, but when I attempted to modify the text command above to the data I had, I could not see the labels on my plot.

推荐答案

您确实可以将示例与 text ,但有一些改进.

You can indeed use the example with text, but with a slight improvement.

text(x, y, ' a string')将文本字符串放在图形上点(x,y)的位置.在您的示例中,x-coordiante可以(条形图的中心),但每个y-coordiante的高度是相应条形图的两倍.这可能会将文本字符串放置在图形边界之外.

text(x, y, ' a string') puts the text string in the location of point (x, y) on the graph. In your example, the x-coordiantes are OK (the centers of the bars) but each y-coordiante is at twice the height of the corresponding bar. This may get the text string placed outside the boundaries of the graph.

我建议您首先按照以下方式设置y轴,以便为新文本标签留出一些余地:

What I suggest you to do first is to set the y-axis in the following manner so that you have some extra room for the new text labels:

ylim([0, max(counts) * 1.2]);  %# The 1.2 factor is just an example

然后使用问题中的示例代码,如下所示:

And then you use the example code from your question, like so:

A = fix(10 * rand(30, 1)) + 1; %# Randomize 30 samples between 1 and 10
[counts, bins] = hist(A);      %# Compute the histogram
figure, bar(bins, counts),     %# Plot the histogram bars
ylim([0, max(counts) * 1.2]);  %# Resize the y-axis

%# Add a text string above each bin
for i = 1:numel(bins)
    text(bins(i) - 0.2, counts(i) + 0.4, ['y = ', num2str(counts(i))], 'VerticalAlignment', 'top', 'FontSize', 8)
end

这是您应该得到的:

在这里,每个标签都位于相应条形y轴上方0.4个刻度线处,与条形中心(x轴)的偏移量为-0.2个刻度线.

Here each label is placed 0.4 ticks above the corresponding bar y-axis), at a -0.2 tick offset from the center of the bar (x-axis).

请注意,我还将字体大小减小到8,以使每个标签都与每个条形的宽度很好地匹配. 当然,您可以使用text的不同属性来根据自己的喜好调整标签.

Notice that I've also decreased the font size to 8 so that each label fits nicely with the width of each bar. You can, of course, play with the different properties of text to align your labels to your liking.

这篇关于如何在MATLAB图的顶部添加(值的)标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆