Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在matplotlib子地块中显示线条集合_Python_Matplotlib - Fatal编程技术网

Python 在matplotlib子地块中显示线条集合

Python 在matplotlib子地块中显示线条集合,python,matplotlib,Python,Matplotlib,尝试在两个子地块中显示线条集合时,将不显示任何线条集合。当我只在第一个窗口中显示它时,它就工作了。我怎样才能让它同时显示在这两种格式中 import numpy import matplotlib.delaunay from matplotlib import pyplot as plt from matplotlib.collections import LineCollection # Unique points points = numpy.random.randint(0,2000,

尝试在两个子地块中显示线条集合时,将不显示任何线条集合。当我只在第一个窗口中显示它时,它就工作了。我怎样才能让它同时显示在这两种格式中

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\U边进行阴影复制。lc_边2和lc_边将使用相同的路径列表,您可以通过以下方式确认:
lc_边。_路径是lc_边2。_路径

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()
结果如下:


您可以使用复制模块对lc\U边进行阴影复制。lc_边2和lc_边将使用相同的路径列表,您可以通过以下方式确认:
lc_边。_路径是lc_边2。_路径

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
不能满足您的需要。它有一个属性
axes
,该属性指定与它关联的轴(并且在创建
LineCollection
时设置为
None
)。当您将
LineCollection
添加到第二个轴时,
lc_edges
的轴记录了更改(它作为
ax2
的子轴添加),但我敢打赌更改不会一直传播到对象堆栈,并且有些东西会变得混乱。很抱歉,我没有更好的答案。我认为这个问题应该比
LineCollections
更一般,因为同样的事情似乎也会发生在普通的lines对象上。这可能有助于你为什么尝试重新使用linecollection,而不是为第二个绘图创建一个新的LineCollections?因为在我的真实案例中,它非常庞大,我希望不必重置我为它定义的属性。我不能重复使用它吗?我怀疑问题在于
LineCollection
不能满足您的需要。它有一个属性
axes
,该属性指定与它关联的轴(并且在创建
LineCollection
时设置为
None
)。当您将
LineCollection
添加到第二个轴时,
lc_edges
的轴记录了更改(它作为
ax2
的子轴添加),但我敢打赌更改不会一直传播到对象堆栈,并且有些东西会变得混乱。很抱歉,我没有更好的答案。我认为这个问题应该比
LineCollections
更一般,因为普通的lines对象似乎也会发生同样的情况。这可能会有所帮助