k01ken’s b10g

He110 W0r1d!

matplotlibで対数のグラフを書く

開発環境は、Windows7 Professional(32bit) + Python 2.7.10.12。
math.logを使って、対数のグラフをmatplotlibで描画してみました。描画したのは3種類で、それぞれ、底が、e(自然対数)、2、10(常用対数)です。

# -*- conding:utf-8 -*-

import math
import matplotlib.pyplot as plt

x = [i+1 for i in range(1000)]
y = [math.log(n) for n in x]
y2 = [math.log(n,2) for n in x]
y3 = [math.log10(n) for n in x]

pp1, = plt.plot(x,y, label='e')
pp2, = plt.plot(x,y2, label='2')
pp3, = plt.plot(x,y3, label='10')
plt.legend(handles=[pp1,pp2,pp3])
plt.show()

グラフはこちら、
f:id:k01ken:20171227192709p:plain

底の数が大きいほど、より小さい数で表現できてます。

参考リンク
Legend guide — Matplotlib 2.0.2 documentation
matplotlibのグラフの体裁を整える - たこ焼き食べた.net