Superimpose two Voronoi plots in Python

1k views Asked by At

I'd like to superimpose two Voronoi plots in Python, but when I plot the two it just gives me two different plots ( I want both diagrams on the same plot).

Here's my code :

import numpy as np 
import scipy.spatial as sp
import matplotlib.pyplot as plt 
points = np.array([[0, 0], [0, 1], [0, 2], [1, 0],[1,1],[1, 2], [2, 0], [2, 1],[2, 2]])
vor=sp.Voronoi(points)
sp.voronoi_plot_2d(vor)
point_bis=np.array([[0.5,0.5],[1,1.5],[1.5,1],[2,2.5]])
vor2=sp.Voronoi(point_bis)
sp.voronoi_plot_2d(vor2)

Thanks

1

There are 1 answers

5
pask On

You need to plot both plots on the same axis

import numpy as np 
import scipy.spatial as sp
import matplotlib.pyplot as plt 

fig, ax = plt.subplots()

points = np.array([[0, 0], [0, 1], [0, 2], [1, 0],[1,1],[1, 2], [2, 0], [2, 1],[2, 2]])
vor=sp.Voronoi(points)
sp.voronoi_plot_2d(vor, ax=ax)
point_bis=np.array([[0.5,0.5],[1,1.5],[1.5,1],[2,2.5]])
vor2=sp.Voronoi(point_bis)
sp.voronoi_plot_2d(vor2, ax=ax)

enter image description here

Edit: This solution won't work with a combination of scipy 1.1- and matplotlib 3.x.