Home > Mobile >  Overlay a figure object to matplotlib plot
Overlay a figure object to matplotlib plot

Time:10-05

I have a figure object returned by a function.

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

I would like to overlay fig on another plot created in the line plt.plot(points[vor.ridge_points[1,0]], points[vor.ridge_points[1,1]])

Suggestions on how to visualize both the plots in a single figure will be helpful.

CodePudding user response:

You should create a fig, ax object, and pass the ax argument to the voronoi_plot_2d as suggested in the comments by @Jody Klymak, like:

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

enter image description here

  • Related