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 = "")

HBITMAP数据对齐

CreateDIBSection生成HBITMAP时,传入的第四个参数ppvBits即会返回指向HBITMAP图像数据的指针,访问数据里面的点会涉及到数据对齐的问题。

一般我们访问未数据对齐的点的方法是:

1
2
3
4
5
6
7
8
for (int i = 0; i < width; i++)
{
	for (int j = 0; j < height; j++)
	{
		BYTE *color3 = pvBits + (j * width + i) * 3;
		...
	}
}

但HBITMAP的数据有对齐规定,正确访问方法是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
BITMAP bm;
CBitmap cbm;
cbm.Attach(hBmp); // <- 这是HBITMAP
cbm.GetBitmap(&bm);
int pixelBytes = bm.bmBitsPixel / 8;
for (int i = 0; i < width; i++)
{
	for (int j = 0; j < height; j++)
	{
		//pData在此没给出获取方法,网上都是关联HDC来获取,不爽,
		//具体是用GetDIBits函数从HBITMAP里拷贝一份内存出来
		BYTE *color3 = pData + j * bm.bmWidthBytes + i * pixelBytes;
		DWORD byte0 = *color3;
		DWORD byte1 = *(color3 + 1);
		DWORD byte2 = *(color3 + 2);
		COLORREF color4 = (byte0 << 16) | (byte1 << 8) | byte2;
		::SetPixel(hDC, i, height - j, color4);
	}
}

atls.lib(atlbase.obj) LNK1112

Additional Library Directories:

C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/atlmfc/lib

改为:

C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/atlmfc/lib/amd64

vs调试时path环境

在项目的属性,debugging,environment设置你要的path:

Path=your path;$(Path)

注意,<your path>不能用双引号括起来,Path变量名大小写都可以。

pyplot imshow 没有窗口

在调用完imshow之后,要继续调用show方法才能显示出来。

例如:

1
2
3
4
5
6
from PIL import Image
import matplotlib.pyplot as plt
 
image = Image.open("smallpi.jpg")
plt.imshow(image) # 绘制图像image
plt.show() # 需要调用show()方法,不然图像只会在内存中而不显示出来
1
2
import pdb
pdb.set_trace()

curl_easy_perform response返回码

curl_easy_perform返回值只表明此函数执行的情况,若要得到http response,必须在执行curl_easy_perform之后调用curl_easy_getinfo获取,例如:

1
2
3
4
long retcode = 0;
int ret = curl_easy_getinfo(_curlHandle, CURLINFO_RESPONSE_CODE , &retcode);
if ((CURLE_OK == ret) && retcode)
cout<<"response: "<<retcode<<endl;