gpt4 book ai didi

python - 如何制作带有未定义区域的彩色图?

转载 作者:太空宇宙 更新时间:2023-11-03 13:36:22 25 4
gpt4 key购买 nike

假设我有一个类似的函数

def coulomb(x,y):
r = sqrt(x**2 + y**2)
return 1/r if r > 1 else None

我怎样才能最好地在彩色图中绘制它,以便每个 None 值都简单地呈现为例如白色,只有分配给色标的实际数值?喜欢,

fig = plt.figure()

xs, ys = meshgrid(linspace(-5, 5, n), linspace(-5, 5, n))
vs = 1/sqrt(xs**2 + ys**2)

ax = fig.add_subplot(1, 1, 1, aspect='equal')
fig.colorbar(ax.pcolor(xs,ys,vs, vmin=0, vmax=1))

The function plot without the excluded domain.

但中心区域为空白而不是深红色。

Center erased

最佳答案

只需使用掩码数组:

from numpy import ma
vs_ma = ma.masked_where(vs > 1, vs)
plt.colorbar(plt.pcolor(xs, ys, vs_ma, vmin=0, vmax=1))

enter image description here

matplotlib 有一个更复杂的例子 image_masked.py您可以在其中选择屏蔽区域的颜色。要在普通数组和屏蔽数组之间进行转换,您可以使用 numpy.ma.masked_* 之一功能

关于python - 如何制作带有未定义区域的彩色图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39021353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com