はじめに
Colabで遊んでいます。
数直線number line
をmatplotlibで描いて見ました。コードセルにimport matplotlib.pyplot as plt
として書いて、実行すると描画がされます。svgにも吐き出せたので、ここではsvgで貼り付けました。
コード
まずは両端を矢印にするのにどうしたらよいか苦労しました。メモリに数字をつけるのもようやくできました。 はじめからSVGで描いた方が簡単だったと思うけど、matplotlibの勉強にはなったと思う。
import matplotlib.pyplot as plt import numpy as np ax=plt.figure(figsize=(12,6)).add_subplot(xlim=(-4,4), ylim=(0, 1.0)) plt.arrow(-3.5, 0.5, 7, 0, head_width=0.05, head_length=0.15, linewidth=4, color='b', length_includes_head=True) plt.arrow(3.5, 0.5, -7, 0, head_width=0.05, head_length=0.15, linewidth=4, color='b', length_includes_head=True) x = [-3, -2, -1, 0, 1, 2, 3] y = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5] data_name = ["-3", "-2", "-1", "0", "1", "2", "3"] plt.plot(x, y, 'r|', ms="40") for (i, j, name) in zip (x, y, data_name) : plt.text(i, j, name, fontsize=15, position=(i-0.05, j-0.2)) plt.savefig("numberline.svg") plt.show()
画像
次に主力された画像(数直線)です。
height
とwidth
をいじれば小さくできると思うけど、とりあえずそのままのせます。