【问题标题】:Overlay a figure object to matplotlib plot将图形对象覆盖到 matplotlib 图
【发布时间】:2021-11-24 07:42:32
【问题描述】:

我有一个函数返回的图形对象。

import numpy as np
from scipy.spatial import Voronoi, voronoi_plot_2d, Delaunay
import shapely.geometry
import shapely.ops

points = np.random.random((20, 2))
print(points)
vor = Voronoi(points)
fig = voronoi_plot_2d(vor, show_vertices=True, show_points=True)
fig.add
plt.show()
print(vor.ridge_points)
print(vor.ridge_points[1,0])
print(vor.ridge_points[1,1])
plt.plot(points[vor.ridge_points[1,0]], points[vor.ridge_points[1,1]])
plt.show()

我想覆盖fig 在该行中创建的另一个图上 plt.plot(points[vor.ridge_points[1,0]], points[vor.ridge_points[1,1]])

关于如何在一个图中可视化两个图的建议会很有帮助。

【问题讨论】:

  • 制作一个轴并将ax kwarg 传递给voronoi_plot_2d
  • 如果您省略了对plt.show() 的第一次调用,那么plt.plot(...) 将默认进一步绘制到已创建的绘图上。

标签: python matplotlib scipy figure


【解决方案1】:

您应该创建一个fig, ax 对象,并将ax 参数传递给voronoi_plot_2d,正如@Jody Klymak 在cmets 中所建议的那样,例如:

import numpy as np
from scipy.spatial import Voronoi, voronoi_plot_2d, Delaunay
import shapely.geometry
import shapely.ops
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
points = np.random.random((20, 2))
print(points)
vor = Voronoi(points)
voronoi_plot_2d(vor, show_vertices=True, show_points=True, ax=ax)

print(vor.ridge_points)
print(vor.ridge_points[1,0])
print(vor.ridge_points[1,1])
ax.plot(points[vor.ridge_points[1,0]], points[vor.ridge_points[1,1]])
plt.show()

【讨论】:

    猜你喜欢
    • 2015-11-30
    • 2012-03-27
    • 2020-08-06
    • 2021-01-19
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    相关资源
    最近更新 更多