matplotlib Python中的文本匹配可视化

72qzrwbm  于 2023-03-03  发布在  Python
关注(0)|答案(1)|浏览(137)

我怎样才能做出这样的图像:

我尝试过的东西:Matplotlib,Seaborn,Plotly,但它们都是用于绘制数据的,并不像给定链接列表(例如matchings = [('The', 'The'), ('over', 'above'), ('face', 'wall')])那样进行可视化。

,其在美学上不如上述示例令人愉悦。

qvk1mo1f

qvk1mo1f1#

不确定是否有matplotlib函数可以直接执行此操作,但您可以使用text boxes在几行中创建一个。

import matplotlib.pyplot as plt

def pairings(col_l,col_r,links):
  fig=plt.figure(figsize=(12,10))

  #Create vertical spacing and text boxes
  step_l=1./len(col_l)
  step_r=1./len(col_r)
  t_l =[plt.text(0.25, 0.9-step_l*i,str(l),ha='center',va='center',bbox=dict(boxstyle="round",fc='lightsteelblue'),size=30) for i,l in enumerate(col_l)] #left column
  t_r =[plt.text(0.75, 0.9-step_r*i,str(r),ha='center',va='center', bbox=dict(boxstyle="round",fc='bisque'),size=30) for i,r in enumerate(col_r)] #right column
  
  #create links
  [plt.plot([0.25,0.75],[0.9-step_l*i,0.9-step_r*v],color='slateblue',lw=5) for i,v in enumerate(links) if v!=-1]
  
  #optimize layout
  plt.xlim([0,1])
  plt.ylim([0,1])
  plt.xticks([])
  plt.yticks([])
  plt.show()

col_l是左列中所有文本的列表。col_r与右列相同。links为左列的每个元素显示与之链接的右列元素的索引。-1表示没有链接
例如:

pairings(['The','fox','jumps','over','the','fence'],['The','dog','above','the','wall','jumps'],[0,-1,5,2,3,4])

退货:

相关问题