栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python 学习小技巧

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python 学习小技巧

现在在上的课经常用到python,想要通过这个文章记录一些小技巧

1.编写API
# The API of the function:
#
#  name(x, h, noff_m, noff_h)
#     Returns the discrete, linear convolution of two sequences.
#
#     Parameters: 
#         x,h: array_like
#             The 2 input sequences.
#         noff_m, noff_h: int
#             n's offsets of the 2 input sequences.
#     Returns: (y, noff)
#         y: array_like
#             The output sequence.
#         noff: int
#             n's offset of the output sequence.

2.格式化输出
print('noff_%s =%2d, %s =' % (name, noff, name), seq)

3.lambda

例子:

seq_delta = lambda n_min, n_max: (np.array([ 1 if n==0 else 0 for n in range(n_min, n_max+1)]), 0 - n_min)
(n_min, n_max)=(0,49)
(x, noff_x) = seq_delta(n_min, n_max)

在这里,seq_delta就是一个函数,n_min, n_max就是变量,返回的是冒号后括号里两个参数,第一个是一个数组,第二个是一个数值

4.画图matplotlib.pyplot
import matplotlib.pyplot as plt    #使用matplotlib.pyplot数据库完成画图,并且在语言中用plt即可表述
# Un-comment the following line to use interactive matplotlib widget.
#%matplotlib widget

如果使用折线图,可以直接使用plot()和show()

plt.title("test") 
plt.xlabel("n")
plt.ylabel("x(n)")
n=[i for i in range(10)]
x=[i for i in np.arange(0,1,0.1)]
plt.plot(n, x) 
plt.show()

如果使用棉棒图,必须先声明fig, ax = plt.subplots()

fig, ax = plt.subplots()#将plt.subplots ()函数的返回值赋值给fig和ax两个变量。 plt.subplots ()是一个函数,返回一个包含figure和axes对象的元组
plt.title("test") 
plt.xlabel("n")
plt.ylabel("x(n)")
n=[i for i in range(10)]
x=[i for i in np.arange(0,1,0.1)]
plt.stem(n, x)#绘制序列图像

or

fig, ax = plt.subplots()#将plt.subplots ()函数的返回值赋值给fig和ax两个变量。 plt.subplots ()是一个函数,返回一个包含figure和axes对象的元组
ax.set_title("test") 
ax.set_xlabel("n")
ax.set_ylabel("x(n)")
n=[i for i in range(10)]
x=[i for i in np.arange(0,1,0.1)]
ax.stem(n, x)#绘制棉棒图

如果想使用点表示图形:

plt.title("test") 
plt.xlabel("n")
plt.ylabel("x(n)")
n=[i for i in range(10)]
x=[i for i in np.arange(0,1,0.1)]
plt.plot(n, x,"ob") 
plt.show()

 如果一幅图想画多个图像,可以如下:

#2行1列,下标从0开始
fig, axs = plt.subplots(2, 1, sharex=True, sharey=True)
n=[i for i in range(10)]
x=[i for i in np.arange(0,1,0.1)]
y=[i for i in np.arange(1,2,0.1)]
axs[0].stem(n, x)
axs[0].set_ylabel('x(n)')
axs[1].stem(n, y)
axs[1].set_ylabel('y(n)')
axs[1].set_xlabel('n')

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/529831.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号