我有一个笔记本,上面有大约 20 个不同的 plotly 。 目前,我在每个绘图函数中使用此命令手动更改颜色:
fig.update_layout(font = dict(color = '#051c2c'))
return fig
这似乎有点多余,有没有办法为所有 Plotly 定义全局颜色或字体选项?
最佳答案
如果您要使用 Plotly Dash 或 JupyterDash , 这可以通过 stylesheets 来完成.据我所知,您唯一的选择似乎是直接通过 locals()
编辑图形对象。 .只需确保您的所有图形在命名时都遵循某种系统,例如 fig1
、fig2
等:
for l in list(locals().keys()):
if l[:3] == 'fig':
locals()[l].update_layout(font_family = 'Old Standard TT', font_size = 24)
这里有四个图显示这个小片段实际上将按照上面的位置编辑一组图 fig1
和 fig2
的字体。
plotly :
完整代码:
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/