Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two existing plots into one plot

I haven't really attempted any way to do this, but I am wondering if there is a way to merge two plots that already exist into one graph. Any input would be greatly appreciated!

like image 598
user1620716 Avatar asked Oct 03 '12 14:10

user1620716


People also ask

How do I merge two maplestory plots?

You can also combine multiple plots using drag and drop. Press Enter. Click sin(x) and, from the context panel, choose Plots > 2-D Plot.


2 Answers

Here is a complete minimal working example that goes through all the steps you need to extract and combine the data from multiple plots.

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,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,1,2)
plt.plot(X,Y,'g')
plt.show()

enter image description here

like image 198
Hooked Avatar answered Oct 25 '22 07:10

Hooked


Assuming you are using Matplotlib, you can get the data for a figure as an NX2 numpy array like so:

gca().get_lines()[n].get_xydata()
like image 41
Gareth Avatar answered Oct 25 '22 07:10

Gareth