![](/img/trans.png)
[英]How can I save a Matplotlib figure after changing the background color?
[英]Setting cell color of matplotlib table and save as a figure?
我正在关注此代码的链接! 将表格另存为图像,并且我有一些功能,例如在单元格中检查值然后为单元格设置颜色,但是我添加了一些代码样式图,但它不起作用
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import six
.........
df = pd.DataFrame()
df['date'] = ['2016-04-01', '2016-04-02', '2016-04-03']
df['calories'] = [2200, 2100, 1500]
df['sleep hours'] = [2200, 2100, 1500]
df['gym'] = [True, False, False]
df.style.applymap(color)
render_mpl_table(df, header_columns=0, col_width=2.0)
.......
def color(val):
if val < datetime.now():
color = 'green'
elif val > datetime.now():
color = 'yellow'
elif val > (datetime.now() + timedelta(days=60)):
color = 'red'
return 'background-color: %s' % color
def render_mpl_table(data, col_width=3.0, row_height=0.625, font_size=14,
header_color='#40466e', row_colors=['#f1f1f2', 'w'], edge_color='w',
bbox=[0, 0, 1, 1], header_columns=0,
ax=None, **kwargs):
if ax is None:
size = (np.array(data.shape[::-1]) + np.array([0, 1])) * np.array([col_width, row_height])
fig, ax = plt.subplots(figsize=size)
ax.axis('off')
mpl_table = ax.table(cellText=data.values, bbox=bbox, colLabels=data.columns, **kwargs)
mpl_table.auto_set_font_size(False)
mpl_table.set_fontsize(font_size)
for k, cell in six.iteritems(mpl_table._cells):
cell.set_edgecolor(edge_color)
if k[0] == 0 or k[1] < header_columns:
cell.set_text_props(weight='bold', color='w')
cell.set_facecolor(header_color)
else:
cell.set_facecolor(row_colors[k[0]%len(row_colors) ])
return ax
我将样式放在数据框下然后呈现表
我的解决方案是为单元格颜色创建新的数据框,使用颜色检查表中的这些值创建数据框,然后添加到cellColours
我的代码块:
#table datas
df = pd.DataFrame()
members = [x.display_name[:10] for x in message.server.members]
arrayDataFrame(members, 'Players', df)
arrayDataFrame(Data=members, df=df)
#table Colors
cf = pd.DataFrame()
arrayColorFrame(members, "Players", cf)
arrayColorFrame(Data=members, df=cf)
#render
plt = render_mpl_table(df, header_columns=0, col_width=2.0, CellCol= cf)
>>>>>>>>>>>>>>
def render_mpl_table(data, col_width=3.0, row_height=0.625, font_size=14,
header_color='#40466e', row_colors=['#f1f1f2', 'W'], edge_color='B',
bbox=[0, 0, 1, 1], header_columns=0,
ax=None, CellCol=None, **kwargs):
>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>
mpl_table = ax.table(cellText=data.values, bbox=bbox, colLabels=data.columns,
cellColours=CellCol.values, **kwargs)
def arrayDataFrame(Data, Lable='No', df=None):
if df is None:
df = pd.DataFrame()
leng = len(Data)
array = list()
if Lable == 'No':
for index in range(leng):
array.append(int(index + 1))
else:
for val in Data:
array.append(val)
df[Lable] = array
return df
def arrayColorFrame(Data, Lable='No', df=None):
if df is None:
df = pd.DataFrame()
leng = len(Data)
array = list()
if Lable == 'No':
for index in range(leng):
val = index + 1
if val < 10:
color = 'green'
elif val < 20:
color = 'yellow'
elif val < 40:
color = 'red'
else:
color = 'w'
array.append(color)
else:
for val in Data:
if "nat" in val :
color = 'blue'
else:
color='w'
array.append(color)
df[Lable] = array
return df
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.