title: How to label the vertical lines independent of the scale of the plot?
body: <p>My program takes <code>n</code> sets of data and plots their histograms.</p>
  
  <h2>I. How to label the vertical lines independent of the height of the plot?</h2>
  
  <p>A vertical line indicates the most frequent value in a dataset. I want to add a label indicating the value, say 20% from the top. When using <code>matplotlib.pyplot.text()</code> I had to manually assign <code>x</code> and <code>y</code> values. <strong>Depending up on the dataset the text goes way up or way down which I don't want to happen.</strong> </p>
  
  <pre><code>matplot.axvline(most_common_number, linewidth=0.5, color='black')
  matplot.text(most_common_number + 3, 10, str(most_common_number),
      horizontalalignment='center', fontweight='bold', color='black')
  </code></pre>
  
  <p>I also tried setting the <code>label</code> parameter of <code>matplotlib.pyplot.axvline()</code> but it only adds to the <code>legend</code> of the plot.</p>
  
  <p><code>matplot.axvline(most_common_number, linewidth=0.5, color='black', label=str(most_common_number))</code></p>
  
  <p><a href="https://i.stack.imgur.com/KGMyP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KGMyP.png" alt="enter image description here"></a></p>
  
  <p><strong>I wonder if there is a way to use percentages so the text appears <code>n%</code> from the top or use a different method to label the vertical lines. Or am I doing this all wrong?</strong></p>
  
  <h2>II. How to make the ticks on x-axis to be spaced out better on resulting image?</h2>
  
  <p>I want the x-axis ticks to be factors of 16 so I had to override the defaults. This is where the trouble began. When I save the plot to a PNG file, the x-axis looks really messed up.</p>
  
  <p><a href="https://i.stack.imgur.com/3uyNg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3uyNg.png" alt="The ticks on the x-axis need to be spaced out better"></a></p>
  
  <p>But when I use <code>show()</code> it works fine:
  <a href="https://i.stack.imgur.com/x5Giv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/x5Giv.png" alt="enter image description here"></a></p>
  
  <h3>Program Snippet</h3>
  
  <pre><code>kwargs = dict(alpha=0.5, bins=37, range=(0, 304), density=False, stacked=True)
  fig, ax1 = matplot.subplots()
  
  colors = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', 'tab:olive', 'tab:cyan']
  
  count = 0
  '''
  datasets = [('dataset name', ['data'])]
  '''
  for item in datasets:
      dataset = item[1]
      most_common_number = most_common(dataset)
      ax1.hist(dataset, **kwargs, label=item[0], color=colors[count])
      matplot.axvline(most_common_number, linewidth=0.5, color='black')
      matplot.text(most_common_number + 3, 10, str(most_common_number),
          horizontalalignment='center', fontweight='bold', color='black')
      count += 1
  
  #for x-axis
  loc = matplotticker.MultipleLocator(base=16) # this locator puts ticks at regular intervals
  ax1.xaxis.set_major_locator(loc)
  #for y-axis
  y_vals = ax1.get_yticks()
  ax1.set_yticklabels(['{:3.1f}%'.format(x / len(datasets[0][1]) * 100) for x in y_vals])
  
  #set title
  matplot.gca().set(title='1 vs 2 vs 3')
  #set subtitle
  matplot.suptitle("This is a cool subtitle.", va="bottom", family="overpass")
  
  matplot.legend()
  
  fig = matplot.gcf()
  fig.set_size_inches(16, 9)
  
  matplot.savefig('out.png', format = 'png', dpi=120)
  matplot.show()
  </code></pre>