如何在
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
我试过
fillcolor
但我怀疑它不起作用,因为这是一条简单的线。
您可以在
line=dict(color="#ffe476")
通话中添加go.Scatter(...)
。文档在这里:https://plot.ly/python/reference/#scatter-line-color
@nicolaskruchten 当然是对的,但我想提出另外两个选择:
line_color="#0000ff"
和:
fig['data'][0]['line']['color']="#00ff00"
或:
fig.data[0].line.color = "#00ff00"
我特别欣赏后一个选项的灵活性,因为它可以让您轻松地为所需的线条设置新颜色在您使用例如
fig.append_trace(go.Scatter())
或fig = go.Figure(data=go.Scatter))
构建图形之后。下面是使用所有三个选项的示例。
代码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()
情节1:
现在,如果您将下面的代码片段插入新单元格并运行它,您可以直接更改颜色。
代码2:
fig['data'][0]['line']['color']="#00ff00"
fig.show()
情节2:
fig.add_trace(
go.Scatter(
x=list(dict_val['yolo_timecost'].keys()),
y=signal.savgol_filter(list(dict_val['yolo_timecost'].values()),2653,3),
mode='lines',
name='YOLOv3实时耗时',
line=dict(
color='rgb(204, 204, 204)',
width=5
),
),
)
fig.data[0].line.color = 'rgb(204, 20, 204)'
你可以像那样使用 color_discrete_sequence
import plotly.express as px
df = px.data.gapminder().query("country=='Canada'")
fig = px.line(df, x="year", y="lifeExp", title='Life expectancy in Canada',color_discrete_sequence=["#ff97ff"])
fig.show()