plotnine
plotnine copied to clipboard
Too many line types results in unclear exception.
(this is on current git head 52e33218b86165d90f5313d57c22465223219e98 )
import pandas as pd
import plotnine as p9
import numpy as np
line_type_count = 5
df = pd.DataFrame(
{
"x": np.random.random(size=100),
"y": np.random.random(size=100),
"lt": (["a_" + str(x) for x in range(line_type_count)] * 200)[:100],
}
)
import plotnine as p9
g = p9.ggplot(df)
g += p9.geom_line(p9.aes(x="x", y="y", group="lt", linetype="lt"))
g
is answered with a couple of warnings:
/ssd/upstream/dev/lib/python3.7/site-packages/mizani/palettes.py:683: UserWarning: Palette can return a maximum of 4 values. 5 were requested from it.
warnings.warn(msg.format(max_n, n))
/ssd/upstream/plotnine_dev/linestyle_errors/plotnine/geoms/geom_path.py:75: PlotnineWarning: geom_path: Removed 20 rows containing missing values.
warn(msg.format(n1-n2), PlotnineWarning)
/ssd/upstream/dev/lib/python3.7/site-packages/mizani/palettes.py:683: UserWarning: Palette can return a maximum of 4 values. 5 were requested from it.
warnings.warn(msg.format(max_n, n))
which could be improved by actually naming the Palette in question, and and exception
ValueError: Unrecognized linestyle: nan
somewhere in guides (it's a long traceback).
This is not particularly helpful.
For comparison, scale_color_brewer(type='qual') degrades gracefully (warnings, grey for additional levels), scale_color_manual dies with a similarly unhelpful message: "ValueError: Invalid RGBA argument: nan", not in guides though.
Can we fix this globally, or do we have to approach each scale by itself?
Also we should unify the approaches, either filter the values (and warn), raise an appropriate exception (possibly with a much shorter stack-trace?) or degrade the plot and warn (as scale_color_brewer(type='qual') seems to do it).
Good
scale_fill_cmap_d: "ValueError: cmap Pastel1 has 5 colors you requested 10 colors."
bad: scale_alpha_manual: ValueError: cannot convert float NaN to integer scale_fill_manual: ValueError: Invalid RGBA argument: nan
don't know: default shape scale: do not show points beyond the scale (unlinke brewer, this omits data, albeit with a warning) scale_shape_manual - see default shape scale (unlike the other manual scales, this one does not raise an Exception) scale_size_manual - same behavour as the shape scales.
Personally, I think cmap_d has it right, throw an exception with an appropriate text. Bonus if we can get the aesthetic referenced in it.
Dropping data with warning is fine for interactive use, but bad for non-interactive plotting where the graphs are produced 'for later'.