Matplotlibs Annotate 中的 xytext 详细信息 [英] xytext details in Matplotlibs Annotate
问题描述
使用以下代码,我正在绘制烛台图并使用注释.我一直玩到找到文本的正确位置,但我仍然不明白 xytext=(-15, -27)
和 xytext=(-17,20)
与他们目前的位置有关.
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:
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')
推荐答案
您选择在偏移点中设置文本坐标.例如.xytext=(-17, 20)
将文本放置在 17
点的左侧和 20
点从您注释的点开始的顶部.
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.
将注释中的horizontalalignment
改为"center"
后,坐标可能会更明显.annotate( ... , ha="center")
.
The coordinates may be more obvious when changing the horizontalalignment
to "center"
in the annotation. annotate( ... , ha="center")
.
然后您可以通过将 x 坐标设置为 0
来获得结果.
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'))
这篇关于Matplotlibs Annotate 中的 xytext 详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!