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?
Adding Traces New traces can be added to a plot_ly figure using the add_trace() method. This method accepts a plot_ly figure trace and adds it to the figure.
By default, text annotations have xref and yref set to "x" and "y" , respectively, meaning that their x/y coordinates are with respect to the axes of the plot.
Simple SubplotFigures with subplots are created using the make_subplots function from the plotly. subplots module. Here is an example of creating a figure that includes two scatter traces which are side-by-side since there are 2 columns and 1 row in the subplot layout.
The following suggestion uses plotly version 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With