Customizing color
1. Customizing color
Let's learn how to customize colors in our plots.2. Customization in general
First, let's discuss customization in general. We can customize plots during their creation, using available arguments such as the color argument we will explore in this video. Once the figure is created, we can refine it using the update_layout() function, which takes a dictionary of layout elements - like updating the title.3. Why customize color?
Customizing the color of your plots is a powerful method that can assist with both; Making plots look awesome for cosmetic and stylistic purposes And conveying insights for analytical purposes. For example, in this scatterplot we will make in a future lesson, adding colors based on species adds a third dimension to analyze!4. Some color theory
Computers use RGB encoding to specify colors. It is created from three digits between 0 to 255 that mixes Red, Green and Blue together - like blending paint. For example, 0 0 255 is blue, and 255 255 0 is yellow. Read more on color codes at this link.5. Specifying colors in plotly.express
Many plotly.express figures have a color argument. This is the column to use for colors, not the color itself. Every category in this column is automatically given a color. A color scale is created if you specify a numerical column. Here's our bar chart from a previous lesson, but with color added for the students' city.6. Our colors revealed
Here is the plot before. Now, we add "color", and each city has a different color. Red and Blue are automatically chosen for us.7. Color with univariate plots
Beware using the plotly express color argument with univariate plots as this can produce interesting results. Histograms appear with stacked bars. And box plots produce side-by-side box plots.8. Specific colors in plotly.express
Let's say we didn't want red and blue or the automatic colors plotly provides but specific colors. The argument color_discrete_map should be a dictionary that maps categorical values to colors. Note that you can use simple color names like "red", but we'll stick to RGB codes for consistency.9. Our specific colors
Let's use some specific colors: sandy yellow for Sydney's beaches and navy blue for Melbourne, the official state color. The setup is similar to before. We specify a dictionary to map each city to a specific RGB color. We also need to specify which DataFrame column to use this map with, the city column. Now, we get a plot with our specified colors. Nice stuff.10. Color scales in plotly.express
You might want a single range (like light to dark green), or a blended gradient (like green into blue). The argument color_continuous_scale allows us to do this with built-in or constructed color scales.11. Using built-in color scales
Let's see a built-in color range. We set the numerical column, temp, as the color argument and use the "inferno" built-in scale. Here is our plot - looks nice! There are many other built-in scales available.12. Constructing our own color range
Let's make a color scale with yellow through orange to red at the hottest temperature. We define a list of RGB codes - yellow to orange to red - to use in our plot. The top of the code is familiar, creating the bar chart. Now, instead of using a built-in scale, we use our list of rgb codes for the color_continuous_scale. This is the plot produced. A simple but powerful color scale of yellow, orange, and red.13. Let's practice!
Let's practice adding color to some plots.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.