我有一个情节表达的数字:
fig = px.line(data, x="DateTime", y="Gold", title="Gold Prices")
我想改变一些细节,就像这样
fig.update_layout(
line_color="#0000ff", # ValueError: Invalid property specified for object of type plotly.graph_objs.Layout: 'line'
line=dict(
color='rgb(204, 204, 204)',
width=5
), # Bad property path: line
)
但是两次尝试(尝试我在这里研究的解决方案)都失败了,评论中给出了错误。
我也尝试过
fig = px.line(data, x="DateTime", y="Gold", title="Gold Prices", template="ggplot2", color_discrete_map={"Gold": "green"})
没有成功。
请问我该如何进行这项工作?
尝试将
.update_traces()
与 plotly.express
一起使用,而不是 .update_layout()
:
fig.update_traces(line_color='#0000ff', line_width=5)
plotly.express
如果你想使用plotly.express,请添加以下设置。
import plotly.express as px
df = px.data.stocks()
fig = px.line(df, x='date', y="GOOG", title='Ticker:GOOG')
fig['data'][0]['line']['color']='rgb(204, 204, 204)'
fig['data'][0]['line']['width']=5
fig.show()
plotly.graph_objects
如果你使用的是plotly.graph_objects,你可以在go.Scatter()中设置它。
import plotly.express as px
import plotly.graph_objects as go
df = px.data.stocks()
fig = go.Figure(data=go.Scatter(x=df['date'], y=df['GOOG'], mode='lines', line_color='rgb(204, 204, 204)', line_width=5))
fig.update_layout(title='Ticker:GOOG')
fig.show()
尝试使用“set_color”函数,如下所示:
fig.set_color('b')
或
fig.set_color('g')
其中 g 和 b 也可以是 RGB 配色方案的 r。 然后
fig.show()
fig = px.line(data, x="DateTime", y="Gold", title="Gold Prices", color_discrete_map={"DateTime": "blue", "Gold": "green"})
这对我来说非常有效:
fig = px.line(
data,
x="DateTime",
y="Gold",
title="Gold Prices",
color_discrete_sequence=["rgb(204, 204, 204)"] # This parameter do the trick.
)
它也适用于使用以下内容:
color_discrete_sequence=["black"]