【问题标题】:plotting data on a hexagonal figure在六边形图形上绘制数据
【发布时间】:2021-04-19 21:23:15
【问题描述】:

我想构建一个看起来像这样的图形, - 对于每个点,我都有一个值,并且有一个最大值到达边界。 我所能找到的只是如何在 seaborn 或类似的散点图中使用 hexbin - 任何想法,是否有一些现成的解决方案,或者我需要编写代码来解决它?

【问题讨论】:

    标签: python pandas hex seaborn data-visualization


    【解决方案1】:

    您可以使用 tripcolor 显示 6 个带阴影的三角形。缩放外部向量可以调整三角形以显示所需的比例。

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.tri as tri
    
    proportions = [0.6, 0.75, 0.8, 0.9, 0.7, 0.8]
    labels = ['alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta']
    N = len(proportions)
    proportions = np.append(proportions, 1)
    theta = np.linspace(0, 2 * np.pi, N, endpoint=False)
    x = np.append(np.sin(theta), 0)
    y = np.append(np.cos(theta), 0)
    triangles = [[N, i, (i + 1) % N] for i in range(N)]
    triang_backgr = tri.Triangulation(x, y, triangles)
    triang_foregr = tri.Triangulation(x * proportions, y * proportions, triangles)
    cmap = plt.cm.rainbow_r  # or plt.cm.hsv ?
    colors = np.linspace(0, 1, N + 1)
    plt.tripcolor(triang_backgr, colors, cmap=cmap, shading='gouraud', alpha=0.4)
    plt.tripcolor(triang_foregr, colors, cmap=cmap, shading='gouraud', alpha=0.8)
    plt.triplot(triang_backgr, color='white', lw=2)
    for label, color, xi, yi in zip(labels, colors, x, y):
        plt.text(xi * 1.05, yi * 1.05, label,  # color=cmap(color),
                 ha='left' if xi > 0.1 else 'right' if xi < -0.1 else 'center',
                 va='bottom' if yi > 0.1 else 'top' if yi < -0.1 else 'center')
    plt.axis('off')
    plt.gca().set_aspect('equal')
    plt.show()
    

    代码允许使用不同数量的三角形。以下是 5 或 6 个三角形的示例:

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    相关资源
    最近更新 更多