Specify what Y labels to use
<p>Using python and Matplotlib, I am trying to explicitly control what Y labels are shown on Y axis:</p>

<pre><code>def plot_sample_top(sample, chrom):
    ax = fig.add_subplot(23, 1, subplot_coord[sample])
    ax.set_xlim([1, chrom_lengths[chrom]])
    ax.set_ylim([-10, 10])

    # scatter
    ax.scatter(df_strain['POS'], df_strain["SD"], color='black', label="&lt; 1 SD")
</code></pre>

<p>I need the bounds of Y axis to be -10 and 10, and I want only the numbers -10, 0, and 10 to be shown as Y axis labels.  with current code it shows -10, -5, 0, 5, 10  and is too squeezed together</p>

<p>You will want to use <a href="http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_yticks" rel="nofollow">axes.set_yticks</a> and/or <a href="http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_yticklabels" rel="nofollow">axes.set_yticklabels</a>.</p>

<p>To set the ticks and labels as you specify, you would do:</p>

<pre><code>ax.set_yticks([-10, 0, 10])
</code></pre>

<p>This will eliminate all of the other ticks. If you want the ticks to remain, but the labels to disappear (e.g. you want to have grid lines on -9, -8, -7 etc. but don't want the label), you could do:</p>

<pre><code>ax.set_yticks(range(-10,11)) # to make sure they are what you think they are
labels = [-10,'','','','','','','','','',0,'','','','','','','','','',10]
ax.set_yticklabels(labels)
</code></pre>

<p>If you wanted to make the labels a little more elegantly, you could do:</p>

<pre><code>labels = [y if y in [-10,0,10] else '' for y in xrange(-10,11)]
</code></pre>

python matplotlib