1

I've tried to find a way to copy a 3D figure in matplotlib but I didn't find a solution which is appropriate in my case.

From these posts How do I reuse plots in matplotlib? and How to combine several matplotlib figures into one figure? Using fig2._axstack.add(fig2._make_key(ax),ax) as in the code below gives quite the good result but figure 2 is not interactive I can't rotate the figure etc :

import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(1)
ax = fig.gca(projection = '3d')

ax.plot([0,1],[0,1],[0,1])

fig2 = plt.figure(2)
fig2._axstack.add(fig2._make_key(ax),ax)

plt.show()

An alternative would be to copy objects from ax to ax2 using a copy method proposed in this post How do I reuse plots in matplotlib? but executing the code below returns RuntimeError: Can not put single artist in more than one figure :

import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D
import numpy as np, copy

fig = plt.figure(1)
ax = fig.gca(projection = '3d')

ax.plot([0,1],[0,1],[0,1])

fig2 = plt.figure(2)
ax2 = fig2.gca(projection = '3d')

for n in range(len(ax.lines)) :
    ax2.add_line(copy.copy(ax.lines[n]))
plt.show()

Those codes are pretty simple but I don't want to copy/paste part of my code for drawing similar figures

Thanks in advance for your reply !

romain.bqt4
  • 159
  • 1
  • 6
  • 1
    Instead of copy-pasting code, why not just write a function which takes an `ax` object as argument and call that function twice with two different `Axes` objects? – Thomas Kühn Jul 31 '17 at 10:08
  • 2
    Yes that would be an "easy" solution but in the case you have a very long plot like a 3d surface or a complicated function, run multiple times this function isn't a "good" solution. Thanks anyway ! – romain.bqt4 Jul 31 '17 at 10:38

0 Answers0