Plotly - Set line color

眉间皱痕 提交于 2020-01-05 04:20:57

问题


How can I set the color of a line in plotly?

import plotly.graph_objects as go
from plotly.subplots import make_subplots


fig = make_subplots(rows=2, cols=1, subplot_titles=('Plot 1', 'Plot 2'))

# plot the first line of the first plot
fig.append_trace(go.Scatter(x=self.x_axis_pd, y=self.y_1, mode='lines+markers', name='line#1'), row=1, col=1)  # this line should be #ffe476

I tried fillcolor but that I suspected doesn't work because this is a simple line.


回答1:


You can add line=dict(color="#ffe476") inside your go.Scatter(...) call. Documentation here: https://plot.ly/python/reference/#scatter-line-color




回答2:


@nicolaskruchten is of course right, but I'd like to throw in two other options:

line_color="#0000ff"

and

 fig['data'][0]['line']['color']="#00ff00"

I particularly appreciate the flexibility of the latter option since it easily lets you set a new color for a desired line after you've built a figure using for example fig.append_trace(go.Scatter()) or fig = go.Figure(data=go.Scatter)). Below is an example using all three options.

Code 1:

import plotly.graph_objects as go
import numpy as np

t = np.linspace(0, 10, 100)
y = np.cos(t)
y2= np.sin(t)
fig = go.Figure(data=go.Scatter(x=t, y=y,mode='lines+markers', line_color='#ffe476'))
fig.add_trace(go.Scatter(x=t, y=y2,mode='lines+markers', line=dict(color="#0000ff")))
fig.show()

Plot 1:

Now you can change the colors directly if you insert the snippet below in a new cell and run it.

Code 2:

fig['data'][0]['line']['color']="#00ff00"
fig.show()

Plot 2:



来源:https://stackoverflow.com/questions/58188816/plotly-set-line-color

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