我还没有真正尝试过这样做,但我想知道是否有办法将两个已经存在的图合并到一个图中.任何投入将不胜感激!
最佳答案
这是一个完整的最小工作示例,它完成了提取和组合多个图中数据所需的所有步骤.
import numpy as np
import pylab as plt
# Create some test data
secret_data_X1 = np.linspace(0,1,100)
secret_data_Y1 = secret_data_X1**2
secret_data_X2 = np.linspace(1,2,100)
secret_data_Y2 = secret_data_X2**2
# Show the secret data
plt.subplot(2,1)
plt.plot(secret_data_X1,secret_data_Y1,'r')
plt.plot(secret_data_X2,secret_data_Y2,'b')
# Loop through the plots created and find the x,y values
X,Y = [],[]
for lines in plt.gca().get_lines():
for x,y in lines.get_xydata():
X.append(x)
Y.append(y)
# If you are doing a line plot,we don't know if the x values are
# sequential,we sort based off the x-values
idx = np.argsort(X)
X = np.array(X)[idx]
Y = np.array(Y)[idx]
plt.subplot(2,2)
plt.plot(X,Y,'g')
plt.show()