matplotlib 曲线图未调整到箭头范围

plupiseo  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(73)

例如:How to turn off matplotlib quiver scaling?
当使用matplotlib.pyplots的箭头来绘制箭头时,箭头经常指向图像之外。看起来图只调整到起点(X,Y参数到箭头()),而没有考虑实际箭头的范围。有没有简单的方法来重新缩放轴以包括整个箭头?
我知道plt.xlim(...,...),plt.ylim(...,...),或者Axes.set_xlim / Axes.set_ylim;我想也许有一个全局命令(比如tight layout命令)可以将所有点包含到图的可见部分(可能是一次所有图)?
更新,因为有人对这个问题不满意:试图添加到我链接的示例中,@Mathieu在评论中建议的内容(约束布局)似乎不起作用:

import matplotlib.pyplot as plt
import numpy as np

pts = np.array([[1, 2], [3, 4]])
end_pts = np.array([[2, 4], [6, 8]])
diff = end_pts - pts

plt.quiver(pts[:,0], pts[:,1], diff[:,0], diff[:,1],
           angles='xy', scale_units='xy', scale=1.)

字符串
我们得到一个图像,其中一个箭头指向图像外:x1c 0d1x
启用受约束的布局:

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['figure.constrained_layout.use'] = True

pts = np.array([[1, 2], [3, 4]])
end_pts = np.array([[2, 4], [6, 8]])
diff = end_pts - pts

plt.quiver(pts[:,0], pts[:,1], diff[:,0], diff[:,1],
           angles='xy', scale_units='xy', scale=1.)


这会导致更小的边距,但对轴范围没有影响:

093gszye

093gszye1#

看起来你可以用plt.scatter()添加不可见的端点,这不是一个“全局”选项,但对我来说它也可以完成这项工作(我不需要取max,所以它比ylim/xlim少了一步)。

import matplotlib.pyplot as plt
import numpy as np

pts = np.array([[1, 2], [3, 4]])
end_pts = np.array([[2, 4], [6, 8]])
diff = end_pts - pts

plt.scatter(end_pts[:, 0], end_pts[:, 1], s=0)
plt.quiver(pts[:,0], pts[:,1], diff[:,0], diff[:,1],
           angles='xy', scale_units='xy', scale=1.)

字符串
这将生成以下图像:
x1c 0d1x的数据

相关问题