python – 如何使用Matplotlib可视化连接矩阵?

前端之家收集整理的这篇文章主要介绍了python – 如何使用Matplotlib可视化连接矩阵?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我试图想象一个双向集合问题的连接矩阵.我如何以最佳方式实现这一目标?

我已经开始使用图形程序了:

圆形描述了红色和蓝色之间的某种连接,而另一个则描述了方形.红色和蓝色方块都会有一些文字.

然而,用matplotlib生成这个grafic会更好,因为我想在附带数据的情况下动态生成它.我该怎么做呢?
我的数据看起来有点像这样:

数据:

  1. name_blue name_red Connection Type
  2. bluepart1 redpart1 1
  3. bluepart1 redpart2 1
  4. bluepart1 redpart3 1
  5. bluepart3 redpart2 2
  6. bluepart4 redpart2 2
  7. ...

等等.我想将名称标签写入蓝色/红色方块,以便用户知道哪个是哪个.

后续问题:
如何通过部分标记为蓝色/红色的节点生成图表?有点像这样:

但是节点反映了它们的二分性质.我对此仍然有点暗,主要是因为我不知道如何使用matplotlib解决这个问题.我希望有一些关于如何可视化的好建议,也许是一个向我展示方式的示例实现.

最佳答案
如何使用这样的颜色边缘进行二分表示?

以下是生成图像的代码.

  1. import matplotlib.pyplot as plt
  2. def addconnection(i,j,c):
  3. return [((-1,1),(i-1,j-1),c)]
  4. def drawnodes(s,i):
  5. global ax
  6. if(i==1):
  7. color='r'
  8. posx=1
  9. else:
  10. color='b'
  11. posx=-1
  12. posy=0
  13. for n in s:
  14. plt.gca().add_patch( plt.Circle((posx,posy),radius=0.05,fc=color))
  15. if posx==1:
  16. ax.annotate(n,xy=(posx,posy+0.1))
  17. else:
  18. ax.annotate(n,xy=(posx-len(n)*0.1,posy+0.1))
  19. posy+=1
  20. ax=plt.figure().add_subplot(111)
  21. set1=['Man1','Man2','Man3','Man4']
  22. set2=['Woman1','Woman2','Woman3','Woman4','Woman5']
  23. plt.axis([-2,2,-1,max(len(set1),len(set2))+1])
  24. frame=plt.gca()
  25. frame.axes.get_xaxis().set_ticks([])
  26. frame.axes.get_yaxis().set_ticks([])
  27. drawnodes(set1,1)
  28. drawnodes(set2,2)
  29. connections=[]
  30. connections+=addconnection(1,'g')
  31. connections+=addconnection(1,3,'y')
  32. connections+=addconnection(1,4,'g')
  33. connections+=addconnection(2,1,'g')
  34. connections+=addconnection(4,'y')
  35. connections+=addconnection(4,'g')
  36. connections+=addconnection(5,'y')
  37. for c in connections:
  38. plt.plot(c[0],c[1],c[2])
  39. plt.show()

得到像你在yEd画的东西

  1. import matplotlib.pyplot as plt
  2. COLOR1='r'
  3. COLOR2='b'
  4. def addconnection(i,c):
  5. if(c==1):
  6. plt.gca().add_patch( plt.Rectangle((j-0.1,-i-0.1),0.2,fc='y'))
  7. if(c==2):
  8. plt.gca().add_patch( plt.Circle((j,-i),radius=0.1,fc='y'))
  9. def drawnodes(s,i):
  10. global ax
  11. if(i==1):
  12. color=COLOR1
  13. vx=1
  14. vy=0
  15. else:
  16. color=COLOR2
  17. vx=0
  18. vy=1
  19. step=1
  20. for n in s:
  21. posx=step*vx
  22. posy=step*vy
  23. plt.gca().add_patch( plt.Circle((posx,-posy),fc=color))
  24. ax.annotate(n,-posy+0.15))
  25. step+=1
  26. f=open('input.txt')
  27. t=f.readlines()
  28. t=map(lambda x: x.replace('(',' ').replace(')',' ').split(':'),t)
  29. set1=set([])
  30. set2=set([])
  31. for x in t:
  32. s=x[1].split()
  33. set1.add(s[0])
  34. set2.add(s[1])
  35. set1=list(set1)
  36. set2=list(set2)
  37. dic={}
  38. for e in zip(set1,xrange(1,len(set1)+1)): dic[(e[0],1)]=e[1]
  39. for e in zip(set2,len(set2)+1)): dic[(e[0],2)]=e[1]
  40. ax=plt.figure(figsize=(max(len(set1),len(set2))+1,len(set2))+1)).add_subplot(111)
  41. plt.axis([-1,-max(len(set1),len(set2))-1,1])
  42. frame=plt.gca()
  43. frame.axes.get_xaxis().set_ticks([])
  44. frame.axes.get_yaxis().set_ticks([])
  45. drawnodes(set1,2)
  46. for x in t:
  47. s=x[1].split()
  48. addconnection(dic[(s[0],1)],dic[(s[1],2)],int(x[2]))
  49. plt.show()

猜你在找的Python相关文章