Search code examples
pythonpandashexseaborndata-visualization

plotting data on a hexagonal figure


I want to build a graph that will look like this, - for each point I have a single value and there is a maximum that reaches the border. All I can find is how to have hexbin in a scatterplot with seaborn or similar - any ideas, is there some ready solution maybe or I would need to code my way through it?

graph


Solution

  • You could use tripcolor to show 6 shaded triangles. Scaling the outer vectors can adapt the triangles to show the desired proportions.

    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()
    

    The code allows for different numbers of triangles. Here are examples with 5 or 6 triangles:

    example plot