Gibberish

Archive for the ‘Python’ Category

Matplotlib color chart

Here is the color chart for using inside matplotlib

 

lfzum

Image gotten from a discussion on stack: https://stackoverflow.com/questions/22408237/named-colors-in-matplotlib

Butterfly curve

Nay ngồi xem lại phương trình tham số (parametric equation), vọc một tí với plot trong python.

Phương trình tham số là phương trình mà nghiệm được biểu diễn bởi một số (tham số) bên ngoài, ví dụ nghiệm x,y sẽ được biểu diễn bởi x = f(t) và y =g(t).

Phương trình tham số của đường con bướm được biểu diễn như sau:

x = sin (t) \left( e^{cos(t)} - 2 cos (4t) -sin^5 (\frac{t}{12}) \right)

y = cos (t) \left( e^{cos(t)} - 2 cos (4t) -sin^5 (\frac{t}{12}) \right)

t \in (0, 12 \pi)

Đồ thị nó sẽ ra như thế này:

ButterflyFunction

Code python để plot như sau:


# the butterfly curve
from __future__ import division
import numpy as np, math, os, scipy
import matplotlib.pyplot as plt

#t_range = np.arange(0,12*math.pi,0.001)
t_range = np.linspace(0,12*math.pi,1000)
for t in t_range:
x = math.sin(t)*(math.exp(math.cos(t))-2*math.cos(4*t)-(math.sin(t/12))**5)
y = math.cos(t)*(math.exp(math.cos(t))-2*math.cos(4*t)-(math.sin(t/12))**5)
plt.scatter(x,y,marker='.',s=0.2,c='k')
plt.show()

Tag Cloud