user308827 user308827 - 4 months ago 10
Python Question

Explicitly labeling end points in matplotlib graph

In the matplotlib graph above, the x-axis ranges from 850 - 2015. I would like them to be explicitly labeled. Is there a way to force matplotlib to do that?

I am doing this:

ax.set_xlim([min(xvar), max(xvar)])
, here xvar is the x-axis array ranging from 850 - 2015

Answer

try set_xticks and set_xticklabels to set the location and content of labels.

import matplotlib.pyplot as plt
a = plt.subplot(1,1,1)
a.plot([1,2,3,4,5,6,7,8,9,10])
a.set_xlim(1,10.5)
a.set_xticks([0, 5, 10, 10.5])
a.set_xticklabels([0, 5, 10, 10.5])

enter image description here