导入matplotlib
import matplotlib.pyplot as plt
import matplotlib
Figures对象包含一个或多个Asex对象
方法:
-
matplotlib.rc(‘figure’, figsize = (14, 7)) 全局设置大小
-
matplotlib.rc(‘font’, size = 14) 全局设置字体
-
matplotlib.rc(‘axes.spines’, top = False, right = False) 不显示顶部和右侧的坐标线
-
matplotlib.rc(‘axes’, grid = False) 不显示网格
-
matplotlib.rc(‘axes’, facecolor = ‘white’) 设置背景颜色是白色
-
fig, axes = plt.subplots() 返回figure和axes对象 创建总画布
-
plt.xlim(x.min()*1.1, x.max()*1.1) 设置x刻度范围
-
plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
[r’ − π -\pi −π’, r’ − π / 2 -\pi/2 −π/2’, r’ 0 0 0’, r’ π / 2 \pi/2 π/2’, r’ π \pi π’]) #latex 设置x下标对应的值 -
plt.legend(frameon=False) 设置图例,控制是否应在图例周围绘制框架
-
plt.title(“Cosine and Sine”) 设置标签名
-
plt.scatter([t, ], [np.cos(t), ], 50, color=‘blue’) 画一个散点在x,y位置大小50颜色蓝色
-
plt.boxplot([data.loc[data.sex == ‘Male’, ‘tip’], data.loc[data.sex == ‘Female’, ‘tip’]], labels=[‘Male’, ‘Female’]) 绘制箱式图
-
plt.axes([0.025,0.025,0.95,0.95]) 设置x最小值最大值,y最小值最大值的范围
-
plt.contourf(X, Y, f(X,Y), 8, alpha=.75, cmap=plt.cm.hot) 绘制等高线并对颜色进行填充
-
C = plt.contour(X, Y, f(X,Y), 8, colors=‘black’, linewidth=.5) 绘制黑色等高线
-
plt.clabel(C, inline=1, fontsize=10) 标出等高线的label
-
plt.grid(True) 显示网格线
-
plt.imshow(Z,interpolation=‘bicubic’, cmap=‘bone’, origin=‘lower’) 画热力图
- cmap参数设置当前色图的函数:
- hot 从黑平滑过度到红、橙色和黄色的背景色,然后到白色。
- cool 包含青绿色和品红色的阴影色。从青绿色平滑变化到品红色。
- gray 返回线性灰度色图。
- bone 具有较高的蓝色成分的灰度色图。该色图用于对灰度图添加电子的视图。
- white 全白的单色色图。
- spring 包含品红和黄的阴影颜色。
- summer 包含绿和黄的阴影颜色。
- autumn 从红色平滑变化到橙色,然后到黄色。
- winter 包含蓝和绿的阴影色。
- cmap参数设置当前色图的函数:
-
plt.colorbar(shrink=.92) 增加颜色类标
-
plt.quiver(X,Y,U,V,R, alpha=.5) 画箭头场
-
plt.quiver(X,Y,U,V, edgecolor=‘k’, facecolor=‘None’, linewidth=.5) 描黑箭头场
GridSpec
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3, 3), (2, 0))
ax5 = plt.subplot2grid((3, 3), (2, 1))
调整grid的排版
gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.05, right=0.48, wspace=0.05)
ax1 = plt.subplot(gs1[:-1, :])
ax2 = plt.subplot(gs1[-1, :-1])
ax3 = plt.subplot(gs1[-1, -1])
gs2 = gridspec.GridSpec(3, 3)
gs2.update(left=0.55, right=0.98, hspace=0.05)
ax4 = plt.subplot(gs2[:, :-1])
ax5 = plt.subplot(gs2[:-1, -1])
ax6 = plt.subplot(gs2[-1, -1])
-
plt.text(0.8, 0.9, r’ x ∈ [ 0.0 , 10.0 ] x \in [0.0, \ 10.0] x∈[0.0, 10.0]’, color=‘k’, fontsize=15) 设置文字描述
-
plt.annotate()
- s 为注释文本内容
- xy 为被注释的坐标点
- xytext 为注释文字的坐标位置
- xycoords 参数如下:
- figure points points from the lower left of the figure 点在图左下
- figure pixels pixels from the lower left of the figure 图左下角的像素
- figure fraction fraction of figure from lower left 左下角数字部分
- axes points points from lower left corner of axes 从左下角点的坐标
- axes pixels pixels from lower left corner of axes 从左下角的像素坐标
- axes fraction fraction of axes from lower left 左下角部分
data use the coordinate system of the object being
- arrowprops #箭头参数,参数类型为字典dict
- width the width of the arrow in points 点箭头的宽度
- headwidth the width of the base of the arrow head in points 在点的箭头底座的宽度
- headlength the length of the arrow head in points 点箭头的长度
- shrink fraction of total length to ‘shrink’ from both ends 总长度为分数“缩水”从两端
- facecolor 箭头颜色
-
annotated(default) 使用的坐标系统被注释的对象(默认)
polar(theta,r) if not native ‘data’ coordinates t
extcoords 设置注释文字偏移量| 参数 | 坐标系 | | 'figure points' | 距离图形左下角的点数量 | | 'figure pixels' | 距离图形左下角的像素数量 | | 'figure fraction' | 0,0 是图形左下角,1,1 是右上角 | | 'axes points' | 距离轴域左下角的点数量 | | 'axes pixels' | 距离轴域左下角的像素数量 | | 'axes fraction' | 0,0 是轴域左下角,1,1 是右上角 | | 'data' | 使用轴域数据坐标系 |
-
matplotlib.rc(“figure”, figsize=(8,6)) 全局设置图像大小
-
matplotlib.rc(“font”, size=10) 全局设置字体大小
-
ax = plt.subplot(111) 画子图 几行几列第几个图
-
ax.spines[“right”].set_color(“none”) 将右边框设置为无色 可以设置四个方位right/top/bottom/left
-
ax2.spines[‘right’].set_visible(True) 设置右侧坐标轴可见
-
ax1.set_ylabel(“Adj Close”, color = “blue”) 设置y坐标说明
-
ax1.set_xlabel(“Date”) 设置x坐标说明
-
ax.plot(x, c, color=‘blue’, linewidth=2.5, linestyle=’-’, label=‘cosine’)
- x,c 为x与y
- color 设置图像颜色
- linewidth 设置线条宽度
- linestyle 设置线条样式
- label 设置线条标签
- bins=20 默认10,等分成20个桶然后把多少个数字给画出来,能反映出一个桶有多少个数,可以看出3个列服从正态分布
plt.annotate(r'$\cos(\frac{2\pi}{3})$', xy=(t, np.cos(t)), xycoords="data", xytext=(-90, -50), textcoords='offset points', fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
导入seaborn
import seaborn as sns
方法:
- sns.heatmap(df.corr()) 热力图