我有一个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进行的一些简单格式化。
这是它的外观:
您应该将表分为三个逻辑部分:
这是代码:
import plotly.graph_objects as go
import pandas as pd
data = [['Jan-20', 1.0, 1, 4.0, 0, 250000.0],
['Jan-20', 2.0, 2, 4.0, 0, 250000.0],
['Jan-20', 3.0, 3, 4.0, 0, 250000.0],
['Jan-20', 3.0, 3, 4.0, 0, 250000.0],
['Jan-20', 3.0, 3, 4.0, 0, 250000.0],
['Jan-20', 3.0, 3, 4.0, 0, 250000.0],
['Jan-20', 4.0, 4, 4.0, 0, 250000.0],
['Jan-20', 5, 232, 114, 2312, 123]]
index = ['NL', 'DE', 'AT', 'FR', 'BE', 'ES', 'IT', 'Global']
columns = ['Months', 'budget_rev', 'budget_net_rev', 'gross_margin', 'new_net_mrr', 'act_shipments']
df = pd.DataFrame(data, index=index, columns=columns)
df.reset_index(inplace=True)
n_cols = len(columns)
n_rows = len(data)
header_c = 'dodgerblue'
rows_c = ['white']*(n_rows-1)
rows_c.extend(['dodgerblue'])
rows_f = ['darkslategray']*(n_rows-1)
rows_f.extend(['white'])
fig = go.Figure(data=[go.Table(columnwidth = [80]*(n_cols+1),
header=dict(values=list(df.columns),
line_color='darkslategray',
fill_color=header_c,
font=dict(color='white',
size=12)),
cells=dict(values=list(df.values.transpose()),
line_color='darkslategray',
fill_color = [rows_c*n_cols],
font = dict(color = [rows_f*n_cols],
size = 12)))])
fig.show()
您得到: