Reputation: 11860
When I try to display LineCollections in both subplots, none are displayed. When I display it only in the first, it works. How can I get it to be displayed in both?
import numpy
import matplotlib.delaunay
from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection
# Unique points
points = numpy.random.randint(0,2000, (1000, 2))
points = numpy.vstack([numpy.array(u) for u in set([tuple(p) for p in points])])
# Delaunay edges
centers, edges, tris, neighb = matplotlib.delaunay.delaunay(points[:,0], points[:,1])
# LineCollection of edges
lc_edges = LineCollection(points[edges])
# 1x2 subplots
fig,(ax) = plt.subplots(1, 2, figsize=(12,16))
ax1 = plt.subplot(211, aspect='equal', xlim=[points[:,0].min()- 100, points[:,0].max() + 100], ylim=[points[:,1].min() - 100, points[:,1].max() + 100])
plt.title("First plot")
plt.gca().add_collection(lc_edges)
plt.scatter(points[:,0], points[:,1])
ax2 = plt.subplot(212, aspect='equal', xlim=[points[:,0].min()- 100, points[:,0].max() + 100], ylim=[points[:,1].min() - 100, points[:,1].max() + 100])
plt.title("Second plot")
#plt.gca().add_collection(lc_edges)
plt.scatter(points[:,0], points[:,1])
fig.savefig('myfile.png', dpi=250)
plt.close()
EDIT:
The real question is "Can LineCollection objects be re-used?"
Upvotes: 1
Views: 2493
Reputation: 97331
You can make a shadow copy of lc_edges by using copy module. Both the lc_edges2 and lc_edges will use the same path list, you can confirm it by: lc_edges._paths is lc_edges2._paths
.
import numpy
import matplotlib.delaunay
from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection
import copy
# Unique points
points = numpy.random.randint(0,2000, (1000, 2))
points = numpy.vstack([numpy.array(u) for u in set([tuple(p) for p in points])])
# Delaunay edges
centers, edges, tris, neighb = matplotlib.delaunay.delaunay(points[:,0], points[:,1])
# LineCollection of edges
lc_edges = LineCollection(points[edges])
lc_edges2 = copy.copy(lc_edges)
# 1x2 subplots
fig,(ax) = plt.subplots(1, 2, figsize=(12,16))
ax1 = plt.subplot(211, aspect='equal', xlim=[points[:,0].min()- 100, points[:,0].max() + 100], ylim=[points[:,1].min() - 100, points[:,1].max() + 100])
plt.title("First plot")
plt.gca().add_collection(lc_edges)
plt.scatter(points[:,0], points[:,1])
ax2 = plt.subplot(212, aspect='equal', xlim=[points[:,0].min()- 100, points[:,0].max() + 100], ylim=[points[:,1].min() - 100, points[:,1].max() + 100])
plt.title("Second plot")
plt.gca().add_collection(lc_edges2)
plt.scatter(points[:,0], points[:,1])
plt.show()
The result looks like:
Upvotes: 1