I'm trying to position text using the .text() method in matplotlib. My x-axis is formatted as datetime64. It works as advertised, but I nevertheless get an error as shown below.
My code:
fix, ax = plt.subplots()
ax.plot(x,y)
ax.text('2014-11-01', 82, 'Text goes here', fontsize=26, weight='bold', alpha=.8)
plt.text('2014-11-01', 77, s= 'Subtitle text goes here', fontsize=18)
Which returns a plot as expected: plot with text
In addition, an error is raised:
TypeError: must be real number, not str
I'm quite confused by this. I also tried passing in a number through matplotlib's dates.date2num(t) method, but that wasn't successful.
You need to pass to .text() the coordinates before the actual text you want to insert. For example:
>>> text(0.5, 0.5,'matplotlib', horizontalalignment='center',
... verticalalignment='center',
... transform=ax.transAxes)
The error you're getting is caused by passing a string ('2014-11-01') where the method expects a number.
See the docs.
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