LoginSignup
0
0

More than 3 years have passed since last update.

pythonで折れ線グラフと目盛線

Posted at

ごく稀に必要になるので,自分用にメモ

モンテカルロ法を例に

import random
import math

result = []
in_circle = 0
out_circle = 0
for i in range( 100000 ):
  x = random.uniform( -1.0, 1.0 )
  y = random.uniform( -1.0, 1.0 )
  distance = math.sqrt( x ** 2 + y ** 2 )
  if distance <= 1:
    in_circle += 1
  else:
    out_circle += 1
  result.append( ( in_circle / ( in_circle + out_circle ) ) * 4)
import numpy as np
import matplotlib.pyplot as plt

plt.figure(figsize=(28,21))
plt.plot(range(1, len(result)+1), result, "-o")
plt.ylabel('area')
plt.xlabel('num of experiments') 
plt.xticks(np.arange(0, len(result) + 1, 10000))
plt.yticks(np.arange(0, 4.5, 0.1))
plt.plot([0, len(result)], [math.pi, math.pi], "red", linestyle='solid') 
plt.grid()
plt.show()

ダウンロード.png

0
0
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
0
0