在 matplotlib 的新子图中重新绘制现有图形
re-plot existing figures in a new supblot in matplotlib
说在可视化数据的过程中,我后来决定将两个在子图中有大量注释的现有图组合起来,在同一个 window.
上并排比较它们
是的,我可以回去并首先在子图中重新创建这些。但是,有没有一种方法可以让我抓住轴或图形句柄——我不明白它是如何工作的——无论捕获单个图形的所有内容并使用数据来创建新的子图?
类似于
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 中的子图。
代码中的每个 ax1
和 ax2
都是 Line2D
的列表。您可以使用 .get_data()
和 plot:
提取线的数据
# 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())
输出:
说在可视化数据的过程中,我后来决定将两个在子图中有大量注释的现有图组合起来,在同一个 window.
上并排比较它们是的,我可以回去并首先在子图中重新创建这些。但是,有没有一种方法可以让我抓住轴或图形句柄——我不明白它是如何工作的——无论捕获单个图形的所有内容并使用数据来创建新的子图?
类似于
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 中的子图。
代码中的每个 ax1
和 ax2
都是 Line2D
的列表。您可以使用 .get_data()
和 plot:
# 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())
输出: