計算機科学のブログ

ほしい物リスト

Python - Formatted String Literals: Make Charts from Data - SVG

Head First Python: A Learner’s Guide to the Fundamentals of Python Programming, A Brain-Friendly GuidePaul Barry(著)、 O’Reilly Mediaの Chapter 5.(Formatted String Literals: Make Charts from Data)、SHARPEN YOUR PENCIL(271/682)の解答を求めてみる。

Jupyter(コード、入出力結果)

HowTo.ipynb

*_, times, average, converts = swimclub.read_swim_data(fn) 
from_max = max(converts)
import hfpy_utils
svgs = ''
for n, t in enumerate(times):
    bar_with = hfpy_utils.convert2range(converts[n], 0, from_max, 0, 400)
    svgs += f'''
<svg height="30" width="400">
    <rect height="30" width="{bar_with}" style="fill:rgb(0,0,255)">
</svg>{t}<br>
'''
print(svgs)
<svg height="30" width="400">
    <rect height="30" width="386.76" style="fill:rgb(0,0,255)">
</svg>1:27.95<br>

<svg height="30" width="400">
    <rect height="30" width="356.51" style="fill:rgb(0,0,255)">
</svg>1:21.07<br>

<svg height="30" width="400">
    <rect height="30" width="400.0" style="fill:rgb(0,0,255)">
</svg>1:30.96<br>

<svg height="30" width="400">
    <rect height="30" width="365.96" style="fill:rgb(0,0,255)">
</svg>1:23.22<br>

<svg height="30" width="400">
    <rect height="30" width="386.76" style="fill:rgb(0,0,255)">
</svg>1:27.95<br>

<svg height="30" width="400">
    <rect height="30" width="388.3" style="fill:rgb(0,0,255)">
</svg>1:28.30<br>