在matplotlib中创建一个参数箭头,返回并注释图中两点之间的差异。

问题描述 投票:0回答:1

我想创造这样的情节enter image description here

我的重点是根据输入的数据,自动绘制一个箭头和他的长度。

我被困在如何创建一个函数,自动获得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()

我也看过了

这个问题"在matplotlib中为参数图添加箭头。"

这个问题"是否可以将matplotlib注解锚定在x轴的数据坐标上,而在y轴的相对位置上?"

文件的这一章 使用复杂的坐标标注

但我还是有问题

python matplotlib data-visualization
1个回答
1
投票

我可能简化的太多了,但是你为什么需要变换呢?我对你的问题的理解是这样的,你只需要指定你要绘制的箭头的索引(哪个月 在此)

def draw_arrow(x,y,yy, shift=0, which_month=-2):
    x_end=x.iloc[which_month]+shift
    y_end=y.iloc[which_month]
    yy_end=yy.iloc[which_month]
    half=(y_end+yy_end)/2
    diff=str(abs(y_end-yy_end))
    annotation=plt.annotate('', xy  = (x_end, y_end),  
    xycoords = 'data', \
    xytext = (x_end, yy_end), textcoords = 'data', fontsize = 7, \
    color = '#303030', arrowprops=dict(edgecolor='black', arrowstyle = '<->', shrinkA = 0, shrinkB = 0),)
    text=plt.text(x_end*1.01,half,diff)
    return annotation,text

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.