Jupyter etc. Cheatsheet

2020-12-05
Header

This article is a bit old and the content may be outdated, so please refer to it with caution and remember to check the latest official materials (such as documentation, etc.)

记录一些我实际遇到的关于 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)
python

双 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()
python

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))
python

numpy #

同阶矩阵逐项相乘 #

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

pandas #

Rolling Mean #

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

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

df.rolling(100).mean() 
python
Leave your comments and reactions on GitHub