【问题标题】:How to add information to a scipy.spatial voronoi plot?如何向 scipy.spatial voronoi 图添加信息?
【发布时间】:2019-11-16 04:13:12
【问题描述】:

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

我想在绘图中添加更多点(独立于 Voronoi 细分)。但是,仅绘制了有限区域之外的点。关于如何使所有额外点可见的任何想法?例如。 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)

【问题讨论】:

  • zorder=3 添加到您的散点图。
  • 感谢您的帮助!效果很好!还有一种方法可以在绘图的一侧添加一个颜色条(见上面的帖子)?

标签: python matplotlib colorbar voronoi


【解决方案1】:

我花了很长时间尝试向 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 给了我使用网格间距的想法。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 2019-11-10
    • 2019-11-14
    • 2014-08-08
    • 1970-01-01
    相关资源
    最近更新 更多