写在前面
关于数据科学环境的建立,可以参考我的博客:
【深耕 Python】Data Science with Python 数据科学(1)环境搭建
往期数据科学博文:
【深耕 Python】Data Science with Python 数据科学(2)jupyter-lab和numpy数组
【深耕 Python】Data Science with Python 数据科学(3)Numpy 常量、函数和线性空间
【深耕 Python】Data Science with Python 数据科学(4)(书337页)练习题及解答
Jupyter代码片段1:简单二次函数
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-2, 2, 100)
fig, ax = plt.subplots(1, 1)
ax.plot(x, x * x)
plt.grid()
plt.show()
绘制图像:
Jupyter代码片段2:简单三角函数
#%%
from math import tau
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, tau, 100)
fig, ax = plt.subplots()
ax.set_xticks([0, tau/4, tau/2, 3 * tau / 4, tau])
ax.set_yticks([-1, -1/2, 0, 1/2, 1])
plt.grid()
ax.set_xticklabels([r'$0$', r'$\tau/4$', r'$\tau/2$', r'$3\tau/4$', r'$\tau$'])
ax.set_yticklabels([r'$-1$', r'$-1/2$', r'$0$', r'$1/2$', r'$1$'])
ax.set_title("One period of cosine and sine", fontsize=16)
ax.set_xlabel(r"$\theta$", fontsize=16)
ax.set_ylabel(r"$f(\theta)$", fontsize=16)
ax.annotate(r"$\cos(\theta)$", xy=(1.75, -0.3), xytext=(0.5, -0.75), arrowprops={"facecolor": "black", "width": 1}, fontsize=16)
ax.annotate(r"$\sin(\theta)$", xy=(2.75, 0.5), xytext=(3.5, 0.75), arrowprops={"facecolor": "black", "width": 1}, fontsize=16)
fig.set_dpi(150)
ax.plot(x, np.cos(x), color="red", linestyle="dashed")
ax.plot(x, np.sin(x), color="blue", linestyle="dotted")
plt.show()
绘制图像:
参考文献 Reference
《Learn Enough Python to be Dangerous——Software Development, Flask Web Apps, and Beginning Data Science with Python》, Michael Hartl, Boston, Pearson, 2023.