我正在创建一个像这样的简单表格...
trace = go.Table(
header = dict(
#some header info
),
cells = dict(
values = weeklyIntegers, #list e.g. [4, 11, 0, 32]
line = dict(color = '#506784'),
font = dict(color = '#506784', size = 9)
),
)
plotyData.append(trace)
layout = go.Layout(
#some layout info
)
fig = Figure(data=plotyData, layout=layout)
plotly.offline.init_notebook_mode(connected=True)
plotly.offline.iplot(fig, filename='someFilename')
我正在尝试弄清楚如何将简单的色阶应用于单元格值而不使用像 colorlover 这样的库如 suggested here (原因是我无法控制安装新库的环境)。
我是否应该能够将预先确定的颜色列表传递给单元格中的字体定义,例如......
myColorList = ['red', 'blue', 'black', 'yellow']
cells = dict(
values = weeklyIntegers, #list e.g. [4, 11, 0, 32]
line = dict(color = '#506784'),
font = dict(color = myColorList, size = 9)
),
我找不到任何解决此问题的文档,因此目前我只是猜测适当的语法是什么。
最佳答案
如果您使用from plotly.colors import n_colors
您可以构建自己的色标,该色标对于 weeklyIntegers
的长度更加灵活列表。下面是使用蓝色的图,但我也为红色和绿色设置了两个变量。
plotly :
代码:
# imports
from plotly.colors import n_colors
import plotly.graph_objects as go
import numpy as np
import pandas as pd
# color scale
a = [4, 11, 0, 32]
blues = n_colors('rgb(200, 200, 255)', 'rgb(0, 0, 200)', max(a)+1, colortype='rgb')
reds = n_colors('rgb(255, 200, 255)', 'rgb(200, 0, 0)', max(a)+1, colortype='rgb')
greens = n_colors('rgb(200, 255, 200)', 'rgb(0, 200, 0)', max(a)+1, colortype='rgb')
# plotly setup
fig = go.Figure()
# table setup
tbl = go.Table(header=dict(values=['<b>Table with custom color scale</b>'],
line_color='white', fill_color='white',
font=dict(color='white', size=8)),
cells=dict(values=[a],
line_color=[np.array(blues)[a]],
fill_color=[np.array(blues)[a]],
align='center', font=dict(color='white', size=11)))
fig.add_trace(go.Table(tbl))
fig.show()
关于python - Python 中带有 Plotly 表值的手动色标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53464263/