我的重点是根据输入数据自动绘制箭头及其长度
我一直在研究如何创建一个函数,自动获取x1、y1、y2坐标并返回:
大概是这样的:
def draw_arrow(x,y,yy,shift=0):
tform = blended_transform_factory(ax.transAxes, ax.transAxes)
x_end=x.iloc[-1]+shift
y_end=y.iloc[-1]
yy_end=yy.iloc[-1]
half=(y_end+yy_end)/2
diff=str(abs(y_end-yy_end))
annotation=plt.annotate('', xy = (x_end, y_end), xycoords = tform, \
xytext = (x_end, yy_end), textcoords = tform, fontsize = 7, \
color = '#303030', arrowprops=dict(edgecolor='black', arrowstyle = '<->', shrinkA = 0, shrinkB = 0),)
text=plt.text(x_end*1.10,half,str())
return annotation,text
这是我为这个问题创建的代码:
retention_dict= {'Month':[0,1,2,3,4,5,6],
'Ret_30':[1000, 1300, 1390, 1417, 1425, 1428, 1428],
'Ret_40':[1000, 1400, 1560, 1624, 1650, 1660, 1664],
'Ret_50':[1000, 1500, 1750, 1875, 1938, 1969, 1984]}
ret_df=pd.DataFrame(retention_dict)
def draw_arrow(x,y,yy,shift=0):
tform = blended_transform_factory(ax.transAxes, ax.transAxes)
x_end=x.iloc[-1]+shift
y_end=y.iloc[-1]
yy_end=yy.iloc[-1]
half=(y_end+yy_end)/2
diff=str(abs(y_end-yy_end))
annotation=plt.annotate('', xy = (x_end, y_end), xycoords = tform, \
xytext = (x_end, yy_end), textcoords = tform, fontsize = 7, \
color = '#303030', arrowprops=dict(edgecolor='black', arrowstyle = '<->', shrinkA = 0, shrinkB = 0),)
text=plt.text(x_end*1.10,half,str())
return annotation,text
fig, ax =plt.subplots(facecolor='lightgray')
formatter = ticker.FormatStrFormatter('%1.0f')
ax.yaxis.set_major_formatter(formatter)
plt.grid(True)
figure_title = "Retention Rate tells us \n % of customers that did not shop again \n after the first puchase"
figure_expl = "Hp. Every 10$ we invest in marketing(FB ads/Google) \n we acquire one new customer"
ax=plt.plot(ret_df['Month'], ret_df['Ret_30'],
ret_df['Month'], ret_df['Ret_40'],
ret_df['Month'], ret_df['Ret_50']
)
plt.text(3,2050, figure_title,
horizontalalignment='center',
fontsize=10,
)
plt.text(3,970, figure_expl,
horizontalalignment='center',
fontsize=10,
)
#draw_arrow(ret_df['Month'],ret_df['Ret_30'],ret_df['Ret_40'])
plt.title('Retention Rate ', pad=50)
plt.xlabel('Months')
plt.ylabel('Customers')
plt.show()
我还看到:
这个问题“Adding arrows to parametric plots in matplotlib”
本章文件using complex coordinatewith annotation
但我还是有麻烦
我可能简化得太多了,但为什么需要变换呢? 我对你的问题的解释是这样的,你只需要指定你想要绘制的箭头的索引(在本例中是哪个月)
相关问题 更多 >
编程相关推荐