杂项
杂项
matplotlib使用
import matplotlib.pyplot as plt
import numpy as np
- 普通画图
x = np.arange(0, 16, 1)
y = x**2 + 1
plt.plot(x, y)
- 确定x, y坐标轴的范围
在jupyter notebook中注意这两行代码需要紧跟上一部分代码
plt.ylim((-100, 1000))
plt.xlim((0, 20))
- 确定x, y轴的坐标间隔
from matplotlib.pyplot import MultipleLocator
x_major_locator = MultipleLocator(5) # 间隔设为5
ax = plt.gca()
ax.xaxis.set_major_locator(x_major_locator)
- 添加标题
plt.title('xxxxxxx')
- 画子图
使用subplots函数
fig, ax = plt.subplots(2, 3) # 2行3列的一个子图
plt.subplots_adjust( wspace=0.5, hspace=0.4 ) # 调整子图之间的间距
fig.suptitle('xxxxxx') # 添加整个大图的标题
for i in range(2):
for j in range(3):
ax[i][j].plot(x, y)
# 设置子图坐标范围
ax[i][j].set_xlim(0, 100)
ax[i][j].set_ylim(0, 16)
# 设置子图坐标间隔
x_major_locator = MultipleLocator(5)
y_major_locator = MultipleLocator(10)
ax[i][j].xaxis.set_major_locator(x_major_locator)
ax[i][j].yaxis.set_major_locator(y_major_locator)
# 设置子图标题
ax[i][j].set_title('xxxxxx')
# 保存图片
fig.savefig('xxxxxxxxxx')
pandas使用
- 查看DataFrame前五行
x.head()
- 查看DataFrame的行数和列数
x.shaoe # 返回一个tuple
x.shape[1] # 查看列数
x.shape[0] # 查看行数
- 查看pandas列名和索引值
x.columns # 列名
x.index # 索引值
- 找到某一列元素中等于某值的所有行
id3 = timeseries.loc[timeseries['id'] == 3]
numpy使用
- 建立一个连续数组
import numpy as np
np.arange(0, 16, 2)
输出:array([ 0, 2, 4, 6, 8, 10, 12, 14])

浙公网安备 33010602011771号