【发布时间】:2018-01-21 16:50:55
【问题描述】:
下面的代码在 jupyter notebook 中运行时会呈现一个我想导出到图像文件的颜色渐变格式的表格。
notebook 呈现的“styled_table”对象是 pandas.io.formats.style.Styler 类型。
我无法找到将 Styler 导出到图像的方法。
我希望有人可以分享一个导出的工作示例,或者给我一些指示。
import pandas as pd
import seaborn as sns
data = {('count', 's25'):
{('2017-08-11', 'Friday'): 88.0,
('2017-08-12', 'Saturday'): 90.0,
('2017-08-13', 'Sunday'): 93.0},
('count', 's67'):
{('2017-08-11', 'Friday'): 404.0,
('2017-08-12', 'Saturday'): 413.0,
('2017-08-13', 'Sunday'): 422.0},
('count', 's74'):
{('2017-08-11', 'Friday'): 203.0,
('2017-08-12', 'Saturday'): 227.0,
('2017-08-13', 'Sunday'): 265.0},
('count', 's79'):
{('2017-08-11', 'Friday'): 53.0,
('2017-08-12', 'Saturday'): 53.0,
('2017-08-13', 'Sunday'): 53.0}}
table = pd.DataFrame.from_dict(data)
table.sort_index(ascending=False, inplace=True)
cm = sns.light_palette("seagreen", as_cmap=True)
styled_table = table.style.background_gradient(cmap=cm)
styled_table
【问题讨论】:
-
你可能想检查this answer
-
我的猜测是最后一条语句通过Styler.render返回HTML代码,所以你需要一个Python的方式来将HTML转换为图像,比如imgkit。
标签: python pandas jupyter-notebook seaborn pandas-styles