Line graphs and scale lines in python

Make a note for yourself as you will need it very rarely

Taking the Monte Carlo method as an example

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

Recommended Posts

Line graphs and scale lines in python
[Python 3] Prime factorization in 14 lines
Python text reading for multiple lines and one line
Try LINE Notify in Python
Stack and Queue in Python
Make python segfault in 2 lines
Unittest and CI in Python
Python install in 2 lines @Windows
Make python segfault in one line
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
Difference between == and is in python
View photos in Python and html
Sorting algorithm and implementation in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
Assignments and changes in Python objects
Check and move directories in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Make python segfault in three lines
Function synthesis and application in Python
Export and output files in Python
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Ignore # line and read in pandas
I tried Line notification in Python
[Introduction] Insert line breaks in Python 3
Implemented in 1 minute! LINE Notify in Python
Create and read messagepacks in Python
Overlapping regular expressions in Python and Java
New in Python 3.9 (2)-Sort directed acyclic graphs in Python
Differences in authenticity between Python and JavaScript
Notes using cChardet and python3-chardet in Python 3.3.1.
CGI server (1) python edition in one line
Modules and packages in Python are "namespaces"
Avoid nested loops in PHP and Python
Differences between Ruby and Python in scope
AM modulation and demodulation in Python Part 2
Eigenvalues and eigenvectors: Linear algebra in Python <7>
Implementation module "deque" in queue and Python
Implement FIR filters in Python and C
Differences in syntax between Python and Java
Check and receive Serial port in Python (Port check)
Search and play YouTube videos in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
Write O_SYNC file in C and Python
Read the file line by line in Python
Dealing with "years and months" in Python
Read the file line by line in Python
Read and write JSON files in Python
Easily graph data in shell and Python
Private methods and fields in python [encryption]
Find and check inverse matrix in Python
Linear Independence and Basis: Linear Algebra in Python <6>
Decompose command arguments in one line in Python
Call sudo in Python and autofill password
[Python] Invert bool value in one line