gpt4 book ai didi

python - Python 中带有 Plotly 表值的手动色标

转载 作者:太空宇宙 更新时间:2023-11-03 21:31:59 24 4
gpt4 key购买 nike

我正在创建一个像这样的简单表格...

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 :

enter image description here

代码:

# 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com