X-axis in Matplotlib
I've written a simple program in Python using Matplotlib to draw lines in a coordinate system. I want X-Axis description to be "2006, 2007, 2008" but instead is shows as "0, 0.5, 1, 1.5, 2.0 +2.006e3"
code is here:
import matplotlib.pyplot
import numpy as np
fig = matplotlib.pyplot.figure()
a=[2006,2007,2008]
b=[-1,3,5]
matplotlib.pyplot.plot(a, b, 'go-', label='line 1', linewidth=2)
matplotlib.pyplot.axis([2开发者_运维技巧006, 2008, -1, 5])
matplotlib.pyplot.show()
Instead of
plt.axis([2006, 2008, -1, 5])
use plt.xticks:
plt.xticks(a, map(str,a))
PS: Its convenient to import matplotlib.pyplot
with
import matplotlib.pyplot as plt
as this cuts down on the typing.
精彩评论