0%

用Python绘制数学背景图

By Z.H. Fu
https://fuzihaofzh.github.io/blog/

Python的matplotlib真心是个好用的东西,除了画一般的图形,他还支持LaTeX\LaTeX,我们可以利用这个来做一点酷酷的东西。下面给出数学公式插图的Python代码,生成结果还是蛮酷的:

python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from pylab import *
eqs = []
eqs.append((r"$W^{3\beta}_{\delta_1 \rho_1 \sigma_2} = U^{3\beta}_{\delta_1 \rho_1} + \frac{1}{8 \pi 2} \int^{\alpha_2}_{\alpha_2} d \alpha^\prime_2 \left[\frac{ U^{2\beta}_{\delta_1 \rho_1} - \alpha^\prime_2U^{1\beta}_{\rho_1 \sigma_2} }{U^{0\beta}_{\rho_1 \sigma_2}}\right]$"))
eqs.append((r"$\frac{d\rho}{d t} + \rho \vec{v}\cdot\nabla\vec{v} = -\nabla p + \mu\nabla^2 \vec{v} + \rho \vec{g}$"))
eqs.append((r"$\int_{-\infty}^\infty e^{-x^2}dx=\sqrt{\pi}$"))
eqs.append((r"$F_G = G\frac{m_1m_2}{r^2}$"))
eqs.append((r"$F_y-\frac{dF_{y'}}{dx}=0$"))
eqs.append((r"$\delta J=\frac{\partial J}{\partial u_i}\delta u_i$"))
eqs.append((r"$\prod_{i=1}^n\times\sum_{j_i=1}^m\prod_{i=1}^na_{ij_i}$"))
eqs.append((r"$\int_\Omega f\nabla \cdot \mathbf{A} d\Omega$"))
eqs.append((r"$(x+y)^n=\sum_{k=0}^n\binom{n}{k}x^{n-k}y^k$"))
eqs.append((r"$\binom{n}{k_1,k_2,\cdots,k_m}$"))
figure(figsize=(20,10))
axes([0.025,0.025,0.95,0.95])

for i in range(20):
index = np.random.randint(0,len(eqs))
eq = eqs[index]
size = np.random.uniform(20,43)
x,y = np.random.uniform(0,1,2)
alpha = np.random.uniform(0.05,.15)
text(x, y, eq, ha='center', va='center', color="# 11557c", alpha=alpha,
transform=gca().transAxes, fontsize=size, clip_on=True)
xticks([]), yticks([])
show()

效果如图:

mbtutorial