0

I'd like to add multiple plots to a specific subplot with the same axis. So far I managed to do that with by just repeatedly callign plt.[some_plotting_function] after activating the corresponding plt.subplot(xxx) like so:

import matplotlib.pyplot as plt

plt.subplot(121)
plt.plot([0, 1], [0, 1], 'o-')

plt.subplot(122)
plt.plot([0, 1], [0,0], 'o-')

plt.subplot(121)
plt.plot([0, 1], [1, 0], 'o-')
plt.show()

enter image description here

Now I get the warning

MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.

It sounds like the same code will behave differently in the future. How will I be able to achieve the same result as with the code above in the future?

flawr
  • 10,814
  • 3
  • 41
  • 71

1 Answers1

1

You can still use this code, but refactor it to use returned axes instead of referring to subplots, like:

ax1 = plt.subplot(121)
ax1.plot([0, 1], [0, 1], 'o-')

ax2 = plt.subplot(122)
ax2.plot([0, 1], [0,0], 'o-')

ax1.plot([0, 1], [1, 0], 'o-')
plt.show()

You can see instead of referring to first subplot, I use axis returned by the first subplot call.

Edit: this is a similar approach to the one mentioned in the comments, but it stays close to your original workflow.

dm2
  • 4,053
  • 3
  • 17
  • 28