座標に点を表示する

plotメソッドを使って、座標に点を表示することができます。

plt.plot(x座標, y座標,marker = '点の形',color = '点の色',markersize = 点のサイズ)


座標に、(2, 1)と(4, 2)を表示します。

%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

plt.plot(2, 1, marker='.')
plt.plot(4, 2, marker='.')

plt.xlim([0, 5])
plt.ylim([0, 5])
plt.xlabel("x", size=14)
plt.ylabel("y", size=14)
plt.grid()
plt.show()

pyenvを使ってインストールしたPythonでNo module named '_tkinter'となったとき

pyenvを使ってインストールしたPythonで、tkinterを使おうとしたとき、エラーとなりました。

import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'


tkinterが自分の環境にインストールされているか調べてみました。
インストールされていないようなので、tkinterをインストールします。

$ python -m tkinter
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'


home brewを使って、tcl-tkをインストールします。

$ brew install pyenv


pyenvがtcl-tkを使うように、python-buildスクリプトを修正します。

$ cd /usr/local/Cellar/pyenv/1.2.15/plugins/python-build/bin/python-build
$ cp python-build python-build_original
$ vi python-build


以下のように修正します。

【修正前】
$CONFIGURE_OPTS ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1

【修正後】
$CONFIGURE_OPTS --with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6' ${!PACKAGE_CONFIGURE_OPTS} "${!PACKAGE_CONFIGURE_OPTS_ARRAY}" || return 1


pyenvを使って、Pythonをインストールします。

$ pyenv install 3.7.5
$ pyenv global


tkinterが使えるか、テストしてみます。

$ python -m tkinter -c 'tkinter._test()'


小さな画面が表示されれば、成功です。

アムダール積

アムダールの積とは、行列の要素ごとの積のことです。

\begin{bmatrix}0 & 1 & 2 \\3 & 4 & 5 \\6 & 7 & 8 \end{bmatrix}  *   \begin{bmatrix}0 & 1 & 2 \\2 & 0 & 1 \\1 & 2 & 0 \end{bmatrix} 
= \begin{bmatrix}0*0 & 1*1 & 2*2 \\3*2 & 4*0 & 5*1 \\6*1 & 7*2 & 8*0 \\ 6*1 & 7*2 & 8*0\end{bmatrix} 
= \begin{bmatrix}0 & 1 & 4 \\6 & 0 & 5 \\6 & 14 & 0 \end{bmatrix}
演算子は*で書いています。本来は、A○Bと書きます。


Pythonの場合、以下のように書きます。

import numpy as np

a = np.array([[0, 1, 2],
              [3, 4, 5],
              [6, 7, 8]])

b = np.array([[0, 1, 2],
              [2, 0, 1],
              [1, 2, 0]])

print(a * b)

Active Storageを利用する準備をする

Active Storageで画像のサイズを変換する機能を利用するために、ImageMagickをインストールします。

$ brew install imagemagick


RailsでImageMagicを利用するために、Gemパッケージをインストールします。
Gemfileに以下を追加します。

gem 'mini_magick'


Gemパッケージをインストールします。

$ bundle install


マイグレーションスクリプトを生成します。

$ bin/rails active_storage:install
$ bin/rails db:migrate