2017-07-05 4 views
1

次のコードでは、私は燭台のグラフをプロットしており、アノテーションも使用しています。私はテキストの正しいポジションを見つけるまでarroundをプレイしましたが、数字xytext=(-15, -27)xytext=(-17, 20)が現在の位置と関係していることはまだ分かりません。Matplotlibsのxytextの詳細Annotate

私は非常に奇妙です。誰かが私にそれを説明できますか?事前に多くの感謝!
は、これは私のグラフのように見えるものであり、以下のコードです:

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') 
+0

値を使いこなすと答えがより明確になるはずです。たとえば、(-17、-20)を(-17、-10)または(-17、-30)と変更すると、あなたは何を観察しますか?それはあなた自身に答えるのを助けるはずです。 –

答えて

1

あなたはテキストがオフセットポイントの座標持っていることを選びました。例えば。 xytext=(-17, 20)は、テキストを左に17点、注釈を付ける点から上に向かって20点を置きます。

アノテーションのhorizontalalignment"center"に変更すると、座標がより明確になることがあります。 annotate(... , ha="center")

x座標を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'))