1. 用matplotlib制作表格:
# 事先需要安装对应的包
# pip install matplotlib
# pip install pandas
import matplotlib.pyplot as plt
import pandas as pd
if __name__ == "__main__":
data = {
"Books": [29, 23, 29, 20, 25, 23, 26],
"Magazines": [26, 23, 29, 28, 24, 22, 29], }
df = pd.DataFrame(data)
fig, ax = plt.subplots(figsize=(3, 4))
ax.axis("off")
ax.axis("tight")
tb = ax.table(cellText=df.values, colLabels=df.columns, bbox=[0, 0, 1, 1], )
tb[0, 0].set_facecolor("lightblue")
tb[0, 1].set_facecolor("lightblue")
tb[0, 0].set_text_props(color="black")
tb[0, 1].set_text_props(color="black")
plt.show()
#保存图片
plt.savefig('C:/Users/Mr.R/Desktop/FigA.png')
得到:
image.png
2. 用plotly制作表格:
import plotly.graph_objects as go
#给首行着色
headerColor = 'lightblue'
#给奇数偶数行制定不同的颜色
rowEvenColor = 'lightgrey'
rowOddColor = 'white'
fig = go.Figure(data=[go.Table(
header=dict(
#制定列名称
values=['EXPENSES','Qasim','Hashim','Sayim','Qayim'],
line_color='darkslategray',
fill_color=headerColor,
align=['left','center'],
font=dict(color='Black', size=16)
),
cells=dict(
values=[
['Salaries', 'Office', 'Merchandise', 'Legal', 'TOTAL'],
[1200000, 20000, 80000, 2000, 12120000],
[1300000, 20000, 70000, 2000, 130902000],
[1300000, 20000, 120000, 2000, 131222000],
[1400000, 20000, 90000, 2000, 14102000]],
line_color='darkslategray',
# 2-D list of colors for alternating rows
fill_color=[[rowOddColor,rowOddColor,rowOddColor, rowOddColor,rowEvenColor]*5],
align=['left', 'center'],
font=dict(color='darkslategray', size=12)
))
])
fig.show()
#保存图片
#fig.write_image("C:/Users/Mr.R/Desktop/Fig1.jpg", width=1920, height=1080)
得到:
image.png