【发布时间】:2019-09-09 17:29:41
【问题描述】:
当我试图画一个箭头时,指向端被其他轴挡住了,你知道如何使那个部分可见吗?
如果您可以在我这里的示例代码之上进行编辑,那就太好了。
from matplotlib.patches import ConnectionPatch
import matplotlib.pyplot as plt
import numpy as np
grid_data = np.array([[[-0.1779874 , -0.90335705, -0.31157705, 0.77770067],
[ 0.93698288, 0.79215241, 0.10155888, 0.96101718],
[ 0.72994894, -0.83939131, 0.24713443, 0.74839211],
[ 0.10039462, -0.95778299, 0.43554077, 0.61927077]],
[[ 0.52294259, -0.0247383 , 0.23717517, -0.0857769 ],
[-0.43539246, 0.28503173, -0.39443502, -0.1478289 ],
[-0.2327904 , -0.08339054, 0.33072907, 0.74634504],
[-0.524284 , -0.72919194, -0.61543159, 0.17086563]]])
fig, axes = plt.subplots(nrows = 2, ncols = 2, figsize = (2.6, 2.8))
for i in range(2):
c = axes[0,i].pcolormesh(grid_data[i], cmap = 'RdBu_r', vmin = -1., vmax = 1.)
xyA = (0.5,0)
xyB = (0.5,.5)
coordsA = "axes fraction"
coordsB = "axes fraction"
con = ConnectionPatch(xyA = xyA, xyB = xyB, coordsA=coordsA, coordsB=coordsB,
axesA=axes[0,0], axesB=axes[0,1],
arrowstyle="->", shrinkB=5, linewidth = 5, edgecolor='b', clip_on=False)
axes[0,0].add_artist(con)
cb_ax = fig.add_axes([0.91, 0.13, 0.01, 0.75])
fig.colorbar(c, cax = cb_ax)
plt.draw()
plt.show()
【问题讨论】:
-
您需要将 ConnectionPatch 添加到顶部坐标区,在本例中为
axes[0,1].add_artist(con)。从下一个 matplotlib 版本开始,您还可以通过fig.add_artist(con)将其添加到图中。 -
正确答案适用于 ImportanceOfBeingErnest 和 lw1.at,两种不同的方法取决于您自己更直观的方法。但看起来 ImportanceOfBeingErnest 喜欢将接受的答案提供给更多其他人:P
标签: python python-3.x matplotlib data-visualization