pythonで乱数表示
pythonで0.0~1.0の乱数を100個/2系列生成し、100枚のpng画像として出力するプログラムを書いてみた。
#!/usr/bin/python # coding: UTF-8 import numpy as np import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt #plot 2 random series in a figure #save plot as 100 figures for i in range(100): plt.figure() plt.scatter(np.random.rand(100), np.random.rand(100), color = "c", label = "random 1") plt.scatter(np.random.rand(100), np.random.rand(100), color = "m", label = "random 2") plt.grid() plt.savefig("_fig_{0}.png".format(i)) pass
出力結果
for文使うのをやめたらもっと早く出力できるのにな。
最初plt.figure()を記述していなくてプロットがどんどん積み重なってしまった。
plt.figure()で新規描画。これ大事。
ついでにplt.legend(loc ="upper right")で凡例を右上につけておきましょう。