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:
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. The xticks() function in pyplot module of the Matplotlib library is used to set x-axis values.
To set axes limits use set_xlim() and set_ylim() for x-axis and y-axis respectively. To plot a line chart, use the plot() function of pyplot module. To add a title to the plot, use the title() function. To add a label at the x-axis, use the xlabel() function.
The xticks() and yticks() function takes a list object as argument. The elements in the list denote the positions on corresponding action where ticks will be displayed. This method will mark the data points at the given positions with ticks.
What you're doing is simple enough that it's easiest to just using plot, rather than plot_date. plot_date is great for more complex cases, but setting up what you need can be easily accomplished without it.
e.g., Based on your example above:
import datetime as DT
from matplotlib import pyplot as plt
from matplotlib.dates import date2num
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 = [date2num(date) for (date, value) in data]
y = [value for (date, value) in data]
fig = plt.figure()
graph = fig.add_subplot(111)
# Plot the data as a red line with round markers
graph.plot(x,y,'r-o')
# Set the xtick locations to correspond to just the dates you entered.
graph.set_xticks(x)
# Set the xtick labels to correspond to just the dates you entered.
graph.set_xticklabels(
[date.strftime("%Y-%m-%d") for (date, value) in data]
)
plt.show()
If you'd prefer a bar plot, just use plt.bar()
. To understand how to set the line and marker styles, see plt.plot()
Plot with date labels at marker locations http://www.geology.wisc.edu/~jkington/matplotlib_date_labels.png
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With