【发布时间】:2020-03-22 20:36:20
【问题描述】:
我有一个 DataFrame,我想将它作为 PNG/JPEG 粘贴到 Powerpoint 中。 我希望表格有一些格式。我一直在尝试使用 Matplotlib,并且有计划地进行,但我并没有接近最终结果。
我试过了:
import plotly.graph_objects as go
import pandas as pd
df = full_table
header_color = '#1D97FF'
fig = go.Figure(data=[go.Table(
header=dict(values=list(df.columns),
fill_color=header_color,
align='left',
line_color='darkslategray',
font=dict(color='white', size=11)),
cells=dict(values=[df.index, df.budget_revenue, df.budget_net_revenue, df.gross_margin,df.new_net_mrr,df.actual_shipments],
fill_color=['white'],
align='left', line_color='darkslategray', font=dict(color='black', size=11)))
])
fig.write_image("fig1.png")
fig.show()
我已经试过了:
import matplotlib.pyplot as plt
# Prepare table
df = full_table
hc = '#1D97FF'
columns=full_table.columns.to_list()
index=full_table.index.to_list()
values=full_table.values.tolist()
# Add a table at the bottom of the axes
colors = [["#56b5fd","w","w","w","w","w","w"],[ "#1ac3f5","w","w","w","w","w","w"]]
# Add a table at the bottom of the axes
colors = [["w","w","w","w","w","w"],[ "w","w","w","w","w","w"],
[ "w","w","w","w","w","w"],[ "w","w","w","w","w","w"],
[ "w","w","w","w","w","w"],[ "w","w","w","w","w","w"],
[ "w","w","w","w","w","w"],[ hc,hc,hc,hc,hc,hc]]
colors1 = ["w","w","w","w","w","w","w",hc]
colors2 = [ hc,hc,hc,hc,hc,hc]
fig, ax = plt.subplots()
ax.axis('off')
the_table = ax.table(cellText=values,cellColours=colors,
colLabels=columns,colColours=colors2,rowLabels=index, rowColours=colors1,loc='center')
plt.show()
第一个选项看起来已经很不错了。一些细微的变化,但我不能让底行变成蓝色。并且列标题不适合。 第二个选项是模糊的,左上角仍然是空的,应该做更多的字体编辑,我也没有成功。
我只是使用了不正确的库。坚持使用 Excel 在一分钟内完成的一些简单格式设置。
它应该是这样的:
【问题讨论】:
-
[['Jan-20', 80000.0, nan, 4.0, nan, 250000.0], ['Jan-20', 80000.0, nan, 4.0, nan, 250000.0], ['Jan-20 ', 80000.0, nan, 4.0, nan, 250000.0], ['Jan-20', 80000.0, nan, 4.0, nan, 250000.0], ['Jan-20', 80000.0, nan, 4.0, nan, 250000.0], [ 'Jan-20', 80000.0, nan, 4.0, nan, 250000.0], ['Jan-20', 80000.0, nan, 4.0, nan, 250000.0], ['Jan-20', 82854.06800000001, 82854.06800000001, nan, 250000.0]] ['NL','DE','AT','FR','BE','ES','IT','全球'] ['月','budget_revenue','budget_net_revenue',' Gross_margin'、'new_net_mrr'、'actual_shipments']
-
请提供数据框。谢谢。
-
数据 = [['Jan-20', 80000.0, 0, 4.0, 0, 250000.0], ['Jan-20', 80000.0, 0, 4.0, 0, 250000.0], ['Jan -20', 80000.0, 0, 4.0, 0, 250000.0], ['Jan-20', 80000.0, 0, 4.0, 0, 250000.0], ['Jan-20', 80000.0, 0, 4.0, 0, 250000.0] , ['Jan-20', 80000.0, 0, 4.0, 0, 250000.0], ['Jan-20', 80000.0, 0, 4.0, 0, 250000.0], ['Jan-20', 82854.06800000001, 82854,068000000, 0, 250000.0]] 索引 = ['NL', 'DE', 'AT', 'FR', 'BE', 'ES', 'IT', 'Global'] 列 = ['Months', 'budget_revenue' , 'budget_net_revenue', 'gross_margin', 'new_net_mrr', 'actual_shipments'] df = pd.DataFrame(data, index=index, columns=columns)
-
现在看起来很乱,将pandas作为pd导入,数据框就出来了。数值与上图略有不同。
-
我只需要那种格式。我想知道对数据框进行任何格式化的最佳方法是什么..
标签: python pandas matplotlib html-table plotly