python - 在 Matplotlib 中绘制字形图

标签 python matplotlib plot glyph

我使用Matplotlib来绘制一些科学可视化效果,其中热图(如下图所示)就足够了。我的代码也是用Python编写的。

enter image description here

但是,现在我需要呈现一个更“精致”的情节,其中包含字形。下图(第二张)显示了一个示例:

enter image description here

在该图像中,绘图的每个点都是一个字形,表示矢量场方向的概率。该字形被绘制为一个具有主方向和与其方向的标准偏差的圆圈。

我想做类似的事情。我的想法是在每个位置绘制类似极坐标直方图的东西,并绘制由极坐标图组成的图。然而,我认为 Matplotlib 不可能做到这一点;至少我不知道如何做到这一点。由于我的整个代码都是用 Python 编写的,我想知道我是否可以使用 Matplotlib,或者我是否应该研究如何使用 OpenGL 或其他 API/libraty 来做到这一点。

谢谢。

最佳答案

也许有很多这样的事情:

enter image description here

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import PatchCollection
from matplotlib.patches import Wedge, Circle
from math import degrees, pi

fig, ax = plt.subplots()
wedges = []
circles = []

for x in np.arange(0, 3.3, .3):
    for y in np.arange(0, 3.3, .3):
        theta, phi = np.random.random(2)  # functions of (x,y) in reality
        for v in (0, pi):
            wedges.append(Wedge((x, y),
                            .15,
                            degrees(v - phi - theta/2),
                            degrees(v - phi + theta/2),
                            edgecolor='none'),
                            )
        circles.append(Circle((x, y),
                             .15,
                             edgecolor='none'))


colors = np.linspace(0, 1, len(circles))  # function of (x,y) in reality
collection = PatchCollection(circles, cmap=plt.cm.jet, alpha=0.2)
collection.set_array(np.array(colors))
collection.set_edgecolor('none')
ax.add_collection(collection)

#wedgecolors = list(chain.from_iterable(repeat(i,2) for i in colors))
wedgecolors = np.array([colors, colors]).flatten('F') # no itertools
collection = PatchCollection(wedges, cmap=plt.cm.jet, alpha=1)
collection.set_array(np.array(wedgecolors))
collection.set_edgecolor('none')
ax.add_collection(collection)

ax.set_xlim(0,3)
ax.set_ylim(0,3)
ax.set_aspect('equal')
plt.show()

(显然,必须在 collection.set_array 调用之后完成(重做?)设置边缘颜色。)

关于python - 在 Matplotlib 中绘制字形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29866592/

相关文章:

雷达图轴范围

python - 使用 Flask-Restful 时使用 fields.Url 生成 url 会生成 BuildError

python - 使用 matplotlib.collection.LineCollection 时发生内存泄漏

perl - 如何使用 Perl 绘制时间序列图?

matplotlib - Seaborn regplot 创建重复轴

python - Matplotlib - 在同一轴上绘制两个 3D 表面时重叠错误

r - 如何用 R 重现这个移动分布图?

python - Django 与乘客

python - 追加后如何打印列表中的所有元素?

java - 为什么PHP构造函数是一个方法?