gpt4 book ai didi

python - 在 matplotlib 子图中显示 LineCollections

转载 作者:太空宇宙 更新时间:2023-11-03 19:14:55 24 4
gpt4 key购买 nike

当我尝试在两个子图中显示 LineCollections 时,没有显示任何内容。当我只在第一个中显示它时,它就起作用了。我怎样才能让它同时显示在两者中?

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()

编辑:

真正的问题是“LineCollection 对象可以重用吗?”

最佳答案

您可以使用复制模块制作 lc_edges 的卷影副本。 lc_edges2 和 lc_edges 都会使用相同的路径列表,您可以通过以下方式确认: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()

结果如下:

LineCollection result

关于python - 在 matplotlib 子图中显示 LineCollections,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11670154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com