【问题标题】:how to add more plots to same axes in subplot? [duplicate]如何在子图中将更多图添加到相同的轴? [复制]
【发布时间】:2021-06-25 13:11:07
【问题描述】:

我想将多个图添加到具有相同轴的特定子图。到目前为止,我通过在激活相应的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.

听起来相同的代码在未来会有不同的表现。以后如何才能实现与上面的代码相同的结果?

【问题讨论】:

  • fig, axs = plt.subplots(ncols=2)。然后使用axs[0].plotaxs[1].plot

标签: python matplotlib subplot


【解决方案1】:

您仍然可以使用此代码,但将其重构为使用返回的轴而不是引用子图,例如:

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()

您可以看到,我使用第一个子图调用返回的轴,而不是引用第一个子图。

编辑:这与 cmets 中提到的方法类似,但它与您的原始工作流程保持接近。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 2019-10-08
    • 2016-11-29
    • 2015-05-29
    • 2020-03-14
    • 2018-01-06
    • 1970-01-01
    相关资源
    最近更新 更多