1

I want to use plotly to specify the color. However, with the current code, using marker=dict(color=colors) will naturally result in an error. How can I specify the type and also the color at the same time?

import plotly.express as px

fig = px.scatter(
    df,
    x=x_axis,
    y=y_axis,
    color="species",
)
fig.show()

As an image, I want to specify something like this. color is a list, and it contains the same kind of data as species.

import seaborn as sns

def get_colorpalette(colorpalette, file_number):
    palette = sns.color_palette(
        colorpalette, file_number)
    rgb = ['rgb({},{},{},{})'.format(*[x*256 for x in rgb],1)
           for rgb in palette]
    return rgb

colors = get_colorpalette('hls', graphNumber)

fig = px.scatter(
    df,
    x=x_axis,
    y=y_axis,
    color="species",
    marker=dict(color=colors)
)
fig.show()

postscript I have prepared color in RGB, but it would be nice if I could specify the color in RGB in plotly.

The following is an example of how to specify a color in plotly.

import numpy as np
import seaborn as sns

import plotly.graph_objs as go 
import plotly.offline as py
import plotly

def get_colorpalette(colorpalette, n_colors):
    palette = sns.color_palette(
        colorpalette, n_colors)
    rgb = ['rgb({},{},{})'.format(*[x*256 for x in rgb])
           for rgb in palette]
    return rgb

n_legends = 12
x = np.arange(0, 1, .01)
y = np.random.rand(n_legends, 100) + \
  np.arange(n_legends).reshape(-1, 1)

colors = get_colorpalette('hls', n_legends)
data = [
    go.Scatter(
        x=x, y=y[i], name='凡例 {:02d}'.format(i),
        marker={'color':colors[i]})
    for i in range(n_legends)]
fig = go.Figure(data=data)
py.iplot(fig)
18aaa
  • 35
  • 1
  • 7
  • and what is the error? – aSaffary May 27 '21 at 05:20
  • `NameError Traceback (most recent call last) in ----> 1 colors = get_colorpalette('hls', graphNumber*2) 2 import plotly.express as px 3 import plotly.graph_objects as go 4 5 # fig = go.Figure( NameError: name 'get_colorpalette' is not defined` – 18aaa May 27 '21 at 07:21

2 Answers2

1

Arbitrary color settings can be made with the following settings See this page for details. The following code was taken from the sample in the official reference.

import plotly.express as px
df = px.data.iris()

fig = px.scatter(df,
                 x="sepal_width",
                 y="sepal_length",
                 color="species",
                 size='petal_length',
                 hover_data=['petal_width'],
                 color_discrete_sequence=["blue", "goldenrod", "magenta"])
fig.show()

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • If my answer helped you, please consider accepting it as the correct answer. GANBATTE! – r-beginners May 27 '21 at 05:12
  • Thank you for your answer. I'm sorry, I didn't have enough information about `color`, so I supplemented it. I would be grateful if it could be specified in RGB. – 18aaa May 27 '21 at 07:17
  • Where color is specified, color name, hexadecimal format, and rgb() are also possible. So the following code is also valid. `color_discrete_sequence=["rgb(40, 40, 40)", "rgb(80, 80, 80)", "rgb(120, 120, 120)"]` – r-beginners May 27 '21 at 07:29
0

By default, Plotly Express will use the color sequence from the active template's layout. colorway attribute, and the default active template is plotly which uses the plotly color sequence. You can choose any of the following built-in qualitative color sequences from the px. colors.

Dharman
  • 30,962
  • 25
  • 85
  • 135