如何在scipy.spatial voronoi图中添加信息?

时间:2019-07-05 14:02:07

标签: python matplotlib colorbar voronoi

为了相对于自己的颜色条为scipy.spatial Voronoi镶嵌的区域着色,我正在使用here

中的代码

我想添加更多点(与Voronoi镶嵌无关)。但是,仅绘制有限区域之外的点。关于如何使所有额外点可见的任何想法?例如。 in this plot仅显示5分中的1分(请参见下面的代码)

此外,我想在侧面添加一个颜色条以查看我的色阶。

感谢您的帮助,谢谢!

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)

1 个答案:

答案 0 :(得分:0)

我花了很长时间尝试在Voronoi图中添加一个彩条,最终我想到了以下解决方案。

#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在改变子图的大小方面给了我很大帮助,this post给了我使用网格间距的想法。

希望对您有帮助。