Projecting contour lines from one graph to another

Viewed 90

I want to project the contour lines from one figure to another figure (with the same x,y reference system). Instead of onto a fixed plane I want it to be projected onto a surface plot in the second figure.

UPDATE: There is a workaround; by using the contour.allsegs command you can get a list of arrays containing x,y coordinates of the contour lines, which you can then use to preprocess data for the second figure.

BUT: This only works, because I know the mathematical respresentation of the second surface. Meaning I can simply calculate the z-values using the x,y-values of the first-figure contour lines according to the formula.

import numpy as np
import matplotlib.pyplot as plt

X,Y = np.meshgrid(np.arange(0,5,1/5), np.arange(0,5,1/5))
Z1 = np.sin(X)+np.cos(Y)
Z2 = X+Y

fig = plt.figure(figsize=plt.figaspect(0.5))

ax1 = fig.add_subplot(1, 2, 1, projection='3d',title='fig1')
surf1 = ax1.plot_surface(X, Y, Z1, alpha=0.2)
contour1 = ax1.contour(X, Y, Z1)

segs=contour1.allsegs

ax2 = fig.add_subplot(1, 2, 2, projection='3d',title='fig2')
surf2 = ax2.plot_surface(X, Y, Z2, alpha=0.2)

for i in range(len(segs)):
    if segs[i]:
        plt.plot(segs[i][0][:,0],segs[i][0][:,1],[x+y for x,y in zip(segs[i][0][:,0],segs[i][0][:,1])])
        if len(segs[i]) >= 2:
            plt.plot(segs[i][1][:,0],segs[i][1][:,1],[x+y for x,y in zip(segs[i][1][:,0],segs[i][1][:,1])])

plt.show()

Resulting in:

enter image description here

  • Is there another (easy) way to get contour lines from fig1 projected onto the surface of fig2 (maybe a mpl built-in function I am missing)?

  • How can I further develop the approach above to also be able to use it with surfaces I do not know the mathematical representation of, but rather have a XYZ data set from a file?

Any hints or alternative approaches for a solution are very welcome!

0 Answers
Related