Matplotlib:可以绘制从一组轴到另一组轴的线吗? [英] Matplotlib: Can a plot a line from one set of axes to another?

查看:56
本文介绍了Matplotlib:可以绘制从一组轴到另一组轴的线吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在 [100,100] 处绘制标记x",然后在 [20%, 30%](不同的轴,相同的图)处绘制o"并用一条线将它们连接起来.我可以在相同的轴上(使用相同的单位)做类似的事情,一次调用绘制线条,另一次调用绘制x",最后一次调用绘制o".

I wish to plot marker "x" at say [100,100] and then plot "o" at [20%, 30%] (different axes, same plot) and connect them with a line. I can do something similar on the same axes (with the same units) with one call to plot the line, another call to plot the "x" and a final call to plot the "o".

ax.plot(x,y,"-")
ax.scatter(x[0], y[0], marker='x')
ax.scatter(x[1], y[1], marker='o')

但是,如何获得从一组轴到另一根轴的线?

However, how can I get the line to go from one set of axes to the other?

推荐答案

可以使用annotate来画单条线:

ax1 = plt.subplot(121)
ax2 = plt.subplot(122)

x = [[1, 2], [3,4]]
y = [[5, 6], [6,4]]

ax1.scatter(x[0], y[0])
ax2.scatter(x[1], y[1])


ax1.annotate('', xy=(x[0][0], y[0][0]), xytext=(x[1][0], y[1][0]), xycoords=ax1.transData, 
         textcoords=ax2.transData, 
         arrowprops=dict(facecolor='black', arrowstyle='-',, clip_on=False))
ax2.annotate('', xy=(x[0][0], y[0][0]), xytext=(x[1][0], y[1][0]), xycoords=ax1.transData, 
         textcoords=ax2.transData, 
         arrowprops=dict(facecolor='black', arrowstyle='-'))

产生以下结果:

这篇关于Matplotlib:可以绘制从一组轴到另一组轴的线吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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