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

Python Felix ⋅ 于 2020-09-20 20:40:29 ⋅ 105 阅读

Introduction

When you have just a number or two to share, simple text can be a great way to communicate.

Comparison

Bar chart:

file

Simple text:

file

Code

Bar chart

# import modules
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.patches as mpathes
plt.style.use('seaborn-whitegrid')
# import data
data = pd.read_csv('D:/programming/dataset/data.csv')
# first method to draw a bar chart
plt.bar(data['year'], data['percentage'], width=10, color='cornflowerblue')

file

# second method to draw a bar chart
fig, ax = plt.subplots()
# create two points where bottoms of two bar locate
point1 = np.array([0.2, 0.2])
point2 = np.array([0.6, 0.2])
# set heights for two bars
h1 = data['percentage'][0] / 50 * 0.4
h2 = data['percentage'][1] / 50 * 0.4
# create two "rectangles" as bars [1]
rect1 = mpathes.Rectangle(point1, 0.2, h1, color='cornflowerblue')
rect2 = mpathes.Rectangle(point2, 0.2, h2, color='cornflowerblue')
ax.add_patch(rect1)
ax.add_patch(rect2)
# add texts based on position
# ax.text(x position, y position, string, fontsize, horizontalalignment,     verticalalignment, color, style)
ax.text(point1[0] + 0.1, h1 + 0.2 + 0.02, str(data['percentage'][0]), fontsize=15, horizontalalignment='center')
ax.text(point2[0] + 0.1, h2 + 0.2 + 0.02, str(data['percentage'][1]), fontsize=15, horizontalalignment='center')
ax.text(point1[0] + 0.1, 0.2 - 0.02, str(data['year'][0]), fontsize=15, horizontalalignment='center', verticalalignment='top')
ax.text(point2[0] + 0.1, 0.2 - 0.02, str(data['year'][1]), fontsize=15, horizontalalignment='center', verticalalignment='top')
ax.text(0, h1 + 0.3, 'working husband', fontsize=20, c='grey', style='italic')
ax.text(0, h1 + 0.35, 'stay-at-home mother with a', fontsize=20, c='grey', style='italic')
ax.text(0, h1 + 0.4, '% of children with a married', fontsize=20, c='grey', style='italic')
ax.text(0, h1 + 0.47, 'Home Mother', fontsize=25)
ax.text(0, h1 + 0.52, '"Traditional" Stay-at-', fontsize=25)
ax.text(0, h1 + 0.57, 'Children with a', fontsize=25)
# add the line y = 0.2 as the x-axis
x = np.arange(0, 1, 0.01)
y = np.ones(len(x))
y = y * 0.2
ax.plot(x, y, c='black')
ax.axis('off')

file

Simple text:

# simple text
plt.figure(figsize=(4, 2))
plt.axis('off')
plt.text(0.2, 1.5, str(data['percentage'][1]) + '%', fontsize=80, weight='bold', color='cornflowerblue')
plt.text(0.2, 1, 'of children had a', fontsize=30)
plt.text(0.2, 0.7, 'traditional stay-at-home mom', fontsize=30, weight='bold', color='cornflowerblue')
plt.text(0.2, 0.4, 'in ' + str(data['year'][1]) + ', compared to ' + str(data['percentage'][0]) + '% in ' + str(data['year'][0]), fontsize=30)

file

References

[1] python-matplotlib-draw-shapes

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