title: Customized x-axis matplotlib
body: <p>I just want to use a list of decimal years to assign the labels and ticks for a customized x-axis:</p>
  <pre><code>labels = [1992.71, 1991.67, 1990.5, 1989.54, 1988.5, 1987.5, .., 1700.5]
  </code></pre>
  <p>IMPORTANT: The interval between 2 consecutive values in the above list is not constant, I only care about steps here, so every position in the list should be a tick or a bin.</p>
  <p>The problem is that if I use all the labels in the list, the graph gets too dense. By the way, inside my graph lies a PNG image, not a plot.</p>
  <p>So, I have decided (maybe wrongly) to generate a second list with only the values I need (this time using strings):</p>
  <pre><code>labels2 = ['1992.71','','','','','1987.50', etc.]
  </code></pre>
  <p>Ideally, I would like the x-axis to have either this configuration:</p>
  <p><a href="https://i.stack.imgur.com/no2Rj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/no2Rj.png" alt="selected labels in the right bin number and no ticks" /></a></p>
  <p>or this one:</p>
  <p><a href="https://i.stack.imgur.com/P5CWN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/P5CWN.png" alt="selected labels and associated ticks in the right bin number, no other ticks" /></a></p>
  <p>or this one:</p>
  <p><a href="https://i.stack.imgur.com/IMjTI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IMjTI.png" alt="selected labels and associated ticks at the start of the bin; left" /></a></p>
  <p>So far I have been playing with these functions:</p>
  <pre><code>ax.set_xticklabels
  plt.locator_params
  ax.tick_params
  plt.xticks
  ax.xaxis.set_ticks
  ax.xaxis.set_major_formatter
  ...
  </code></pre>
  <p>with no success.</p>
  <p>This is the closest I have been:</p>
  <p><a href="https://i.stack.imgur.com/JRJCn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JRJCn.png" alt="enter image description here" /></a></p>
  <p>But the sequence of labels gets CUT in the right hand-side of the x-axis and never reaches the end of the list, so it finishes half-way through the list (which should end in 1700.5):</p>
  <p><a href="https://i.stack.imgur.com/uYkwr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uYkwr.png" alt="enter image description here" /></a></p>
  <p>Also, I just want to display the ticks corresponding to the selected labels and not all of them.</p>
  <p>this is my code:</p>
  <pre><code>plt.locator_params(axis='x', nbins=len(labels2)-1)
  ax.set_xticklabels(labels2, rotation=90 )
  ax.tick_params(axis='x', which='major', length=4, labelsize=2)
  </code></pre>
  <p>In essence, currently I cannot reproduced what I want to,</p>
  <p>any ideas?</p>