Python Implementation on "Storytelling with Data"——Figure 2.4

Python Felix ⋅ 于 2020-09-20 17:57:48 ⋅ 185 阅读

Introduction

This text introduces how to use Python to draw tables.

Sometimes table with light background color and solid text color could draw readers' attention more on data and more clearly emphasize the points.

Import modules

import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.patches as mpathes

Set style

plt.style.use('seaborn-whitegrid')

Import data

data = pd.read_csv('D:/programming/dataset/storytellingwithdata/project/data2.4.csv')

Data preview

file

Data preprocessing

# Change all rows and columns to lists.
row1 = data.loc[0].values.tolist()
row2 = data.loc[1].values.tolist()
row3 = data.loc[2].values.tolist()
row4 = data.loc[3].values.tolist()
row5 = data.loc[4].values.tolist()
columns = data.columns.tolist()

Draw tables

Define a format_table() function

def format_table(table, i, row, col, color_1, color_2, linewidth):
    """Draw a table in a subplot with specified row, column, cell background color, text     color, and linewidth."""

    table_cells = table.get_celld()
    table.set_fontsize(15)

    # [1]
    for _col_ in range(0, col):
        # Set background color and text color of the column titles.
        table_cells[0, _col_].set_color(color_1) 
        table_cells[0, _col_].get_text().set_color(color_2)
        for _row_ in range(0, row):
            # Set cells' height and linewidth.
            table_cells[(_row_, _col_)].set_height(.15)
            table_cells[(_row_, _col_)].set_linewidth(linewidth)

    ax[i].axis('off')

Show tables and formatting

# Define constants.
ROW: int = 6
COL: int = 4
LINEWIDTH_1: int = 3
LINEWIDTH_2: int = 1
LINEWIDTH_3: int = 0
LINE_HEIGHT: int = 83

# Create 3 subplots (1 * 3).
fig, ax = plt.subplots(1, 3, figsize=(15, 3), dpi=150)

# Put data in and return 3 tables.
table_1 = ax[0].table([columns,row1,row2,row3,row4,row5], cellLoc='center', loc=9)
table_2 = ax[1].table([columns,row1,row2,row3,row4,row5], cellLoc='center', loc=9)
table_3 = ax[2].table([columns,row1,row2,row3,row4,row5], cellLoc='center', loc=9)

# Format 3 tables by function defined.
# black background and white text
format_table(table_1, 0, ROW, COL, 'black', 'white', LINEWIDTH_1) 
# grey background and white text
format_table(table_2, 1, ROW, COL, 'grey', 'white', LINEWIDTH_2)
# white background and black text
foramt_table(table_3, 2, ROW, COL, 'white', 'black', LINEWIDTH_3)

# Set boundries in the third subplot.
# invisible line: y = 100
x = np.arange(0, 5, 0.01)
y = np.ones(len(x))
y = y * 100
ax[2].plot(x, y, c='white')
# invisible line: y = 0
x = np.arange(0, 5, 0.01)
y = np.ones(len(x))
y = y * 0
ax[2].plot(x, y, c='white')

# Draw the line in the third subplot.
# visible black line: y = LINE_HEIGHT
x = np.arange(-1, 10, 0.01)
y = np.ones(len(x))
y = y * LINE_HEIGHT
ax[2].plot(x, y, c='grey')

plt.show()

Result

file

References

[1] how-to-set-background-color-of-a-column-in-a-matplotlib-table

本帖已被设为精华帖!
本帖由 YX 于 4周前 加精
回复数量: 0
    暂无评论~~
    • 请注意单词拼写,以及中英文排版,参考此页
    • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
    • 支持表情,使用方法请见 Emoji 自动补全来咯,可用的 Emoji 请见 :metal: :point_right: Emoji 列表 :star: :sparkles:
    • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif
    • 发布框支持本地存储功能,会在内容变更时保存,「提交」按钮点击时清空
    Ctrl+Enter