我正在try 使用Plotly和SQL创建一个可视化表格

我已经设法以所需的格式提取数据,并使用SQL添加了一个摘要行.

然而,我需要将最后一行"Total"的格式设置为粗体,并使用更大的字体.

在jupyter中使用plotly有可能吗?

fig = go.Figure(data=[go.Table(
    header=dict(values=['<b>Product Group<b>','<b>Quantity Sold<b>', '<b>Sales<b>'],
                fill_color='white',
                align='center',
                font_size = 15),
    cells=dict(values=[PQS.ProductGroup, PQS.Quantity, PQS.Price],
               fill_color='white',
               align='center',
               format=[None,",d",",d"],
               suffix=[None, ' Units',' EGP'],
               font_size=12,
               ))
])

fig.show()

Image from Plotly

试图达到与此相似的外观.

Image from Tableau Report

这张图片来自Tableau,迁移的原因是更容易通过python实现自动化和报告

有没有办法在Plotly中复制这种格式?或者我应该使用另一个图书馆?

推荐答案

我不认为Plotly可以格式化一个表中的单个行,因为所选列的所有行都会同时传递到cells.

但是,一种解决方法是复制DataFrame PQS(以便在需要时保留原始表中的数字),将此新表转换为对象或字符串类型,将所有格式更改应用于这些列(包括使用小数和逗号格式化数字),然后将html标记<b>...</b>添加到最后一行,然后再将这个新格式化的PQS副本传递给go.Table.

例如:

import plotly.graph_objects as go
import pandas as pd

## reproduce your example with a similar dataframe
PQS = pd.DataFrame({
    'ProductGroup': [None,'Beverages','Others','Pizzas','Trattoria','Total'],
    'Quantity':[37,341,67,130,25,600],
    'Price':[1530,10097,3810,18625,4110,38172]
})

PQS_formatted_strings = PQS.copy()

for col in ["Quantity","Price"]:
    PQS_formatted_strings[col] = PQS_formatted_strings[col].map('{:,d}'.format)

PQS_formatted_strings["Quantity"] = PQS_formatted_strings["Quantity"] + ' Units'
PQS_formatted_strings["Price"] = PQS_formatted_strings["Price"] + ' EGP'

## add bold html tags to the last row
last_row = PQS_formatted_strings.iloc[-1,:].values
new_last_row = ['<b>' + entry + '</b>' for entry in last_row]
PQS_formatted_strings.iloc[-1,:] = new_last_row

fig = go.Figure(data=[go.Table(
    header=dict(values=['<b>Product Group</b>','<b>Quantity Sold</b>', '<b>Sales</b>'],
                fill_color='white',
                align='center',
                font_size = 15),
    cells=dict(values=[PQS_formatted_strings.ProductGroup, PQS_formatted_strings.Quantity, PQS_formatted_strings.Price],
               fill_color='white',
               align='center',
               font_size=12,
               ))
])

fig.show()

enter image description here

Python相关问答推荐

Pandas 按照特殊规则保留每n行

Pandas滚动分钟,来自其他列的相应值

Docker-compose:为不同项目创建相同的容器

具有症状的分段函数:如何仅针对某些输入值定义函数?

试图找到Python方法来部分填充numpy数组

根据另一列中的nan重置值后重新加权Pandas列

max_of_three使用First_select、second_select、

抓取rotowire MLB球员新闻并使用Python形成表格

运行Python脚本时,用作命令行参数的SON文本

_repr_html_实现自定义__getattr_时未显示

Python库:可选地支持numpy类型,而不依赖于numpy

我如何根据前一个连续数字改变一串数字?

Python Pandas获取层次路径直到顶层管理

在Python中使用if else或使用regex将二进制数据如111转换为001""

使用Python查找、替换和调整PDF中的图像'

为什么常规操作不以其就地对应操作为基础?

python—telegraph—bot send_voice发送空文件

Python避免mypy在相互引用中从另一个类重定义类时失败

巨 Python :逆向猜谜游戏

Python 3试图访问在线程调用中实例化的类的对象