Home > Back-end >  How can I match color palettes across different seaborn plots?
How can I match color palettes across different seaborn plots?

Time:12-17

I want to make a pie chart and a barplot (displot) that use the same colour label across both plots, but I don't know how to do that.

temp_df = pd.DataFrame([['d7d1b050-0e48-4c00-8061-c78817155b72',
  '42773088-e38f-4578-bc2a-69d1797a90eb',
  11,
  'groupA'],
 ['962c397d-a8f8-4f1c-a589-ecf74a7da62d',
  'b5baafb0-f6d4-4b4e-bc76-1287614b985d',
  10,
  'groupA'],
 ['a90fde40-9b9f-466e-bd5e-a40325b5fc9d',
  'b3fba007-aef5-4a5f-a53b-94eb0705d953',
  11,
  'groupB'],
 ['22ebb056-603c-4f66-8240-8c54e8043509',
  'b780fa66-addd-48c0-8db4-d755ebd351b8',
  10,
  'groupC'],
 ['52ffd64c-a5c1-4cd5-89c8-c1dcb8bd24b2',
  '37482cb7-c354-4b4b-92b6-2aaa62811e5b',
  10,
  'groupA'],
 ['55524169-f159-4c31-b939-bb00e1cba804',
  '34a9ff63-ea75-473d-ab89-9a92c3f4a8d9',
  10,
  'groupB'],
 ['2027d9d0-1e29-4d1f-969a-995a47f12052',
  '875488ea-85a2-47cb-b1ea-62003bbce80a',
  10,
  'groupA'],
 ['10d9c9fb-b5dd-4581-b148-a6198abecec1',
  '3f4b0604-513a-424b-98a3-e788ab3daa97',
  11,
  'groupD'],
 ['1c1e183b-6459-41bd-99aa-5f89b375006a',
  '53dd2ffd-c9b0-49c3-9275-190716c78799',
  10,
  'groupB'],
 ['31030ded-64a7-4854-8042-585605141e71',
  'f0514527-2d7b-4cad-a36f-f21e3425093c',
  10,
  'groupD'],
 ['cdfd5a0c-dd8c-4546-ba31-c2f021fb4859',
  '1ed007fe-d4f7-41bc-8f3c-b163c57f8a1f',
  11,
  'groupE'],
 ['66bd16a5-b514-4d8a-ad7a-afb8921f7dd2',
  'a2e9f137-bba5-46ec-8b13-7b17821de735',
  10,
  'groupB'],
 ['3cdb21d9-be3c-4723-bf28-0a7769d492b4',
  '9a6f1516-54a0-4dda-83d7-e05311e87ff5',
  10,
  'groupE'],
 ['d25f4cb2-3bf7-4898-a8a3-91d9e1b58576',
  '716a7732-6bcd-478d-87f9-c13cd83eaf66',
  11,
  'groupA'],
 ['e95134fd-7ce2-4e88-808c-e5abf13a4892',
  'c021c21b-7bab-4e1f-9ff0-4dfc584263b8',
  11,
  'groupE'],
 ['e13da005-1033-466f-b984-48fdfa0988f2',
  '5bcc0651-0775-4fa5-b521-ac90e0a33b1c',
  10,
  'groupB'],
 ['b60ee53d-e4fc-4e37-aa1c-df67f66e304e',
  '592adca4-6fa6-48c3-be97-2357250d736d',
  10,
  'groupD'],
 ['c1d47246-838f-418a-a92d-7b5150122775',
  'ff5d180c-cca9-474a-974e-e18c35cab912',
  10,
  'groupA'],
 ['fc129686-f7cd-407a-aca3-68f86c52af41',
  'a18dfc3a-2ce6-43f7-a21f-4c7371cff2b6',
  11,
  'groupE'],
 ['191af645-cb9e-408a-af2e-b6826f7177b9',
  'd430610b-b7da-42cb-aa93-c7f94774093c',
  10,
  'groupA']])

temp_df.columns = ['clickId', 'sessionId', 'month','group']

temp_df.groupby(['group']).agg({'sessionId':'count'}).plot.pie(y='sessionId');

sns.displot(data=temp_df[['sessionId', 'month', 'group']].drop_duplicates(), x='date', hue='group',  stat='count', multiple="stack")

Those two plots will use different color palettes, and what I would like is for each level within group to have the same colour for both the displot and the pie chart.

Any idea how I would do that?

CodePudding user response:

You could use the same colors (plt.cm.tab10) in both cases. You might want to set alpha to 1. Note that you need discrete=True for the histogram.

To make sure that the same order is used everywhere, temp_df['group'] = pd.Categorical(temp_df['group']) can be used. Or temp_df['group'] = pd.Categorical(temp_df['group'], ['groupA', 'groupB', 'groupC', 'groupD', 'groupE']) if the exact ordering is important. By default, the order of appearance in the dataframe (or in the subset) is used.

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import seaborn as sns
import pandas as pd
import numpy as np

temp_df = pd.DataFrame([
    ['d7d1b050-0e48-4c00-8061-c78817155b72', '42773088-e38f-4578-bc2a-69d1797a90eb', 11, 'groupA'],
    ['962c397d-a8f8-4f1c-a589-ecf74a7da62d', 'b5baafb0-f6d4-4b4e-bc76-1287614b985d', 10, 'groupA'],
    ['a90fde40-9b9f-466e-bd5e-a40325b5fc9d', 'b3fba007-aef5-4a5f-a53b-94eb0705d953', 11, 'groupB'],
    ['22ebb056-603c-4f66-8240-8c54e8043509', 'b780fa66-addd-48c0-8db4-d755ebd351b8', 10, 'groupC'],
    ['52ffd64c-a5c1-4cd5-89c8-c1dcb8bd24b2', '37482cb7-c354-4b4b-92b6-2aaa62811e5b', 10, 'groupA'],
    ['55524169-f159-4c31-b939-bb00e1cba804', '34a9ff63-ea75-473d-ab89-9a92c3f4a8d9', 10, 'groupB'],
    ['2027d9d0-1e29-4d1f-969a-995a47f12052', '875488ea-85a2-47cb-b1ea-62003bbce80a', 10, 'groupA'],
    ['10d9c9fb-b5dd-4581-b148-a6198abecec1', '3f4b0604-513a-424b-98a3-e788ab3daa97', 11, 'groupD'],
    ['1c1e183b-6459-41bd-99aa-5f89b375006a', '53dd2ffd-c9b0-49c3-9275-190716c78799', 10, 'groupB'],
    ['31030ded-64a7-4854-8042-585605141e71', 'f0514527-2d7b-4cad-a36f-f21e3425093c', 10, 'groupD'],
    ['cdfd5a0c-dd8c-4546-ba31-c2f021fb4859', '1ed007fe-d4f7-41bc-8f3c-b163c57f8a1f', 11, 'groupE'],
    ['66bd16a5-b514-4d8a-ad7a-afb8921f7dd2', 'a2e9f137-bba5-46ec-8b13-7b17821de735', 10, 'groupB'],
    ['3cdb21d9-be3c-4723-bf28-0a7769d492b4', '9a6f1516-54a0-4dda-83d7-e05311e87ff5', 10, 'groupE'],
    ['d25f4cb2-3bf7-4898-a8a3-91d9e1b58576', '716a7732-6bcd-478d-87f9-c13cd83eaf66', 11, 'groupA'],
    ['e95134fd-7ce2-4e88-808c-e5abf13a4892', 'c021c21b-7bab-4e1f-9ff0-4dfc584263b8', 11, 'groupE'],
    ['e13da005-1033-466f-b984-48fdfa0988f2', '5bcc0651-0775-4fa5-b521-ac90e0a33b1c', 10, 'groupB'],
    ['b60ee53d-e4fc-4e37-aa1c-df67f66e304e', '592adca4-6fa6-48c3-be97-2357250d736d', 10, 'groupD'],
    ['c1d47246-838f-418a-a92d-7b5150122775', 'ff5d180c-cca9-474a-974e-e18c35cab912', 10, 'groupA'],
    ['fc129686-f7cd-407a-aca3-68f86c52af41', 'a18dfc3a-2ce6-43f7-a21f-4c7371cff2b6', 11, 'groupE'],
    ['191af645-cb9e-408a-af2e-b6826f7177b9', 'd430610b-b7da-42cb-aa93-c7f94774093c', 10, 'groupA']],
    columns=['clickId', 'sessionId', 'month', 'group'])

fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(14, 6))

temp_df['group'] = pd.Categorical(temp_df['group'], ['groupA', 'groupB', 'groupC', 'groupD', 'groupE'])
temp_df.columns = ['clickId', 'sessionId', 'month', 'group']
temp_df.groupby(['group']).agg({'sessionId': 'count'}).plot.pie(y='sessionId', ax=ax1)

sns.histplot(data=temp_df[['sessionId', 'month', 'group']].drop_duplicates(),
             x='month', hue='group', palette='tab10', alpha=1,
             stat='count', discrete=True, multiple="stack", shrink=0.8, ax=ax2)
ax2.xaxis.set_major_locator(MultipleLocator(1))
sns.despine(ax=ax2)
plt.tight_layout()
plt.show()

pie plot and histogram with same colors

  • Related