python - 如何更新所有 Plotly 图表的全局字体颜色?

标签 python plotly

我有一个笔记本,上面有大约 20 个不同的 plotly 。 目前,我在每个绘图函数中使用此命令手动更改颜色:

fig.update_layout(font = dict(color = '#051c2c')) 
return fig

这似乎有点多余,有没有办法为所有 Plotly 定义全局颜色或字体选项?

最佳答案

如果您要使用 Plotly Dash 或 JupyterDash , 这可以通过 stylesheets 来完成.据我所知,您唯一的选择似乎是直接通过 locals() 编辑图形对象。 .只需确保您的所有图形在命名时都遵循某种系统,例如 fig1fig2 等:

for l in list(locals().keys()):
    if l[:3] == 'fig':
        locals()[l].update_layout(font_family = 'Old Standard TT', font_size = 24)

这里有四个图显示这个小片段实际上将按照上面的位置编辑一组图 fig1fig2 的字体。

plotly :

enter image description here

完整代码:

import pandas as pd
import plotly.express as px

# data
df = pd.DataFrame({'a': {0: 1000, 1: 996, 2: 996},
                   'b': {0: 1000, 1: 1007, 2: 998},
                   'c': {0: 1000, 1: 1009, 2: 999}})

# figure setup and display
fig1 = px.scatter(df, x = 'a', y='c', title = 'Figure 1')
fig2  = px.bar(df, x = 'a', y='c', title = 'Figure 2')
fig2.update_layout(font=dict(family = 'Courier New'))
fig1.show()
fig2.show()

# some brute force editing
keys = list(locals().keys())
for l in keys:
    if l[:3] == 'fig':
        locals()[l].update_layout(font_color = 'firebrick', font_size = 16)

# display after editing
fig1.show()
fig2.show()

关于python - 如何更新所有 Plotly 图表的全局字体颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68331719/

相关文章:

python - plotly.figure_factory.create_annotated_heatmap 没有正确显示带有轴标签的图形

python - 名称错误 : global name 'dot_parser' is not defined

python - tipfy on Google App Engine (GAE) 安装扩展

python - CPython 是否保证 dict.keys 与 dict.values 具有相同的顺序?

python - 嵌套类以模仿静态成员

python - 无法在 Kivy 中设置布局大小

javascript - 当plotlyJS中的数据与其他图表差异很大时,如何打破条形图?

plotly - 隐藏模式栏在Dash中

r - 如何关闭在 Plotly 中显示我的所有数据的工具提示?

r - 手动重新排序绘图 parcoord 图的尺寸后希望触发观察事件