AllanChain / blog

Blogging in GitHub issues. Building with Astro.
https://allanchain.github.io/blog/
MIT License
13 stars 0 forks source link

Jupyter etc. Cheatsheet #153

Open AllanChain opened 3 years ago

AllanChain commented 3 years ago

View Post on Blog

记录一些我实际遇到的关于 Jupyter 使用,Numpy 和 Pandas 数据处理,以及 Matplotlib 绘图相关的一些问题和解决方案。


JupyterLab

Extensions out of sync?

Go to <python>\share\jupyter\lab\settings, edit (or delete) those JSON files.

Matplotlib

官方 cheat sheet

官方 cheat sheet

绘制横线

plt.axhline(y, color, xmin, xmax, linestyle)

双 Y 轴

From https://matplotlib.org/stable/gallery/spines/multiple_yaxis_with_spines.html

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

twin1 = ax.twinx()

p1, = ax.plot([0, 1, 2], [0, 1, 2], "b-", label="Density")
p2, = twin1.plot([0, 1, 2], [0, 3, 2], "r-", label="Temperature")

ax.set_xlim(0, 2)
ax.set_ylim(0, 2)
twin1.set_ylim(0, 4)

ax.set_xlabel("Distance")
ax.set_ylabel("Density")
twin1.set_ylabel("Temperature")

ax.legend(handles=[p1, p2])

plt.show()

size

plot 中使用 markersize 或者 ms 来修改点的大小, 而在 scatter 中直接使用 sizes 即可.

Scipy

B-Spline

scipy doc

from scipy.interpolate import make_interp_spline

xx = np.linspace(x.min(), x.max(), 400)
plt.plot(xx, make_interp_spline(x, y, k=2)(xx))

numpy

同阶矩阵逐项相乘

称为 Hadamard Product(哈达玛积),np.multiply() 得到

pandas

Rolling Mean

From https://stackoverflow.com/a/49016377/8810271

类似股票 n 日均线,或者示波器采样平均值

df.rolling(100).mean()