在 Matplotlib 中的两个图之间画线 [英] Drawing lines between two plots in Matplotlib

查看:53
本文介绍了在 Matplotlib 中的两个图之间画线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Matplotlib 绘制两个子图,基本上如下:

subplot(211);显示(一);分散(...,...)子图(212);显示(b);分散(...,...)

我可以在这两个子图之间画线吗?我该怎么做?

解决方案

其他答案的解决方案在许多情况下都不是最优的(因为它们只有在计算点后不对绘图进行更改才有效).

>

更好的解决方案是使用专门设计的

I am drawing two subplots with Matplotlib, essentially following :

subplot(211); imshow(a); scatter(..., ...)
subplot(212); imshow(b); scatter(..., ...)

Can I draw lines between those two subplots? How would I do that?

解决方案

The solution from the other answers are suboptimal in many cases (as they would only work if no changes are made to the plot after calculating the points).

A better solution would use the specially designed ConnectionPatch:

import matplotlib.pyplot as plt
from matplotlib.patches import ConnectionPatch
import numpy as np

fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

x,y = np.random.rand(100),np.random.rand(100)

ax1.plot(x,y,'ko')
ax2.plot(x,y,'ko')

i = 10
xy = (x[i],y[i])
con = ConnectionPatch(xyA=xy, xyB=xy, coordsA="data", coordsB="data",
                      axesA=ax2, axesB=ax1, color="red")
ax2.add_artist(con)

ax1.plot(x[i],y[i],'ro',markersize=10)
ax2.plot(x[i],y[i],'ro',markersize=10)


plt.show()

这篇关于在 Matplotlib 中的两个图之间画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆