I'm trying to plot arbitrary text below the x tick marks in a matplotlib figure (see example figure below). I'm using dates as the x-axis and, for instance, I want to display counts of some variable associated with each date.
In the example below I use the relative positions from 0-1 within the figure for the x-coordinate of where the text should be positioned. However I've just guessed these relative values (using trial and error) and so I would like to know how would one plot text below the x-ticks using the actual positions of the x-data instead of using these 0-1 relative scaling that's the default in figtext?
import numpy as np
import matplotlib.pyplot as plt
import datetime
x = [datetime.datetime(2010, 12, 1, 0, 0),
datetime.datetime(2011, 1, 1, 0, 0),
datetime.datetime(2011, 5, 1, 1, 0)]
y = [4, 9, 2]
fig, ax = plt.subplots()
ax.bar(x, y, width = 20)
xticks = ax.get_xticks()
xtick_rel_position = np.linspace(0.13, 0.81, len(xticks)) # <- these are just guessed
counts = np.random.randint(0, 25, len(xticks))
for i, xpos in enumerate(xtick_rel_position):
plt.figtext(xpos, 0.028, "Below tick\nlabel "+str(i),
size = 6, ha = 'center')
plt.figtext(xpos, 0.005, "Count: "+str(counts[i]),
size = 6, ha = 'center')
# For better aesthetics
ax.yaxis.set_visible(False)
plt.show()
I've tried including transform = ax.transAxes with the actual x coordinates from ax.get_xticks() but this doesn't change anything.
