Plotly: How to use the same color scheme on two subplots?

你。 提交于 2020-04-10 03:32:37

问题


I am trying to plot two scatter subplots with plotly whereby the colour automatically chosen in the two sub-plots is the same, but I cannot find a way to do so.

Following is a code snippet:

import plotly as py
import plotly.tools as to
import numpy as np
import plotly.graph_objs as go
fig=to.make_subplots(rows=1,cols=2,subplot_titles=['Plot 1','Plot 2'])
X=[0,50,100,150,200,250,300,350,400,450,500]
Y=[1,2,3,4,5,6,7,8,9,10,11]
Z=Y.copy()
for i  in np.arange(11):
    if i==0:
            Z[0]=Y[0]
    else:
            Z[i]=Y[i]+Z[i-1]
trace=go.Scatter(x=X,y=Y)
fig.append_trace(trace,1,1)
trace=go.Scatter(x=X,y=Z,showlegend=False)
fig.append_trace(trace,1,2);
py.offline.plot(fig)

The above code plots two subplots and the scatters will have a different colour. I want them to have the same colour. I am aware that I can hard-code the colour on the two scatters but I need the colours to be selected automatically from some colour scheme.

How can I achieve what I need?


回答1:


The following suggestion uses plotly verion 4.2.0, but the approach regarding the colors should be the same.

You can set the color of your lines in markers in your subplots using:

line=dict(width=2, color=cols[0])
marker=dict(color=cols[1])

If you'd like to work with a certain color scheme you can pick up the default plotly colors using:

#in:
import plotly
cols = plotly.colors.DEFAULT_PLOTLY_COLORS

#out:
['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)']

Here's an example that uses the same colors from the same color scheme on the lines, and a bit different colors on the markers.

Plot:

Code:

# imports
from plotly.subplots import make_subplots
import plotly.graph_objs as go
import pandas as pd
import numpy as np
import plotly

cols = plotly.colors.DEFAULT_PLOTLY_COLORS


fig=make_subplots(rows=1,cols=2,subplot_titles=['Plot 1','Plot 2'])
X=[0,50,100,150,200,250,300,350,400,450,500]
Y=[1,2,3,4,5,6,7,8,9,10,11]
Z=Y.copy()
for i  in np.arange(11):
    if i==0:
            Z[0]=Y[0]
    else:
            Z[i]=Y[i]+Z[i-1]
trace=go.Scatter(x=X,
                 y=Y,
                 line=dict(width=2, color=cols[0]),
                 marker=dict(color=cols[1]),
                 showlegend=False
                )
fig.append_trace(trace,1,1)

trace=go.Scatter(x=X,
                 y=Z,
                 line=dict(width=2, color=cols[0]),
                 marker=dict(color=cols[7]),
                 showlegend=False)

fig.append_trace(trace,1,2)

fig.show()


来源:https://stackoverflow.com/questions/42549054/plotly-how-to-use-the-same-color-scheme-on-two-subplots

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!