I have a dataset consisting of date-value pairs. I want to plot them in a bar graph with the specific dates in the x-axis.
My problem is that matplotlib
distributes the xticks
over the entire date range; and also plots the data using points.
The dates are all datetime
objects. Here's a sample of the dataset:
data = [(DT.datetime.strptime('2010-02-05', "%Y-%m-%d"), 123),
(DT.datetime.strptime('2010-02-19', "%Y-%m-%d"), 678),
(DT.datetime.strptime('2010-03-05', "%Y-%m-%d"), 987),
(DT.datetime.strptime('2010-03-19', "%Y-%m-%d"), 345)]
Here is a runnable code sample using pyplot
import datetime as DT
from matplotlib import pyplot as plt
data = [(DT.datetime.strptime('2010-02-05', "%Y-%m-%d"), 123),
(DT.datetime.strptime('2010-02-19', "%Y-%m-%d"), 678),
(DT.datetime.strptime('2010-03-05', "%Y-%m-%d"), 987),
(DT.datetime.strptime('2010-03-19', "%Y-%m-%d"), 345)]
x = [date for (date, value) in data]
y = [value for (date, value) in data]
fig = plt.figure()
graph = fig.add_subplot(111)
graph.plot_date(x,y)
plt.show()
QUESTION SUMMARY:
My situation is more like I have an Axes
instance ready (referenced by graph
in the code above) and I want to do the following:
- Make the
xticks
correspond to the exact date values. I have heard of matplotlib.dates.DateLocator
but I have no idea how create one and then associate it with a specific Axes
object.
- Get tighter control over the type of graph being used (bar, line, points, etc.)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…