0

The problem is there are 2 seperate Figure objects displayed on the screen. Each Figure object contains an Axes object. How can I overwrite / replace the Axes object in Figure 1, with the Axes object from Figure 2?

In other words:

  1. We have some data called A.
  2. We plot the data called A.
  3. The plot is returned in a new Figure and displayed.
  4. We have some data called B.
  5. We plot the data called B.
  6. Now we want to copy and overwrite the Axes containing data B into the Axes containing data A
import matplotlib.pyplot as plt
import numpy as np

plt.ion()

# Draw the first figure and plot
x_1 = np.linspace(0, 10*np.pi, 100)
y_1 = np.sin(x_1)

figure_1 = plt.figure()
axes_1 = figure_1.add_subplot(111)
line_1, = axes_1.plot(x_1, y_1, 'b-')
figure_1.canvas.draw()

# Draw the second figure and plot
x_2 = np.linspace(0, 9*np.pi, 100)
y_2 = np.sin(x_2)

figure_2 = plt.figure()
axes_2 = figure_2.add_subplot(111)
line_2 = axes_2.plot(x_2, y_2, 'r-')
figure_2.canvas.draw()

# Now clear figure_1's axes and replace it with figure_2's axis
figure_1.get_axes()[0].clear()
figure_1.add_axes(axes_2)
figure_1.canvas.draw()
print("Done")

Using the add_axes() method is raising ValueError "The Axes must have been created in the present figure".

Is there a way to detatch the Axes from the current figure so that it can be added to the other one?

There is this question which addresses copying an Axes and displaying in a new figure, however I want to display it in an existing figure.

  • Does this answer your question? [pyplot - copy an axes content and show it in a new figure](https://stackoverflow.com/questions/45810557/pyplot-copy-an-axes-content-and-show-it-in-a-new-figure) – Mr. T Nov 16 '20 at 11:26
  • Thanks yes I have seen that. It is not far off what I need to do, however maybe a better title for this question would be "copy an axes content and show it in an existing figure" – ArmadilloChubbz65 Nov 16 '20 at 13:38
  • No difference. You cannot copy axes objects. – Mr. T Nov 16 '20 at 13:46
  • Yes it does explain that you cannot copy axes objects. However that post was eventually successful as they were able to copy a figure using pickle, and then display the axes in a new seperate figure. Unfortunatly it does not explain how to copy the axes and display in an exisiting figure. – ArmadilloChubbz65 Nov 16 '20 at 14:52

1 Answers1

-1
import matplotlib.pyplot as plt
import numpy as np

# b: blue, g: green, r: red, c: cyan, m: magenta, y: yellow, k: black, w: white

##################################################################################
# EQUATIONS
##################################################################################

x_1 = np.linspace(0, 10*np.pi, 100)
y_1 = np.sin(x_1)

#If you change the equation with a conditional/loop, you will change the plot

##################################################################################
# LABELING
##################################################################################

plt.xlabel('x axis')
plt.ylabel('y axis')
plt.title('Data_VIsualization')

##################################################################################
# PLOTTING
##################################################################################

plt.plot(x_1, y_1, color='b', linestyle='--')
plt.show()
jordiba90
  • 5
  • 1
  • 6
  • 1
    Thanks, however I do not need to change the equation. My problem is about copying an ```Axes``` object from one ```Figure``` object and inserting into a seperate ```Figure``` object – ArmadilloChubbz65 Nov 16 '20 at 09:23