Friday, 5 February 2021

Consistent colour across Matplotlib Groupby Graph

I am attempting to create a scatter pie plot that groups by 2 columns, Column1 & Column 2 where the colour in the pie (if numbers are the same) is decided by Column 3.

See my example below of where I am: enter image description here

This graph shows Column 1 (y-axis) and Column 2 (x-axis). The colour is dictated by Column 3.

But with the code I use the colours do not stay consistent across graphs and if the same Column 3 appears with a different Column 2 or Column 3 value it assigns it a different colour.

I have attempted using cmaps and manually assigning colours but I cannot keep it consistent across each column 2.

See my current code below:

import pandas as pd 
import matplotlib.pyplot as plt
import matplotlib.ticker as mticks
from matplotlib.font_manager import FontProperties
import numpy as np

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

    # 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,c=color)

    return ax


colors = {'Group A':'red', 'Group B':'green', 'Group C':'blue', 'Group D':'yellow', 'Group E':'yellow', 'Group F':'yellow', 'Group G':'yellow', 'Group H':'yellow'}


fig, ax = plt.subplots(figsize=(94,70))
for (x,y), d in dataset.groupby(['Column 1','Column 2']):
    dist = d['Column 3'].value_counts()
    draw_pie(dist, x, y, 50000, ax=ax,color=dataset['Column 3'].map(colors))


params = {'legend.fontsize': 100}
plt.rcParams.update(params)
#plt.legend(dataset["Column 3"],markerscale=.4,frameon=True,framealpha=1,ncol=3,loc=(0.00, -0.3), bbox_to_anchor=(0.0, 0., 0.5, 1.25),handletextpad=1,markerfirst=True,facecolor='lightgrey',mode='expand',borderaxespad=-16)


ax.yaxis.set_major_locator(mticks.MultipleLocator(1))
full = plt.Rectangle((-0.05, 4.25), 2.10, 2, color='g', alpha=0.15)
partial = plt.Rectangle((-0.05, 2.25), 2.10, 2, color='orange', alpha=0.15)
low = plt.Rectangle((-0.05, 0.25), 2.10, 2, color='r', alpha=0.15)
ax.add_patch(full)
ax.add_patch(partial)
ax.add_patch(low)
plt.xticks(fontsize=120)
plt.yticks(fontsize=100)
plt.ylim([0, 6.75]) 
plt.tight_layout()
plt.show()

Ideally the output graph based on the data (I will copy in below) should be like the below graph (I have placed a number in each pie to define what colour should be there)

enter image description here

Here is the full data used for the graph:

Column 1    3       2   Colour Group Desired
First Line  Group A 6   1
First Line  Group A 6   1
First Line  Group A 6   1
First Line  Group C 6   3
First Line  Group B 6   2
First Line  Group B 6   2
First Line  Group B 6   2
First Line  Group A 6   1
First Line  Group A 6   1
First Line  Group C 6   3
First Line  Group A 6   1
Second Line Group A 6   1
Second Line Group A 6   1
Second Line Group A 6   1
Second Line Group C 6   3
Second Line Group B 6   2
Second Line Group B 6   2
Second Line Group B 6   2
Second Line Group A 4.5 1
Second Line Group A 6   1
Second Line Group C 6   3
Second Line Group A 6   1
Third Line  Group A 1   1
Third Line  Group A 6   1
Third Line  Group A 1   1
Third Line  Group C 6   3
Third Line  Group B 3.5 2
Third Line  Group B 3.5 2
Third Line  Group B 3.5 2
Third Line  Group A 1   1
Third Line  Group A 1   1
Third Line  Group C 4   3
Third Line  Group A 1   1

Additionally I would like to add a label in each section of the pie with the count of distinct(Column 3).

Any help to get this done would be greatly appreciated!

This post is eligible for bounty tomorrow I would be happy to award it then if I can get help with both items!!

THANKS!



from Consistent colour across Matplotlib Groupby Graph

No comments:

Post a Comment