擅长:python、mysql、java
<p>对于<strong>vanilla matplotlib</strong>解决方案,请将<code>xticklabels</code>与<code>xticks</code>一起使用:</p>
<pre><code>import random
import matplotlib.pyplot as plt
NUM_FAMILIES = 10
# set the random seed (for reproducibility)
random.seed(42)
# setup the plot
fig, ax = plt.subplots()
# generate some random data
x = [random.randint(0, 5) for x in range(NUM_FAMILIES)]
# create the histogram
ax.hist(x, align='left') # `align='left'` is used to center the labels
# now, define the ticks (i.e. locations where the labels will be plotted)
xticks = [i for i in range(NUM_FAMILIES)]
# also define the labels we'll use (note this MUST have the same size as `xticks`!)
xtick_labels = ['Family-%d' % (f+1) for f in range(NUM_FAMILIES)]
# add the ticks and labels to the plot
ax.set_xticks(xticks)
ax.set_xticklabels(xtick_labels)
plt.show()
</code></pre>
<p>结果是:</p>
<p><a href="https://i.stack.imgur.com/LkEYn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LkEYn.png" alt="histogram plot"/></a></p>