100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > python中等高线填充颜色_Matplotlib:使用透明颜色填充等高线图

python中等高线填充颜色_Matplotlib:使用透明颜色填充等高线图

时间:2018-09-11 23:11:46

相关推荐

python中等高线填充颜色_Matplotlib:使用透明颜色填充等高线图

有人知道——在Matplotlib中——如何用半透明的颜色生成好看的填充等高线图吗?如果向contourf()传递半透明颜色的颜色贴图,则填充区域之间会产生小间隙:

根据docs,这不是一个bug(“contourf()[…]不绘制多边形边”)。要绘制边缘,建议“添加调用contour()的线条轮廓”。但这看起来也不好,因为边缘变得太不透明:

您可以使用linewidth的linewidth参数,但这没有多大帮助。有什么想法吗?在

下面是重现问题的代码(我使用面向对象的API,但结果与pyplot相同):import matplotlib

import numpy as np

from matplotlib.figure import Figure

from matplotlib.backends.backend_agg import FigureCanvasAgg

# generate some data

shape = (100, 100)

x_rng = np.linspace(-1, 1, shape[1])

y_rng = np.linspace(-1, 1, shape[0])

x, y = np.meshgrid(x_rng, y_rng)

z = np.sqrt(x**2 + y**2)

# create figure

width_inch, height_inch = 5, 5 # results in 500x500px with dpi=100

fig = Figure()

fig.set_size_inches((width_inch, height_inch))

FigureCanvasAgg(fig)

ax = fig.add_axes([0., 0., 1., 1.])

ax.set_axis_off()

# define some colors with alpha < 1

alpha = 0.9

colors = [

(0.1, 0.1, 0.5, alpha), # dark blue

(0.0, 0.7, 0.3, alpha), # green

(0.9, 0.2, 0.7, alpha), # pink

(0.0, 0.0, 0.0, alpha), # black

(0.1, 0.7, 0.7, alpha), # light blue

]

cmap = matplotlib.colors.ListedColormap(colors)

levels = np.array(np.linspace(0, z.max(), len(colors)))

norm = matplotlib.colors.BoundaryNorm(levels, ncolors=cmap.N)

# contourf plot produces small gaps between filled areas

cnt = ax.contourf(x, y, z, levels, cmap=cmap, norm=norm,

antialiased=True, linecolor='none')

# this fills the gaps, but it makes them too opaque

# ax.contour(x, y, z, levels, cmap=cmap, norm=norm,

# antialiased=True)

# the same is true for this trick:

# for c in cnt.collections:

# c.set_edgecolor("face")

filename = "/tmp/contourf.png"

fig.savefig(filename, dpi=100, transparent=True, format="png")

PS:SVG后端同样的图看起来不错。在

PPS:pcolormesh()也有类似的问题:

^{pr2}$

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。