Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13981

Setting grouped barplot colour based on the catagory and not the grouping used with hue in Seaborn

$
0
0

I am trying to change how the colours are displayed when plotting a grouped barplot with seaborn.

In the following minimal example,you get the output you would expect, the colour being set on the group. However, I want the colour to be set based on the category.

import pandas as pdimport seaborn as snsimport matplotlib.pyplot as pltdata = [3, 5, 2, 4, 6, 1]group = [1, 2, 1, 2, 1, 2]category = ['a', 'a', 'b', 'b', 'c', 'c']data = pd.DataFrame(zip(category, group, data), columns=['category', 'group', 'data'])palette = sns.color_palette("pastel")fig, ax = plt.subplots(figsize=(15, 15))sns.barplot(data=data, x='category', y='data', palette=palette, hue='group', edgecolor='k', zorder=3, ax=ax)ax.grid(zorder=0)plt.tight_layout()plt.show()

enter image description here

I attempted to use a custom palette where each colour is repeated twice, but it still colours by the group and therefore displays with the same colour for all bars.

palette = [color for color in sns.color_palette("pastel") for _ in range(2)]

enter image description here

Is it possible to change the colouring to have the colour set on the category and not the group with Seaborn?


Viewing all articles
Browse latest Browse all 13981

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>