question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Could we access plotly.js default colors in plotly.py ?

See original GitHub issue

I like very much plotly’s default color scale. However, I am not aware of an official way to reproduce it when I want multiple traces to be of the same color. For now I use the explicit list from https://stackoverflow.com/a/44727682, which I have coded into a function (see below). Is there a recommended way to enforce two traces to be of the same color ? Would you consider including in plotly.py the default color scale ? Thanks

from itertools import cycle

def plotly_color_map(names):
    # From https://stackoverflow.com/a/44727682
    plotly_colors = cycle(['#1f77b4',  # muted blue
                           '#ff7f0e',  # safety orange
                           '#2ca02c',  # cooked asparagus green
                           '#d62728',  # brick red
                           '#9467bd',  # muted purple
                           '#8c564b',  # chestnut brown
                           '#e377c2',  # raspberry yogurt pink
                           '#7f7f7f',  # middle gray
                           '#bcbd22',  # curry yellow-green
                           '#17becf'  # blue-teal
                           ])

    return dict(zip(names, plotly_colors))

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

13reactions
jonmmeasecommented, Aug 24, 2018

Turns out this has been in plotly.py all along, but I just learned about it today 🙂

>>> from plotly.colors import DEFAULT_PLOTLY_COLORS
>>> DEFAULT_PLOTLY_COLORS
['rgb(31, 119, 180)',
 'rgb(255, 127, 14)',
 'rgb(44, 160, 44)',
 'rgb(214, 39, 40)',
 'rgb(148, 103, 189)',
 'rgb(140, 86, 75)',
 'rgb(227, 119, 194)',
 'rgb(127, 127, 127)',
 'rgb(188, 189, 34)',
 'rgb(23, 190, 207)']

Can you confirm that this is what you were looking for?

1reaction
mwoutscommented, Jun 4, 2018

Hello @jmmease, yes certainly I could import the colorscale from the colorlover package, that would already be more generic than copying/pasting stackoverflow.

I like the colorway layout option, thanks for sharing.

Finally, I would appreciate to simplify the task of giving same colors to traces that have identical names. Would you have recommendations on how to make the below shorter ?

import plotly.graph_objs as go
from plotly.offline import iplot, init_notebook_mode
init_notebook_mode()

cm = plotly_color_map(['trace1', 'trace2']) # function defined in previous post

fig = go.Figure(
    data=[{'y': [1, 2, 3], 'name':'trace1', 'legendgroup':'trace1',
           'line':{'color': cm['trace1']}},
          {'y': [2, 1, 3], 'name':'trace2', 'legendgroup':'trace2',
              'line':{'color': cm['trace2']}},
          {'y': [1, 2, 3], 'name':'trace1', 'legendgroup':'trace1',
              'showlegend':False, 'xaxis':'x2',
           'line':{'color': cm['trace1']}},
          {'y': [1, 3, 2.5], 'name': 'trace2', 'legendgroup': 'trace2',
           'showlegend': False, 'xaxis': 'x2', 'line': {'color': cm['trace2']}}],
    layout={'xaxis': {'domain': [0, .45]},
            'xaxis2': {'domain': [.55, 1]}}
)
iplot(fig)

newplot-4

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get Plotly.js default colors list? - Stack Overflow
According to the source code, the default color list is: '#1f77b4', // muted blue '#ff7f0e', // safety orange '#2ca02c', // cooked asparagus ...
Read more >
Discrete colors in Python - Plotly
This page is about using color to represent categorical data using discrete colors, but Plotly can also represent continuous values with color.
Read more >
Introducing plotly.py 3.0.0 | by Plotly | Plotly | Medium
plotly.js graphs are declarative. This means that every aspect of the charts (the colors of the bars, the axes grid lines, the position...
Read more >
Python Plotly: How to set up a color palette? - GeeksforGeeks
In this article, we will discuss how to explicitly set up a color sequence/color palette in plotly. Generally, we use built-in color ......
Read more >
plotly package — 5.11.0 documentation - Plotly Help Center
Plotly's Python API allows users to programmatically access Plotly's server resources ... For example, to specify the annotation text “example” you can pass ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found