更改 python dashplotly 主题中的颜色

Ste*_*aus 10 python plotly-dash

我目前正在创建我的第一个绘图破折号应用程序,并对图形主题有疑问:

我想使用可用的plotly_dark主题,并且仅将基础颜色(背景和元素颜色)调整为自定义值。我尝试了这里提供的想法:https://plotly.com/python/templates/# saving -and-distributing-custom-themes 像这样

import plotly.graph_objects as go
import plotly.io as pio

pio.templates["plotly_dark_custom"] = go.layout.Template(
    ...custom definitions here...
)
pio.templates.default = "plotly_dark_custom"
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有更直观的方法,例如随后构建子主题plotly_dark 并仅覆盖颜色(提供颜色调色板并定义新的背景颜色)。

由于我对Python很陌生,所以我的知识非常有限,所以我希望你能给我一些正确方向的指导。

谢谢您,请告诉我是否应该提供有关此请求的更多详细信息。斯蒂芬

yas*_*ash 6

显然,当您使用 update_layout() 方法更改现有模板设置(仅指定的模板设置)时,它会设法覆盖它们。

fig1.update_layout(
    plot_bgcolor='rgb(17,17,17)',
    paper_bgcolor ='rgb(10,10,10)')
Run Code Online (Sandbox Code Playgroud)

我刚刚这样做了,它完成了工作,访问模板没有意义:)


Ste*_*aus 5

如果这对这里的其他人有帮助,我设法得到我想要的东西:

  1. 获取设置plotly_dark(您只需执行一次)
  2. 将自定义模板设置为情节黑暗
  3. 根据 1 的输出,根据您的需要更新自定义模板。

import plotly.graph_objects as go
import plotly.io as pio

plotly_template = pio.templates["plotly_dark"]
print (plotly_template)

pio.templates["plotly_dark_custom"] = pio.templates["plotly_dark"]

pio.templates["plotly_dark_custom"].update({
#e.g. you want to change the background to transparent
'paper_bgcolor': 'rgba(0,0,0,0)',
'plot_bgcolor': 'rgba(0,0,0,0)'
})
Run Code Online (Sandbox Code Playgroud)

很可能不是最优雅的解决方案,但它确实有效。斯蒂芬


bod*_*y11 5

我在运行斯蒂芬的答案时遇到错误,所以我想我会发布最终对我有用的内容。

# this helps us get the theme settings
import plotly.io as plt_io

# this is for simple plotting with plotly express
import plotly.express as px

# create our custom_dark theme from the plotly_dark template
plt_io.templates["custom_dark"] = plt_io.templates["plotly_dark"]

# set the paper_bgcolor and the plot_bgcolor to a new color
plt_io.templates["custom_dark"]['layout']['paper_bgcolor'] = '#30404D'
plt_io.templates["custom_dark"]['layout']['plot_bgcolor'] = '#30404D'

# you may also want to change gridline colors if you are modifying background
plt_io.templates['custom_dark']['layout']['yaxis']['gridcolor'] = '#4f687d'
plt_io.templates['custom_dark']['layout']['xaxis']['gridcolor'] = '#4f687d'

# load an example dataset to test
df = px.data.iris()

# create a nice default plotly example figure
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
                 size='petal_length', hover_data=['petal_width'])

# set the template to our custom_dark template
fig.layout.template = 'custom_dark'

# and voila, we have a modified dark mode figure
fig.show()
Run Code Online (Sandbox Code Playgroud)