0

I'm trying to visualize if a group of people live in their birthplace or not in different years (pandas df). But the histplot colors are much darker than what I have specified as if there is a dark filter on the plot itself. The colors in the legend corresponds to what I assign. How can I fix this/what am I doing wrong? I am still new to python and visualizations in seaborn.

I have tried to specify both a color_palette, set.palette() and palette and to assign different colors, but the plot itself is always very dark.

Code:

#import packages
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

#Create dataset
d = {'ID': ['ID-1', 'ID-1', 'ID-1', 'ID-1', 'ID-1', 'ID-2', 'ID-2', 'ID-2', 'ID-2', 'ID-2', 'ID-3', 'ID-3', 'ID-3', 'ID-3', 'ID-3'],
     'year': ['birthyear', 'year_1', 'year_2', 'year_3', 'year_4', 'birthyear', 'year_1', 'year_2', 'year_3', 'year_4', 'birthyear', 'year_1', 'year_2', 'year_3', 'year_4'],
     'living_where': ['lives in birthplace', 'lives in birthplace', 'lives elsewhere', 'lives in birthplace', 'dead', 'lives in birthplace', 'lives elsewhere', 'lives elsewhere', 'lives elsewhere', 'lives elsewhere', 'lives in birthplace', 'lives elsewhere', 'lives elsewhere', 'dead', 'dead']}
df = pd.DataFrame(data=d)


#Create histplot
ax = sns.histplot(data=df, x="year", y='ID', hue="living_where", palette='pastel')

sns.move_legend(
    ax, "lower center",
    bbox_to_anchor=(.5, 1), ncol=4, title=None, frameon=False)


#Even if I specify a color to be white it becomes grey
pal = ['blue', 'white', 'red']

#Create histplot
ab = sns.histplot(data=df, x="year", y='ID', hue="living_where", palette=pal)

sns.move_legend(
    ab, "lower center",
    bbox_to_anchor=(.5, 1), ncol=4, title=None, frameon=False)

color palette set as 'pastel':

enter image description here

color palette set as 'blue, 'white' and 'red'

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
samnord
  • 1
  • 1
  • 1
    Maybe the colors look different due to transparency in overlapping areas? Maybe a 2D histplot isn't the best visualization for your discrete data? In the [docs](https://seaborn.pydata.org/generated/seaborn.histplot.html) a 2D histplot with only one color looks like [this](https://seaborn.pydata.org/_images/histplot_39_0.png) – JohanC Mar 30 '23 at 08:12
  • 1
    Maybe pivot the dataframe and use pandas styling instead: [see code and plot](https://i.stack.imgur.com/BPkxr.jpg). Styling as per [this answer](https://stackoverflow.com/a/69183301/7758804). – Trenton McKinney Mar 30 '23 at 20:08
  • @JohanC I think you are right. I've been suggested to use a categorical heatmap. And the dummy dataset works so far. Thank you for your comment. – samnord Apr 07 '23 at 08:45
  • @TrentonMcKinney It may work, however, it is not exactly the way of visualising I was going for. Thank you for your comment! – samnord Apr 07 '23 at 08:47

0 Answers0