During playback with the ImportanceOfBeingErnest code to move artists between axes, I thought it was easy to expand it to collections (e.g. PathCollectionsgenerated ones plt.scatter) as well. No such luck:
import matplotlib.pyplot as plt
import numpy as np
import pickle
x = np.linspace(-3, 3, 100)
y = np.exp(-x**2/2)/np.sqrt(2*np.pi)
a = np.random.normal(size=10000)
fig, ax = plt.subplots()
ax.scatter(x, y)
pickle.dump(fig, open("/tmp/figA.pickle", "wb"))
fig, ax = plt.subplots()
ax.hist(a, bins=20, density=True, ec="k")
pickle.dump(fig, open("/tmp/figB.pickle", "wb"))
plt.close("all")
figA = pickle.load(open("/tmp/figA.pickle", "rb"))
figB = pickle.load(open("/tmp/figB.pickle", "rb"))
fig, ax = plt.subplots()
for figO in [figA, figB]:
lists = [figO.axes[0].lines, figO.axes[0].patches, figO.axes[0].collections]
addfunc = [ax.add_line, ax.add_patch, ax.add_collection]
for lis, func in zip(lists, addfunc):
for artist in lis[:]:
artist.remove()
artist.axes = ax
artist.figure = fig
func(artist)
ax.relim()
ax.autoscale_view()
plt.close(figA)
plt.close(figB)
plt.show()
gives

Deleting artist.set_transform(ax.transData)(at least during a call ax.add_collection) seems to help a little, but note that the y offset is still disabled:
How do I move collections from one axis to another?
source
share