Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can indeed use the example with <a href="http://www.mathworks.com/help/matlab/ref/text.html" rel="nofollow noreferrer"><code>text</code></a>, but with a slight improvement.</p> <p><code>text(x, y, ' a string')</code> 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.</p> <p>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:</p> <pre><code>ylim([0, max(counts) * 1.2]); %# The 1.2 factor is just an example </code></pre> <p>And then you use the example code from your question, like so:</p> <pre><code>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 </code></pre> <p>This is what you should get:</p> <p><img src="https://i.stack.imgur.com/uAbhg.jpg" alt="histogram"></p> <p>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).</p> <p>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 <code>text</code> to align your labels to your liking.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload