Python anotation arrow direction -
i trying annotate scatter plot in python 2.7, matplotlib. here plot code:
import pandas pd import numpy np import matplotlib.pyplot plt df = pd.dataframe(np.random.rand(5,3), columns=list('abc')) df.insert(0,'annotation_text',['abc','def','ghi','jkl','mnop']) q = 2 pqr = 1 # scatter plot: x = df['a'] y = df.iloc[:,q] plt.scatter(x, y, marker='o', label = df.columns.tolist()[q]) # plot annotation: plt.annotate(df.iloc[pqr,0]+', (%.2f, %.2f)' % (x.ix[pqr],y.ix[pqr]), xy=(x.ix[pqr], y.ix[pqr]), xycoords='data', xytext = (x.ix[pqr], y.ix[pqr]), textcoords='offset points', arrowprops=dict(arrowstyle='-|>')) # axes title/legend: plt.xlabel('xlabel', fontsize=18) plt.ylabel('ylabel', fontsize=16) plt.legend(scatterpoints = 1) plt.show()
as can see, main line line starting plt.annotate(df.iloc[pqr,0]+', (%...............
.
i think main problem part of plt.annotate()
line: xytext = (x.ix[pqr], y.ix[pqr]), textcoords='offset points', arrowprops=dict(arrowstyle='-|>')
. here, xytext = (x.ix[pqr], y.ix[pqr])
tuple of x , y co-ordinates of data point annotated. unfortunately, placing annotation right @ data point, not want. want leave blank space between data point , annotation text.
also, having problem arrow , annotation line producing. see below.
problems:
- currently, annotation text overlapping arrow. annotation text close data point. don't want close.
- also arrow pointing right left. don't think asked plot arrow right-to-left, don't know why plotting arrow in direction.
is there way control text annotation there no overlap data point? also, how change arrow direction right-to-left either a) best
direction or b) left-to-right?
in plt.annotate(...)
, xy
gives position of data want point @ (one end of arrow) , xytext
gives position of text. in code, overlapping because specified same positions xy
, xytext
. try instead (for example):
plt.annotate(df.iloc[pqr,0]+', (%.2f, %.2f)' % (x.ix[pqr], y.ix[pqr]), xy=(x.ix[pqr], y.ix[pqr]), xycoords='data', xytext=(x.ix[pqr], y.ix[pqr]+0.3), arrowprops=dict(arrowstyle='-|>'), horizontalalignment='center')
the arrow default pointing text data point. if want change direction of arrow, can use arrowprops=dict(arrowstyle='<|-')
Comments
Post a Comment