Python 中的颜色编码数字类似于 Excel 条件格式

Color coding numbers in Python similar to Excel conditional formatting

Python 中是否有模块或任何变通方法可以将 0 到 100 之间的代码数字从红色变为绿色?我试图通过根据数字更改字体颜色来更好地可视化分数,但不想重新发明轮子。以为我可以找到 100 种不同的颜色并用它制作一本字典,但我想提前询问社区。非常感谢。

注意:我正在使用 Python、Flask 和 HTML。我需要一个简单的 function/module,我可以插入一个数字以十六进制输出相应的颜色。

在Pandas中,您可以使用以下代码获取附图。您将需要 Pandas、Numpy 进行四舍五入,并且您可能 还需要 Matplotlib。

(np.round(df.corr().iloc[:-1, :], 6).style.format({}) .background_gradient(cmap='RdYlGn', low=.4, high=.4))

您需要的一切都可以在 matplotlib 中找到。

https://matplotlib.org/2.0.0/examples/color/named_colors.html

也许尝试将颜色分成 10 个桶而不是 100 个桶,然后从中列出一个列表。

colors = ["#first", "#second", "#third", ..., "#tenth"]

然后,编写一个简单的函数,将数字分配给相应的颜色。

def pick_color(num):
    if num in range(0, 10):
        return colors[1]
    elif num in range(10, 20):
        return colors[2]

...等等。