表格单元格的 2 级或 3 级着色的条件格式 [英] Conditional formatting for 2- or 3-scale coloring of cells of a table

查看:93
本文介绍了表格单元格的 2 级或 3 级着色的条件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个简单的表格输出到PDF文件,其中某些条件格式取决于该值的单元格2或3色.就像Microsoft Excel条件格式设置选项中的红白绿颜色缩放一样.

导入熊猫将numpy导入为npdf = pandas.DataFrame(np.random.randn(10, 2), columns=list('ab'))打印文件#输出:b0 -1.625192 -0.9491861 -0.089884 0.8259222 2.117651 -0.0462583 -0.921751 -0.1444474 -0.294095 -1.7747255 -0.780523 -0.4359096 0.544958 0.3032687 0.014335 0.0361828 -0.756565 0.1207119 1.145055 0.542755

现在,我想以3比例条件格式将其输出到PDF表中的PDF中,该条件格式用于 a 列,并且独立地用于 b 列,因此我的输出将看起来像下面的 Excel 示例.

有点像这样,但按列:

解决方案

好的,我做了更多的研究,这几乎涵盖了我想做的事情.使用 matplotlib 颜色图可以解决问题.这是地图颜色选项的链接.

I would like to output a simple table to a PDF file with some conditional formatting of 2- or 3-scale coloring of cells dependent on the value. Like the red-white-green color scaling in Microsoft Excel conditional formatting option.

import pandas
import numpy as np
df = pandas.DataFrame(np.random.randn(10, 2), columns=list('ab'))
print df

#Output:
          a         b
0 -1.625192 -0.949186
1 -0.089884  0.825922
2  2.117651 -0.046258
3 -0.921751 -0.144447
4 -0.294095 -1.774725
5 -0.780523 -0.435909
6  0.544958  0.303268
7  0.014335  0.036182
8 -0.756565  0.120711
9  1.145055  0.542755

Now, I would like to output this to a PDF in a table with a 3-scale conditional format for column a and independently for column b so my output would look like the below example from Excel.

Kind of like this, but by column:

解决方案

Ok, I did a little more research and this pretty much covers what i wanted to do. using matplotlib colormaps did the trick. here is a link to the options of map colours. matplotlib color maps

I went with RdYlGn colouring.

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt

df = pd.DataFrame(np.random.randn(10, 8), columns=list('abcdefgh'))

print df

#Round to two digits to print nicely
vals = np.around(df.values, 2)
#Normalize data to [0, 1] range for color mapping below
normal = (df - df.min()) / (df.max() - df.min())

fig = plt.figure()
ax = fig.add_subplot(111)
ax.axis('off')
the_table=ax.table(cellText=vals, rowLabels=df.index, colLabels=df.columns, 
                   loc='center', cellColours=plt.cm.RdYlGn(normal),animated=True)
fig.savefig("table.png")

Output:

这篇关于表格单元格的 2 级或 3 级着色的条件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆