如何在子图中的相同轴上添加更多图?
how to add more plots to same axes in subplot?
我想向具有相同轴的特定子图添加多个图。到目前为止,我通过在激活相应的 plt.subplot(xxx)
之后重复调用 plt.[some_plotting_function]
来做到这一点,如下所示:
import matplotlib.pyplot as plt
plt.subplot(121)
plt.plot([0, 1], [0, 1], 'o-')
plt.subplot(122)
plt.plot([0, 1], [0,0], 'o-')
plt.subplot(121)
plt.plot([0, 1], [1, 0], 'o-')
plt.show()
现在我收到警告
MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
听起来相同的代码在未来会有不同的表现。我将来如何才能达到与上面的代码相同的结果?
您仍然可以使用此代码,但重构它以使用返回的轴而不是引用子图,例如:
ax1 = plt.subplot(121)
ax1.plot([0, 1], [0, 1], 'o-')
ax2 = plt.subplot(122)
ax2.plot([0, 1], [0,0], 'o-')
ax1.plot([0, 1], [1, 0], 'o-')
plt.show()
您可以看到我没有引用第一个子图,而是使用第一个子图调用返回的轴。
编辑:这是与评论中提到的方法类似的方法,但它与您的原始工作流程保持接近。
我想向具有相同轴的特定子图添加多个图。到目前为止,我通过在激活相应的 plt.subplot(xxx)
之后重复调用 plt.[some_plotting_function]
来做到这一点,如下所示:
import matplotlib.pyplot as plt
plt.subplot(121)
plt.plot([0, 1], [0, 1], 'o-')
plt.subplot(122)
plt.plot([0, 1], [0,0], 'o-')
plt.subplot(121)
plt.plot([0, 1], [1, 0], 'o-')
plt.show()
现在我收到警告
MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
听起来相同的代码在未来会有不同的表现。我将来如何才能达到与上面的代码相同的结果?
您仍然可以使用此代码,但重构它以使用返回的轴而不是引用子图,例如:
ax1 = plt.subplot(121)
ax1.plot([0, 1], [0, 1], 'o-')
ax2 = plt.subplot(122)
ax2.plot([0, 1], [0,0], 'o-')
ax1.plot([0, 1], [1, 0], 'o-')
plt.show()
您可以看到我没有引用第一个子图,而是使用第一个子图调用返回的轴。
编辑:这是与评论中提到的方法类似的方法,但它与您的原始工作流程保持接近。