python绘制函数图像

2D隐函数画法:

1
2
3
4
from sympy.parsing.sympy_parser import parse_expr
from sympy import plot_implicit
ezplot = lambda expr: plot_implicit(parse_expr(expr))
ezplot('x*x - y')

2D一般画法:

1
2
3
4
5
6
import matplotlib.pyplot as plt
import numpy as np
import math
t = np.arange(-1.0, 1.01, 0.01)
plt.plot(t, np.exp(t), 'r', t, np.log(1 + np.exp(t)), 'b')
plt.show()

3D一般画法:

1
2
3
4
5
6
7
8
9
10
11
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = Axes3D(fig)
x = np.arange(-4, 4, 0.25)
y = np.arange(-4, 4, 0.25)
x, y = np.meshgrid(x, y)
z = x + y ** 2
ax.plot_surface(x, y, z, rstride = 1, cstride = 1, cmap = 'rainbow')
plt.show()

3D画法2:

1
2
3
4
5
from sympy import *
from sympy.plotting import plot3d
x, y = symbols('x y')
plot3d(x * y, -x + y, (x, -5, 5), (y, -5, 5))
plot3d((x**2 + y**2, (x, -5, 5), (y, -5, 5)), (x*y, (x, -3, 3), (y, -3, 3)))

3D画法3:

1
2
3
4
5
6
7
8
import numpy as np
from mayavi import mlab
x = np.arange(-40, 40, 0.25)
y = np.arange(-40, 40, 0.25)
x, y = np.meshgrid(x, y)
z = np.cos(np.sqrt(x**2 + y**2))
s = mlab.mesh(x,y,z)
mlab.show()

参数方程3D画法:

1
2
3
4
5
from sympy import *
from sympy.plotting import *
u, v = symbols('u v')
plot3d_parametric_line((cos(u), sin(u), u, (u, -5, 5)), (sin(u), u ** 2, u, (u, -5, 5)))
plot3d_parametric_surface((cos(u + v), sin(u - v), u - v,(u, -5, 5), (v, -5, 5)),(cos(u - v), sin(u + v), u - v,(u, -3, 3), (v, -3, 3)))

极坐标图:

1
2
3
4
5
6
7
import numpy as np
import matplotlib.pyplot as plt
ax = plt.subplot(111, polar = True)
ax.set_rlim(0, 2)
theta = np.arange(0, 2 * np.pi, 0.02)
ax.plot(theta, np.cos(theta))
plt.show()

矩阵对角化:

1
2
3
4
5
6
7
8
9
import numpy as np
a = [[np.cos(np.pi / 3), -np.sin(np.pi / 3)], [np.sin(np.pi / 3), np.cos(np.pi / 3)]]
c = np.linalg.eig(a)
d = c[1]
e = np.dot(np.dot(np.linalg.inv(d), a), d)
 
A = np.mat("1 -2 1; 0 2 -8; -4 5 9")
b = np.array([0, 8 -9])
x = np.linalg.solve(A, b)

输出excel表格:

1
2
3
4
5
import os
import pandas as pd
fileNames = os.listdir("./")
df = pd.DataFrame({'no' : range(1, len(fileNames) + 1), 'filename' : fileNames})
df.to_excel('dir.xlsx')

命令行参数提示:

1
2
3
4
5
6
7
8
9
10
11
12
import click
 
@click.command()
@click.option('--times', default=1, help='times to run.')
@click.argument('name')
def runLoop(name, times = 1):
	print("name is " + name)
	for i in range(times):
		print("running {} times".format(i+1))
 
if __name__ == '__main__':
	runLoop()

字符串格式化为C语言语句:

1
2
3
str = "DEADBEEF"
for i in range(int(len(str) / 2)):
    print("0x" + str[i * 2] + str[i * 2 + 1] + ", ", end = "")

Leave a Comment