pandas - 用笑脸绘制图表百分比
问题描述
我想添加一个基于笑脸的绘图图,如下所示:dat will come from a dataframe pandas:dataframe.value_counts(normalize=True)
谁能给我一些线索。
解决方案
- 以正常方式对热图使用色阶
- 使用annotation_text将表情符号分配给一个值
import plotly.figure_factory as ff
import plotly.graph_objects as go
import pandas as pd
import numpy as np
df = pd.DataFrame([[j*10+i for i in range(10)] for j in range(10)])
e=["","","","☹️"]
fig = go.Figure(ff.create_annotated_heatmap(
z=df.values, colorscale="rdylgn", reversescale=False,
annotation_text=np.select([df.values>75, df.values>50, df.values>25, df.values>=0], e),
))
fig.update_annotations(font_size=25)
# allows emoji to use background color
fig.update_annotations(opacity=0.7)
更新彩色表情符号
- 从根本上说,您需要可以接受颜色样式的表情符号
- 为此,我切换到了 Font Awesome。这还需要切换到dash, plotly的表弟,以便可以使用外部 CSS(使用 FA)
- 然后构建一个破折号HTML 表格,应用样式逻辑来选择表情和颜色
from jupyter_dash import JupyterDash
import dash_html_components as html
import pandas as pd
import branca.colormap
# Load Data
df = pd.DataFrame([[j*10+i for i in range(10)] for j in range(10)])
external_stylesheets = [{
'href': 'https://use.fontawesome.com/releases/v5.8.1/css/all.css',
'rel': 'stylesheet', 'crossorigin': 'anonymous',
'integrity': 'sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf',
}]
# possibly could use a a different library for this - simple way to map a value to a colormap
cm = branca.colormap.LinearColormap(["red","yellow","green"], vmin=0, vmax=100, caption=None)
def mysmiley(v):
sm = ["far fa-grin", "far fa-smile", "far fa-meh", "far fa-frown"]
return html.Span(className=sm[3-(v//25)], style={"color":cm(v),"font-size": "2em"})
# Build App
app = JupyterDash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
html.Table([html.Tr([html.Td(mysmiley(c)) for c in r]) for r in df.values])
])
# Run app and display result inline in the notebook
app.run_server(mode='inline')
推荐阅读
- sql - 选择数组中的最后一个公共元素
- apache-spark - 将 Spark 累加器与结构化流结合使用
- excel - 查询按日期和用户对这些行进行分组,在特定事件后开始一个新组
- google-apps-script - TypeError:response.getContentText 不是函数应用脚本
- image - Latex 图像页眉和页脚
- github - 使用自定义域使用 GitHub 页面托管文件时获取“页面 url”?
- configuration - Gulp 和 Coffeescript - 信号异步完成警告
- dart - 如何检查列表是否包含列表,并从列表中删除列表
- php - 集合属性中的 PHP Blade Foreach 元素
- jenkins - Jenkins kubernetes-plugin 与多个集群对话