【问题标题】:re-plot existing figures in a new supblot in matplotlib在 matplotlib 的新 supblot 中重新绘制现有图形
【发布时间】:2021-02-10 03:26:56
【问题描述】:

说在可视化数据的过程中,我后来决定将两个现有的图合并在一个子图中进行大量注释,以便在同一个窗口中并排比较它们。

是的,我可以首先在子图中返回并重新创建这些。但是,有没有办法我可以抓住轴或图形手柄——我不明白它是如何工作的——无论是捕获单个图形的所有内容并使用数据来创建新的子图吗?

类似的东西

from numpy.random import seed
from numpy.random import randint
import matplotlib.pyplot as plt    

x=list(range(1,11))
seed(1)
y1= randint(5, 35, 10)
seed(2)
y2= randint(5, 35, 10)
seed(3)
y3=randint(5, 35, 10)

fig1=plt.figure(1)
ax1=plt.plot(x,y1,x,y2,x,y3)
plt.xlabel('Xaxis')
plt.ylabel('yaxis')
plt.title('Some Plot')
plt.text(10,10, 'some text')

fig2=plt.figure(2)
ax2= plt.plot(x,y1+y2+y3)

# later, I say decided I wanted to also display the two plots as subplots in the same window.
fig3=pltfigure(3)
plt.subplot(2,1,1)
plt.plot(ax1) # plt.plot(fig1.lines),plt.plot(fig1)
plt.subplot(2,1,2)
plt.plot(ax2)

我正在寻找一种简单的方法来获取图 1 和图 2 中已经绘制的所有内容,并将其直接传递给图 3 中的子图。

【问题讨论】:

    标签: matplotlib subplot


    【解决方案1】:

    代码中的每个ax1ax2 都是Line2D 的列表。您可以使用.get_data() 提取线条数据并绘制:

    # later, I say decided I wanted to also display the two plots as subplots in the same window.
    fig3=plt.figure(3)
    plt.subplot(2,1,1)
    for line in ax1:
        plt.plot(*line.get_data())
    plt.subplot(2,1,2)
    for line in ax1:
        plt.plot(*line.get_data())
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 2017-03-02
      • 1970-01-01
      • 2014-06-11
      相关资源
      最近更新 更多