[英]Customize tables using Matplotlib - colorization and fonts
我想使用 Matplotlib 創建要導出到 pdf 的表。
我在以下問題上掙扎。
以自動方式為單元格的着色創建一個列表列表。 第一列應該是白色的,第二列應該是綠色的,依此類推。
要為我用綠色着色的行設置 alpha 值,以增加透明度。
df = pd.DataFrame({'Name':['Z', 'S', 'H'],
'Category':['A', 'A', 'A'],
'Number':[2,1,3],
'P':[0,0,0]})
col_col=[]
blue = '#00005A'
green = '#95C13E'
for i in range(len(df.columns)):
col_col.append(blue)
fig, ax = plt.subplots()
plt.rcParams['font.family'] = 'Arial'
fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')
# Can I use a for loop to create the list of list colo in smart way?
colo = [["w",green,"w", green],["w",green,"w", green],["w",green,"w", green]]
table=ax.table(cellText=df.values, colLabels=df.columns, loc='center',
colColours=col_col,
cellColours=colo)
table=ax.table(cellText=df.values, colLabels=df.columns, loc='center',
colColours=col_col,
cellColours=colo)
for i, j in zip(table.properties()['celld'], table.properties()['children']):
if i[0]==0:
j.get_text().set_color('white')
j.get_text().set_weight('bold')
fig.tight_layout()
plt.show()
最簡單的方法是將您指定的綠色轉換為 RGBA 格式並指定透明度。
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors #import
df = pd.DataFrame({'Name':\['Z', 'S', 'H'\],
'Category':\['A', 'A', 'A'\],
'Number':\[2,1,3\],
'P':\[0,0,0\]})
col_col=\[\]
blue = '#00005A'
green = '#95C13E'
green = mcolors.to_rgba(green, alpha=0.5) #update and set alpha value
for i in range(len(df.columns)):
col_col.append(blue)
fig, ax = plt.subplots()
plt.rcParams\['font.family'\] = 'Arial'
fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')
# Can I use a for loop to create the list of list colo in smart way?
colo = \[\["w",green,"w", green\],\["w",green,"w", green\],\["w",green,"w", green\]\]
table=ax.table(cellText=df.values, colLabels=df.columns, loc='center',
colColours=col_col,
cellColours=colo)
for i, j in zip(table.properties()\['celld'\], table.properties()\['children'\]):
if i\[0\]==0:
j.get_text().set_color('white')
j.get_text().set_weight('bold')
fig.tight_layout()
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.