LoginSignup
7
7

More than 3 years have passed since last update.

掛け算を覚える九九(くく)を視覚化するポスターをmatplotlibで作成

Last updated at Posted at 2021-01-13

「九九」(くく)ってありますよね。日本で子供たちが掛け算を覚える時のやつです。

「いんいちがいち、いんにがに、いんさんがさん、」ってやつです。

掛け算を覚えるのに九九は便利なのですが、「語呂だけ覚えてもダメだ。掛け算の意味を理解しないと。」という意見を耳にしたので、掛け算の意味もあわせて理解できるポスターを Python の matplotlib で作ってみました。コーディングに要した時間は約30分。

日本語対応 matploblib のインストールとインポート

!pip install japanize-matplotlib
Collecting japanize-matplotlib
[?25l  Downloading https://files.pythonhosted.org/packages/aa/85/08a4b7fe8987582d99d9bb7ad0ff1ec75439359a7f9690a0dbf2dbf98b15/japanize-matplotlib-1.1.3.tar.gz (4.1MB)
[K     |████████████████████████████████| 4.1MB 5.5MB/s 
[?25hRequirement already satisfied: matplotlib in /usr/local/lib/python3.6/dist-packages (from japanize-matplotlib) (3.2.2)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->japanize-matplotlib) (1.3.1)
Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->japanize-matplotlib) (2.8.1)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib->japanize-matplotlib) (0.10.0)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib->japanize-matplotlib) (2.4.7)
Requirement already satisfied: numpy>=1.11 in /usr/local/lib/python3.6/dist-packages (from matplotlib->japanize-matplotlib) (1.19.5)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.6/dist-packages (from python-dateutil>=2.1->matplotlib->japanize-matplotlib) (1.15.0)
Building wheels for collected packages: japanize-matplotlib
  Building wheel for japanize-matplotlib (setup.py) ... [?25l[?25hdone
  Created wheel for japanize-matplotlib: filename=japanize_matplotlib-1.1.3-cp36-none-any.whl size=4120276 sha256=642a1577ef5e695d7ef7a5fc14e07ad45d62ab853ca64da221873f69938f3e6c
  Stored in directory: /root/.cache/pip/wheels/b7/d9/a2/f907d50b32a2d2008ce5d691d30fb6569c2c93eefcfde55202
Successfully built japanize-matplotlib
Installing collected packages: japanize-matplotlib
Successfully installed japanize-matplotlib-1.1.3
import matplotlib.pyplot as plt
import japanize_matplotlib 

九九のポスターの描画

fig, axes = plt.subplots(nrows=9, ncols=9, figsize=(16, 16))
for x in range(9):
    for y in range(9):
        axes[y][x].axis("off")
        axes[y][x].set_aspect("equal")
        axes[y][x].set_title("{} x {} = {}".format(x + 1, y + 1, (x + 1) * (y + 1)))
        axes[y][x].set_xlim([0, 11])
        axes[y][x].set_ylim([0, 11])
        X = []
        Y = []
        for i in range(x + 1):
            for j in range(y + 1):
                X.append(i + 2)
                Y.append(10 - j - 1)
        axes[y][x].scatter(X, Y)

plt.savefig("300_dpi_scatter.png", format="png", dpi=300)

300_dpi_scatter.png

掛け算の順番?

さて、掛け算の順番を気にする先生たちがTwitterなどで話題になっているようですが、これについては何か言われるんでしょうかね?

ネズミバージョン

うちの子たちはネズミさんたちが大好きなので、ネズミバージョンも作ってみました。

fig, axes = plt.subplots(nrows=9, ncols=9, figsize=(28, 28))
for x in range(9):
    for y in range(9):
        axes[y][x].axis("off")
        axes[y][x].set_aspect("equal")
        axes[y][x].set_title("{} x {} = {}".format(x + 1, y + 1, (x + 1) * (y + 1)), fontsize=20)
        axes[y][x].set_xlim([-1, 11])
        axes[y][x].set_ylim([-1, 11])
        X = []
        Y = []
        for i in range(x + 1):
            for j in range(y + 1):
                X.append(i + 2)
                Y.append(10 - j - 1)
        if (x + 1) * (y + 1) % 2 == 1:
            c = 'k'
        else:
            c = 'r'
        axes[y][x].scatter(X, Y, c=c, s=28, marker='o')
        axes[y][x].scatter([xx + 0.25 for xx in X], [yy + 0.25 for yy in Y], c=c, s=12, marker='o')
        axes[y][x].scatter([xx - 0.25 for xx in X], [yy + 0.25 for yy in Y], c=c, s=12, marker='o')

plt.savefig("300_dpi_scatter.png", format="png", dpi=300)

300_dpi_scatter (1).png

掛け算の結果が奇数なら黒、偶数なら赤にしてみたよ。ハハッ♪

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7