数据分析可视化-matplotlib
1、figure
属性:color,linewidth,linestyle
2、设置坐标轴
plt.xlim设置坐标轴范围
plt.xlabel设置坐标轴名称
plt.xticks设置x轴刻度
使用ax=plt.gca获取当前坐标轴信息. 使用ax.spines设置边框;使用.set_color设置边框颜色:默认白色;
ax.xaxis.set_ticks_position设置x坐标刻度数字或名称的位置
ax.spines[‘bottom’].set_position((‘data’, 0))设置边框的位置,y=0的位置
3、legend图例
画每一条图像时先设置label属性,然后添加plt.legend(loc=’ ')
还有一种方式:
plt.legend(handles=[l1, l2], labels=[‘up’, ‘down’], loc=‘best’),但是要保证图像用变量l1和l2保存起来
4、annotation标注
plt.annotate(r’ 2 x + 1 = 2x+1=%s 2x+1=’ % y0, xy=(x0, y0), xycoords=‘data’, xytext=(+30, -30),
textcoords=‘offset points’, fontsize=16,
arrowprops=dict(arrowstyle=’->’, connectionstyle=“arc3,rad=.2”))
其中参数xycoords=‘data’ 是说基于数据的值来选位置, xytext=(+30, -30) 和 textcoords=‘offset points’ 对于标注位置的描述 和 xy 偏差值, arrowprops是对图中箭头类型的一些设置.
5、tick能见度
linewidth决定线条的宽度
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize(12)
- 在 plt 2.0.2 或更高的版本中, 设置 zorder 给 plot 在 z 轴方向排序
label.set_bbox(dict(facecolor=‘white’, edgecolor=‘None’, alpha=0.7, zorder=2))
plt.show()
其中label.set_fontsize(12)重新调节字体大小,bbox设置目的内容的透明度相关参,facecolor调节 box 前景色,edgecolor 设置边框, 本处设置边框为无,alpha设置透明度.
6、散点图
T = np.arctan2(Y,X) # for color value每一个点的颜色值
plt.scatter(X, Y, s=75, c=T, alpha=.5)
7、bar柱状图
plt.bar(X, +Y1, facecolor=’#9999ff’, edgecolor=‘white’)
facecolor设置主体颜色,edgecolor设置边框颜色
for x, y in zip(X, Y1):
// ha: horizontal alignment水平对齐
//va: vertical alignment
plt.text(x + 0.4, y + 0.05, '%.2f' % y, ha='center', va='bottom')
8、等高线图
栅格数据
X,Y = np.meshgrid(x, y)
//use plt.contourf to filling contours
//X, Y and value for (X,Y) point
plt.contourf(X, Y, f(X, Y), 8, alpha=.75, cmap=plt.cm.hot)
// use plt.contour to add contour lines
C = plt.contour(X, Y, f(X, Y), 8, colors='black', linewidth=.5)
plt.clabel(C, inline=True, fontsize=10)
9、Image图片
a = np.array([0.313660827978, 0.365348418405, 0.423733120134,
0.365348418405, 0.439599930621, 0.525083754405,
0.423733120134, 0.525083754405, 0.651536351379]).reshape(3,3)
plt.imshow(a,interpolation='nearest',cmap='bone',origin='lower')
plt.colorbar(shrink=.92)
10、3D数据
from mpl_toolkits.mplot3d import Axes3D
首先定义窗口,在窗口添加3D坐标轴
fig = plt.figure()
ax = Axes3D(fig)
之后数据XYZ
三维曲面
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap(‘rainbow’))
rstride 和 cstride 分别代表 row 和 column 的跨度。
投影
ax.contourf(X, Y, Z, zdir=‘z’, offset=-2, cmap=plt.get_cmap(‘rainbow’))
11、分格显示
plt.figure()
plt.subplot(2,2,1)#还有234位置
plt.plot()
subplot2grid
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
分成3行3列,从0,0位置开始作图,横跨列为3
gridspec
import matplotlib.gridspec as gridspec
#定义窗口,分成3行3列
plt.figure()
gs = gridspec.GridSpec(3, 3)
表示这个图占第一行和第二列前所有列
ax7 = plt.subplot(gs[1, :2])
12、图中图
首先绘制大图,确定大图左下角位置以及宽高
left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
ax1 = fig.add_axes([left, bottom, width, height])
然后绘制小图,确定坐标系位置和大小:
left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
ax2 = fig.add_axes([left, bottom, width, height])
ax2.plot(y, x, 'b')
13、次坐标轴
小图,确定坐标系位置和大小:
left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
ax2 = fig.add_axes([left, bottom, width, height])
ax2.plot(y, x, 'b')
13、次坐标轴
ax2 = ax1.twinx()