Seaborn散点图中palette参数值"ch:r=-.2,d=.3_r"含义解析
palette="ch:r=-.2,d=.3_r" in Seaborn's scatterplot Question Context
Here's the sample scatter plot code from the Seaborn gallery that sparked the question:
# From seaborn gallery - scatter plots import seaborn as sns import matplotlib.pyplot as plt sns.set(style="whitegrid") # Load the example diamonds dataset diamonds = sns.load_dataset("diamonds") # Draw a scatter plot while assigning point colors and sizes to different variables in the dataset f, ax = plt.subplots(figsize=(6.5, 6.5)) sns.despine(f, left=True, bottom=True) clarity_ranking = ["I1", "SI2", "SI1", "VS2", "VS1", "VVS2", "VVS1", "IF"] sns.scatterplot(x="carat", y="price", hue="clarity", size="depth", palette="ch:r=-.2,d=.3_r", hue_order=clarity_ranking, sizes=(1, 8), linewidth=0, data=diamonds, ax=ax)
The core question: What does the palette="ch:r=-.2,d=.3_r" parameter specifically mean? We know the _r suffix reverses the colormap, but need a full breakdown of the entire string.
Answer
Let’s break down every part of that ch:r=-.2,d=.3_r string so it makes total sense:
ch:Prefix: This tells Seaborn to use a Cubehelix colormap. Cubehelix is a smart colormap designed so that even when converted to grayscale, the colors still maintain a clear visual hierarchy. It’s perfect for visualizations that might be printed in black and white or viewed by people with color vision deficiencies.r=-.2: Rotation Parameter: Therstands for rotation. It controls how the hue progresses around the color wheel. A negative value like-0.2means we’re rotating the hue clockwise slightly from the default starting point (which is usually a warm reddish tone), shifting our colormap’s starting hue toward blue-purples. Positive values would rotate counterclockwise, pushing the hue toward warmer oranges and reds.d=.3: Darkness Parameter: Thedis short for darkness. This sets the range of brightness (lightness) across the colormap. A value of0.3gives us moderate contrast between the lightest and darkest colors—higherdvalues make the end of the gradient much darker, while lower values keep the colors more uniformly bright._rSuffix: As you guessed, this suffix reverses the entire colormap. Instead of the gradient progressing from light to dark (or the default hue order), it flips to dark to light. This can help emphasize different parts of your data depending on your visualization goals.
Content sourced from Stack Exchange, question author: Vim

