xytext details in Matplotlibs Annotate
Asked Answered
B

1

6

With the following code I am plotting a candlestick graph and also make use of annotations. I have played arround until I found the right positions for the text, but I still don't understand what the figures xytext=(-15, -27) and xytext=(-17, 20) have to do with their current position.

It is very strange to me. could somebody please explain it to me? Many thanks in advance!
This is what my graph looks like and below is the code:

enter image description here

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.finance import candlestick_ohlc
from matplotlib import style
import pandas_datareader
import datetime as dt

style.use('classic')
start = dt.datetime(2017,1,1)
end = dt.datetime(2017,4,1)

def graph(stock):
    ax1 = plt.subplot2grid((1,1), (0,0))
    stock_data = pandas_datareader.DataReader(name=stock, data_source='google', start=start, end=end)
    stock_data.reset_index(inplace=True)
    stock_data['Date'] = stock_data['Date'].map(mdates.date2num)
    candlestick_ohlc(ax1, stock_data.values, width=0.5, colorup='g', colordown='r')

    ax1.annotate('Long',
                 xy=(stock_data['Date'][10], stock_data['Low'][10]),
                 xytext=(-15, -27),
                 textcoords='offset points',
                 arrowprops=dict(facecolor='grey', color='grey'))

    ax1.annotate('Short',
                 xy=(stock_data['Date'][28], stock_data['High'][28]),
                 xytext=(-17, 20),
                 textcoords='offset points',
                 arrowprops=dict(facecolor='grey', color='grey'))

    ax1.annotate('Long',
                 xy=(stock_data['Date'][42], stock_data['Low'][42]),
                 xytext=(-15, -27),
                 textcoords='offset points',
                 arrowprops=dict(facecolor='grey', color='grey'))

    ax1.annotate('Short',
                 xy=(stock_data['Date'][48], stock_data['High'][48]),
                 xytext=(-17, 20),
                 textcoords='offset points',
                 arrowprops=dict(facecolor='grey', color='grey'))

    plt.show()

graph('TSLA')
Bougainville answered 5/7, 2017 at 13:44 Comment(1)
The answer should become more clear if you play around with the values. For example, if you change (-17, -20) to say (-17, -10) or (-17, -30), what do you observe? That should help you answer yourself.Partition
M
5

You chose to have the text coordinates in offset points. E.g. xytext=(-17, 20) places the text at 17 points to the left and 20 points to the top from the point which you annotate.

The coordinates may be more obvious when changing the horizontalalignment to "center" in the annotation. annotate( ... , ha="center").

You can then get the result by setting the x coordinate to 0.

ax1.annotate('Long', xy=(stock_data['Date'][10], stock_data['Low'][10]),
                 xytext=(0, -27),
                 textcoords='offset points', ha="center",
                 arrowprops=dict(facecolor='grey', color='grey'))
Monoplane answered 5/7, 2017 at 17:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.