I have a list to draw. Is there a way to display parts of x-axis labels, for example x[0], x[10], ...
, but keep the figure the same?
x = [\'alice
One way you can do this is to reduce the number of ticks on the x axis. You can set the ticks using ax.set_xticks()
. Here you can slice the x
list to set a ticks at every 2nd entry using the slice notation [::2]
. Then set the x tick labels using ax.set_xticklabels()
using the same slice when setting the ticks.
For example:
x = ["Ant", "Bob", "Crab", "Donkey", "Elephant", "Fire", "Giant","Hello",
"Igloo", "Jump", "Kellogg","Llama", "More", "Night"]
y = np.random.randint(0,10,14)
fig, (ax1, ax2) = plt.subplots(1,2, figsize=(9,5))
ax1.plot(x,y)
ax1.set_title("Crowded x axis")
ax2.plot(x,y)
ax2.set_xticks(x[::2])
ax2.set_xticklabels(x[::2], rotation=45)
ax2.set_title("Every 2nd ticks on x axis")
plt.show()