pythonpandasmatplotlibscatterpie

Scatter Pie Plot Python Pandas


"Scatter Pie Plot" ( a scatter plot using pie charts instead of dots). I require this as I have to represent 3 dimensions of data. 1: x axis (0-6) 2: y axis (0-6) 3: Category lets say (A,B,C - H)

If two x and y values are the same I want a pie chart to be in that position representing that Category. Similar to the graph seen in this link: https://matplotlib.org/gallery/lines_bars_and_markers/scatter_piecharts.html#sphx-glr-gallery-lines-bars-and-markers-scatter-piecharts-py

or this image from Tableu: [![enter image description here][1]][1]

As I am limited to only use python I have been struggling to manipulate the code to work for me. Could anyone help me with this problem? I would very grateful!

Example data:

XVAL    YVAL    GROUP
1.3      4.5    A
1.3      4.5    B
4          2    E
4          6    A
2          4    A
2          4    B
1          1    G
1          1    C
1          2    B
1          2    D
3.99    4.56    G

The final output should have 6 pie charts on the X & Y with 1 containing 3 groups and 2 containing 3 groups.

My attempt:

import pandas as pd 
import matplotlib.pyplot as plt
import numpy as np

def draw_pie(dist, 
             xpos, 
             ypos, 
             size, 
             ax=None):
    if ax is None:
        fig, ax = plt.subplots(figsize=(10,8))

    # for incremental pie slices
    cumsum = np.cumsum(dist)
    cumsum = cumsum/ cumsum[-1]
    pie = [0] + cumsum.tolist()

    for r1, r2 in zip(pie[:-1], pie[1:]):
        angles = np.linspace(2 * np.pi * r1, 2 * np.pi * r2)
        x = [0] + np.cos(angles).tolist()
        y = [0] + np.sin(angles).tolist()

        xy = np.column_stack([x, y])

        ax.scatter([xpos], [ypos], marker=xy, s=size)

    return ax

fig, ax = plt.subplots(figsize=(40,40))
draw_pie([Group],'xval','yval',10000,ax=ax)
draw_pie([Group], 'xval', 'yval', 20000, ax=ax)
draw_pie([Group], 'xval', 'yval', 30000, ax=ax)
plt.show()

Solution

  • I'm not sure how to get 6 pie charts. If we group on XVAL and YVAL, there are 7 unique pairs. You can do something down this line:

    fig, ax = plt.subplots(figsize=(40,40))
    for (x,y), d in df.groupby(['XVAL','YVAL']):
        dist = d['GROUP'].value_counts()
        draw_pie(dist, x, y, 10000*len(d), ax=ax)
    plt.show()
    

    Output:

    enter image description here