has2k1 / plotnine

A Grammar of Graphics for Python
https://plotnine.org
MIT License
4k stars 213 forks source link

Breaks and labels in scale_color_identity don't match. #735

Closed bmorledge-hampton19 closed 9 months ago

bmorledge-hampton19 commented 9 months ago

The order of the breaks and labels parameters for scale_color_identity are not being matched properly. Here is a reproducible example:

import pandas
from plotnine import *

df = pandas.DataFrame({'x': [1, 2, 3, 4, 5, 6, 7],
                       'y': [18, 22, 19, 14, 14, 11, 20],
                       "color": ["blue", "red", "blue", "blue", "red", "red", "blue"]})

(
    ggplot(df, aes(x = 'x', y = 'y', color = "color")) + geom_point() +
    scale_color_identity(breaks = ("red", "blue"), labels = ("Red", "Blue"),
                         guide = "legend", name = "")
)

This code produces the following plot with mismatched labels in the legend. output

It looks like a similar issue was addressed in the past (#445), but the issue still persists here.

On a side note, why does the legend use a default title when an empty string is passed as the name? This seems like strange behavior to me and isn't clearly documented. (The documentation states: "Name used as the label of the scale. This is what shows up as the axis label or legend title. Suitable defaults are chosen depending on the type of scale.")