0

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 Answers1

1

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.

pask
  • 899
  • 9
  • 19
  • I get a depreciation warning "The ishold function was deprecated in version 2.0. was_held = ax.ishold()". – Mr. T Jul 24 '18 at 08:12
  • @Mr.T indeed, the problem is the backwards compatibility of scipy with matplotlib 1.x. This should be solved when scipy drops support for matplotlib 1.x versions. – pask Jul 24 '18 at 08:27
  • 1
    This is now breaking the function (matplotlib 3.x) https://github.com/scipy/scipy/issues/7199 – j_s Jul 02 '19 at 11:48
  • @j_s which scipy version are you using? Should be solved with scipy 1.2+ and matplotlib 3.x (https://github.com/scipy/scipy/issues/9713). – pask Jul 04 '19 at 15:08
  • @pask my bad, I was using scipy 1.1 (sorry for that) – j_s Jul 15 '19 at 07:43