1

In order to color the areas of a scipy.spatial Voronoi tessellation with respect to an own colorbar I am using the code from here

I want to add some more points (that are independent of the Voronoi tessellation) to the plot. However, only the points outside of the finite regions are being plotted. Any ideas on how to make all of the extra points visible? E.g. only 1 out of 5 points is visible in this plot (see code below)

Also, I would like to add a colorbar on the side to see my color scale.

I appreciate any help, thank you!

def plot_voronoi_colour(vor, O):

# @param vor: Voronoi tessellation
# @param O: own color scale

# find min/max values for normalization
minima = min(O)
maxima = max(O)

# normalize chosen colormap
norm = mpl.colors.Normalize(vmin=minima, vmax=maxima, clip=True)
mapper = mpl.cm.ScalarMappable(norm=norm, cmap='viridis')

# plot Voronoi diagram, and fill finite regions with color mapped from speed value
voronoi_plot_2d(vor, show_points=True, show_vertices=False, s=1)
for r in range(len(vor.point_region)):
    region = vor.regions[vor.point_region[r]]
    if not -1 in region:
        polygon = [vor.vertices[i] for i in region]
        plt.fill(*zip(*polygon), color=mapper.to_rgba(O[r]))

p2 = [[0,0],[20, 10], [30, 40], [70, 35], [75, 75]] 
# [Only [0,0] visible][1]
x, y = zip(*p2)

plt.scatter(x, y, c='y', s=500)
Amira Bedhiafi
  • 8,088
  • 6
  • 24
  • 60
kappie
  • 11
  • 5

1 Answers1

0

I spent a long time trying to add a colour bar to a Voronoi plot and I finally came up with the following solution.

#packages to import
from scipy.spatial import Voronoi, voronoi_plot_2d
import matplotlib.cm as cm
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import gridspec

# find min/max values for normalization
minima = 0
maxima = 1

#sets the colour scale  
norm = mpl.colors.Normalize(vmin=minima, vmax=maxima, clip=True)
mapper = cm.ScalarMappable(norm=norm, cmap=cm.PRGn)

#create the figure with set figure size
fig = plt.figure(figsize=(10,8))

#puts a grid on the figure
gs = gridspec.GridSpec(16, 10) 

#creates two subplots
ax = plt.subplot2grid((16,20), (0,17), colspan=1, rowspan=16)
ax2 = plt.subplot2grid((16,20), (0,0), colspan=16, rowspan=16)

#creates a colourbar on the first subplot
cb1 = mpl.colorbar.ColorbarBase(ax, cmap=cm.PRGn, norm=norm, orientation='vertical')
cb1.set_label('Order Parameter')
mpl.rcParams.update({'font.size': 15})

#plots the voronoi diagram on the second subplot
voronoi_plot_2d(vor, show_vertices =False, show_points =False, ax=ax2)

#gives the order values for colour scale (which come from a data frame in a different part of the code)
order = df_3['phi6'] 

#colours the voronoi cells    
for r in range(len(vor.point_region)):
    region = vor.regions[vor.point_region[r]]
    if not -1 in region:
        polygon = [vor.vertices[i] for i in region]
        plt.fill(*zip(*polygon), color=mapper.to_rgba(order[r]))

This site helped me a lot with changing the sizes of the subplots and this post gave me the idea to use the grid spacing.

I hope this helps you.

KMac
  • 1