问题是所有给出的解决方案Matplotlib 绘图:删除轴、图例和空格实际上是为了与imshow
.
所以,以下显然有效
import matplotlib.pyplot as plt
fig = plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.set_axis_off()
im = ax.imshow([[2,3,4,1], [2,4,4,2]], origin="lower", extent=[1,4,2,8])
ax.plot([1,2,3,4], [2,3,4,8], lw=5)
ax.set_aspect('auto')
plt.show()
并产生
![enter image description here](https://i.stack.imgur.com/TDYsr.png)
但在这里,你正在使用scatter
。添加散点图
import matplotlib.pyplot as plt
fig = plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.set_axis_off()
im = ax.imshow([[2,3,4,1], [2,4,4,2]], origin="lower", extent=[1,4,2,8])
ax.plot([1,2,3,4], [2,3,4,8], lw=5)
ax.scatter([2,3,4,1], [2,3,4,8], c="r", s=2500)
ax.set_aspect('auto')
plt.show()
产生
![enter image description here](https://i.stack.imgur.com/HBywU.png)
Scatter
具有特殊性,matplotlib 尝试默认使所有点可见,这意味着轴限制的设置使得所有散点作为一个整体可见。
为了克服这个问题,我们需要专门设置轴限制:
import matplotlib.pyplot as plt
fig = plt.figure()
ax=fig.add_axes([0,0,1,1])
ax.set_axis_off()
im = ax.imshow([[2,3,4,1], [2,4,4,2]], origin="lower", extent=[1,4,2,8])
ax.plot([1,2,3,4], [2,3,4,8], lw=5)
ax.scatter([2,3,4,1], [2,3,4,8], c="r", s=2500)
ax.set_xlim([1,4])
ax.set_ylim([2,8])
ax.set_aspect('auto')
plt.show()
这样我们就能得到想要的行为。
![enter image description here](https://i.stack.imgur.com/fqh2e.png)