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

标签 python plotly

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

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/

相关文章:

python - 如何更改 Plotly 保存图像的步骤?

python - 尽管使用 utf8 编码,但某些字符无法识别

python - 来自 Enthought 的 Enaml 示例不适用于 Python(x,y) Enthought Tool Suite 版本的 Enaml

python - 为什么列表的长度必须包含 -1 才能在 for 循环中容纳 +1?

python - 如何应用 ConvLSTM 层

python - 禁用向下滚动并以绘图方式显示表格上的所有数据

python - 如何获得 PyQt 方法的正确签名

python - Plotly:如何为离散分类变量设置等值线图颜色?

python - Plotly 中的行悬停文本

r - 如何自定义 ggplotly 对象中的悬停信息?