首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >三角形等值线网格划分

三角形等值线网格划分
EN

Stack Overflow用户
提问于 2011-04-03 08:17:58
回答 1查看 3.1K关注 0票数 0

我正在尝试使用matplotlib和Dirichlet distribution为维基百科上的numpy重新创建图表(或者只是等值线图就可以了)。我很难很容易地生成一个三角形轮廓。第一个问题是meshgrid没有返回点的三角形。即使我得到一个三角形的点,contourf会处理非矩形的输入吗?

这是我到目前为止所知道的:

代码语言:javascript
运行
复制
#!/usr/bin/env python
from __future__ import division
import matplotlib
matplotlib.use("TkAgg")
matplotlib.rc('text', usetex=True)
matplotlib.rcParams['text.latex.preamble']=r"""\usepackage{amsmath}
"""
import math
import scipy.special

root_three_over_two = np.sqrt(3) / 2

def new_figure():
    # 1.45
    plt.figure(figsize = [2.6, 2.6 * root_three_over_two], dpi = 1200)
    plt.axes([0.05, 0.10, 0.90, 0.90], frameon = False)

xsize = 1.0
ysize = root_three_over_two * xsize
plt.axis([0, xsize, 0, ysize])
resolution = 0.05

R = inclusive_arange(0.0, 1.0, resolution)
x, y = np.meshgrid(inclusive_arange(0.0, 1.0, resolution),
                   inclusive_arange(0.0, 1.0, resolution))
# UNFORTUNATELY x, and y include a lot of points where x+y>1
x = []
y = []
for yy in R:
    x.append(list(inclusive_arange(0.0, 1.0 - yy, resolution)))
    y.append([yy for xx in R])
print x
print y
z = 1 - x - y

# We can use these to convert to and from the equilateral triangle.
M = [[1, 0.5], [0, root_three_over_two]]
Mi = np.linalg.inv(M)

def dirichlet(x, y, z, a, b, c):
    if z < 0:
        return 0
    return x ** (a - 1) * y ** (b - 1) * z ** (c - 1) \
            * math.gamma(a + b + c) \
            / (math.gamma(a) * math.gamma(b) * math.gamma(c))

dirichlet = np.frompyfunc(dirichlet, 6, 1)

for (dirichlet_parm, filename) in [((5.0, 1.5, 2.5), "dir_small.pdf")]:
    new_figure()
    height = dirichlet(x, y, z, *dirichlet_parm)
    M = np.max(height)
    cs = plt.contourf(x, y, height, 50)
    S = sum(dirichlet_parm)
    plt.savefig(filename)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-03 12:53:59

创建等高线图不需要直线网格。可以定义周长并执行Delaunay三角剖分。当然,坐标仍然是直线的,(看起来)你需要做一个变换。This example应该足以创建2D轮廓。我制作了一些带有非矩形周长的3D曲面图,你会得到一些难看的边缘伪影。一个非常好的网格可能会改善其中的一些。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5526743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档