#!/usr/bin/env python # coding: utf-8 # # New `Scale` Functions with Parameter `aesthetic` # # - `scale_identity(aesthetic, *, ...)` # - `scale_manual(aesthetic, values, *, ...)` # - `scale_continuous(aesthetic, *, ...)` # - `scale_gradient(aesthetic, *, ...)` # - `scale_gradient2(aesthetic, *, ...)` # - `scale_gradientn(aesthetic, *, ...)` # - `scale_hue(aesthetic, *, ...)` # - `scale_discrete(aesthetic, *, ...)` # - `scale_grey(aesthetic, *, ...)` # - `scale_brewer(aesthetic, *, ...)` # - `scale_viridis(aesthetic, *, ...)` # # Comparing to familiar "scale" functions like `scale_color_gradient()` etc., the new set of functions # adds more flexibility by allowing specifying an aesthetic or a list of aesthetics the scale is working with. # # For example, you can use just one function call to setup the same color palette for both, stroke and fill colors on plot: # # `scale_brewer(['color', 'fill'], palette='Set1')` # # # But, the main reason why you might want to use new "scale" functions is configuring of additional color aesthetics: `paint_a, paint_b, paint_c`. # # These aesthetics are flexible and can be used as either "color" or "fill" as needed. See [Multiple Color Scales](https://nbviewer.org/github/JetBrains/lets-plot/blob/master/docs/f-23a/multiple_color_scales.ipynb). # # In[1]: import pandas as pd from lets_plot import * from lets_plot.mapping import as_discrete # In[2]: LetsPlot.setup_html() # In[3]: mpg_df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv") # #### 1. Plot with Default Colors # In[4]: p = ggplot(mpg_df, aes(as_discrete('drv', order=-1), 'hwy')) + \ geom_violin(aes(color='drv', fill='drv'), alpha=.5, size=2) p # #### 2. Setup a Brewer Palette # ##### 2.1. Old School: for Each Aesthetic Separately # In[5]: p + scale_color_brewer(palette='Set1') + scale_fill_brewer(palette='Set1') # ##### 2.2. New: for Both Aesthetics at Once # In[6]: p + scale_brewer(['color', 'fill'], palette='Set1')