In this article, we will learn how to hide the colorbar and legend in plotly express in Python. Color bar are gradients that go from light to dark or the other way round. They are great for visualizing datasets that goes from low to high, like income, temperature, or age.
Stepwise Implementation
Step 1:
Import all the required packages.
# import the modules import inspect
import plotly.express as px
from textwrap import fill
|
Step 2:
Here we will get all the individual color-scale names by using the inspect module while iterating over the color module.
# iterating over color module colorscale_names = []
colors_modules = [ 'carto' , 'colorbrewer' , 'cmocean' , 'cyclical' ,
'diverging' , 'plotlyjs' , 'qualitative' , 'sequential' ]
for color_module in colors_modules:
colorscale_names.extend([name for name, body
in inspect.getmembers( getattr (px.colors, color_module))
if isinstance (body, list )])
|
Complete Code:
# import the modules import inspect
import plotly.express as px
from textwrap import fill
# iterating over color module colorscale_names = []
colors_modules = [ 'carto' , 'cmocean' , 'cyclical' ,
'diverging' , 'plotlyjs' , 'qualitative' , 'sequential' ]
for color_module in colors_modules:
colorscale_names.extend([name for name, body
in inspect.getmembers( getattr (px.colors, color_module))
if isinstance (body, list )])
print (fill(' '.join(sorted({f' {x: <{ 15 }}' for x in colorscale_names})), 75 ))
|
Output:
The difference between Aggrnyl and Aggrnyl_r is that it will show the reversed scale i.e Aggrnyl (light to dark) Aggrnyl_r(dark to light). To understand it more clearly below in the examples.
Note: Some color-scale names may not work due to version control.
Aggrnyl Aggrnyl_r Agsunset Agsunset_r Alphabet Alphabet_r Antique Antique_r Armyrose Armyrose_r Blackbody Blackbody_r Bluered Bluered_r Blues Blues_r Blugrn Blugrn_r Bluyl Bluyl_r Bold Bold_r BrBG BrBG_r Brwnyl Brwnyl_r BuGn BuGn_r BuPu BuPu_r Burg Burg_r Burgyl Burgyl_r Cividis Cividis_r D3 D3_r Dark2 Dark24 Dark24_r Dark2_r Darkmint Darkmint_r Earth Earth_r Edge Edge_r Electric Electric_r Emrld Emrld_r Fall Fall_r G10 G10_r Geyser Geyser_r GnBu GnBu_r Greens Greens_r Greys Greys_r HSV HSV_r Hot Hot_r IceFire IceFire_r Inferno Inferno_r Jet Jet_r Light24 Light24_r Magenta Magenta_r Magma Magma_r Mint Mint_r OrRd OrRd_r Oranges Oranges_r Oryel Oryel_r PRGn PRGn_r Pastel Pastel1 Pastel1_r Pastel2 Pastel2_r Pastel_r Peach Peach_r Phase Phase_r PiYG PiYG_r Picnic Picnic_r Pinkyl Pinkyl_r Plasma Plasma_r Plotly Plotly3 Plotly3_r Plotly_r Portland Portland_r Prism Prism_r PuBu PuBuGn PuBuGn_r PuBu_r PuOr PuOr_r PuRd PuRd_r Purp Purp_r Purples Purples_r Purpor Purpor_r Rainbow Rainbow_r RdBu RdBu_r RdGy RdGy_r RdPu RdPu_r RdYlBu RdYlBu_r RdYlGn RdYlGn_r Redor Redor_r Reds Reds_r Safe Safe_r Set1 Set1_r Set2 Set2_r Set3 Set3_r Spectral Spectral_r Sunset Sunset_r Sunsetdark Sunsetdark_r T10 T10_r Teal Teal_r Tealgrn Tealgrn_r Tealrose Tealrose_r Temps Temps_r Tropic Tropic_r Turbo Turbo_r Twilight Twilight_r Viridis Viridis_r Vivid Vivid_r YlGn YlGnBu YlGnBu_r YlGn_r YlOrBr YlOrBr_r YlOrRd YlOrRd_r __all__ _cols algae algae_r amp amp_r balance balance_r curl curl_r deep deep_r delta delta_r dense dense_r gray gray_r haline haline_r ice ice_r matter matter_r mrybm mrybm_r mygbm mygbm_r oxy oxy_r phase phase_r scale_pairs scale_pairs_r scale_sequence scale_sequence_rsolar solar_r speed speed_r tempo tempo_r thermal thermal_r turbid turbid_r
Example 1:
In this example, we are selecting color-scale as colorscale = “Agsunset” in Plotly Express, this will select Aggrnyl colorscale from the inbuilt plotly library.
import plotly.graph_objects as go
import numpy as np
fig = go.Figure(data = go.Scatter(
y = np.random.randn( 500 ),
mode = 'markers' ,
marker = dict (
size = 8 ,
color = np.random.randn( 550 ), # set color equal to a variable
colorscale = 'Agsunset' , # one of plotly colorscales
showscale = True
)
)) fig.update_layout( margin = dict (l = 12 , r = 5 , t = 20 , b = 20 ),
paper_bgcolor = "LightSteelBlue" ,
) fig.show() |
Output:
Example 2:
In this example, we are selecting color-scale as colorscale = “Agsunset_r” (r stand for reverse) in Plotly Express, this will select reverse Aggrnyl_r colorscale from the inbuilt plotly library. The only difference between both is that it will show the reversed scale i.e Aggrnyl_r(dark to light) and Aggrnyl (light to dark).
import plotly.graph_objects as go
import numpy as np
fig = go.Figure(data = go.Scatter(
y = np.random.randn( 500 ),
mode = 'markers' ,
marker = dict (
size = 8 ,
color = np.random.randn( 550 ), # set color equal to a variable
colorscale = 'Agsunset_r' , # reverse Agsunset colorscales
showscale = True
)
)) fig.update_layout( margin = dict (l = 12 , r = 5 , t = 20 , b = 20 ),
paper_bgcolor = "LightSteelBlue" ,
) fig.show() |
Output: