Skip to content

Reinstate ability to set colour palette for slopegraph and delta dots in paired plots for non-group column #218

@mlotinga

Description

@mlotinga

Is your feature request related to a problem? Please describe.
In earlier versions of dabest-python, it was possible to simultaneously set the color_col argument to a column in the data (that does not define the groups being compared) alongside a custom_palette. Now setting both of these results in an a KeyError. For example, using the setup from from https://acclab.github.io/DABEST-python/tutorials/08-plot_aesthetics.html:

from scipy.stats import norm # Used in generation of populations.

np.random.seed(9999) # Fix the seed to ensure reproducibility of results.

Ns = 20 # The number of samples taken from each population

# Create samples
c1 = norm.rvs(loc=3, scale=0.4, size=Ns)
c2 = norm.rvs(loc=3.5, scale=0.75, size=Ns)
c3 = norm.rvs(loc=3.25, scale=0.4, size=Ns)

t1 = norm.rvs(loc=3.5, scale=0.5, size=Ns)
t2 = norm.rvs(loc=2.5, scale=0.6, size=Ns)
t3 = norm.rvs(loc=3, scale=0.75, size=Ns)
t4 = norm.rvs(loc=3.5, scale=0.75, size=Ns)
t5 = norm.rvs(loc=3.25, scale=0.4, size=Ns)
t6 = norm.rvs(loc=3.25, scale=0.4, size=Ns)


# Add a `gender` column for coloring the data.
females = np.repeat('Female', Ns/2).tolist()
males = np.repeat('Male', Ns/2).tolist()
gender = females + males

# Add an `id` column for paired data plotting.
id_col = pd.Series(range(1, Ns+1))

# Combine samples and gender into a DataFrame.
df = pd.DataFrame({'Control 1' : c1,     'Test 1' : t1,
                   'Control 2' : c2,     'Test 2' : t2,
                   'Control 3' : c3,     'Test 3' : t3,
                   'Test 4'    : t4,     'Test 5' : t5, 'Test 6' : t6,
                   'Gender'    : gender, 'ID'  : id_col
                  })

np.random.seed(9999) # Fix the seed so the results are replicable.

two_groups_paired = dabest.load(df, idx=("Control 1", "Test 1"), paired="baseline", id_col="ID")

two_groups_paired.mean_diff.plot(color_col="Gender");  # works: default colour palette separates the slopes and delta dots into colours by male/female
two_groups_paired.mean_diff.plot(custom_palette=['red', 'blue']);  # works: colors the delta violin 'blue' (not desired)
two_groups_paired.mean_diff.plot(color_col="Gender", custom_palette=['red', 'blue']);  # fails KeyError: 'Test 1'

Describe the solution you'd like
In previous versions of dabest-python, it was possible to make a plot like the one below, using both color_col and custom_palette together:

Image

Here, the paired groups are Modelled and Scaled, while color_col is used to highlight another variable, using a custom_palette.

The problem seems to be that the use of custom_palette now appears to be focussed on colouring the paired groups instead of allowing its use to highlight another distinction within the data. It seems like this may have been a consequence of the modifications made to address #207. Could the feature to colour by another column (outside the paired groups variable) using a custom palette be reinstated somehow? This is a very useful way to highlight data features, and the flexibility of using custom colours for it is very handy. The option to use custom colours for paired groups is purely aesthetic as far as I can tell (since the groups are already spatially distinguished), and doesn't really add any data analysis value?

dabest-python==2025.10.20

I have also tried using 'color_col' and 'custom_palette' as dict keys inside slopegraph_kwargs, but they aren't recognised. Is there any another existing solution?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions