21.添加注释文字sin(x)
x = np.linspace(0.05, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.text(3.2, 0, 'sin(x)', weight='bold', color='r');
22.用箭头标出第一个峰值
x = np.linspace(0.05, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.annotate('maximum',xy=(np.pi/2, 1),xytext=(np.pi/2+1, 1),
weight='bold',
color='r',
arrowprops=dict(arrowstyle='->', connectionstyle='arc3', color='r'));
23.在一张图里绘制sin,cos的图形,并展示图例
x = np.linspace(0, 10, 1000)
fig, ax = plt.subplots()
ax.plot(x, np.sin(x), label='sin')
ax.plot(x, np.cos(x), '--', label='cos')
ax.legend();
24.调整图例在左上角展示,且不显示边框
ax.legend(loc='upper left', frameon=False);
fig
25.调整图例在画面下方居中展示,且分成2列
ax.legend(frameon=False, loc='lower center', ncol=2)
fig
26.绘制sin(x),sin(x+π/2),sin(x+π)的图像,并只显示前2者的图例
y = np.sin(x[:, np.newaxis] + np.pi * np.arange(0, 2, 0.5))
lines = plt.plot(x, y)
# lines 是 plt.Line2D 类型的实例的列表
plt.legend(lines[:2], ['first', 'second']);
# 第二个方法
#plt.plot(x, y[:, 0], label='first')
#plt.plot(x, y[:, 1], label='second')
#plt.plot(x, y[:, 2:])
#plt.legend(framealpha=1, frameon=True);
27.将图例分不同的区域展示
fig, ax = plt.subplots()
lines = []
styles = ['-', '--', '-.', ':']
x = np.linspace(0, 10, 1000)
for i in range(4):
lines += ax.plot(x, np.sin(x - i * np.pi / 2),styles[i], color='black')
ax.axis('equal')
# 设置第一组标签
ax.legend(lines[:2], ['line A', 'line B'],
loc='upper right', frameon=False)
# 创建第二组标签
from matplotlib.legend import Legend
leg = Legend(ax, lines[2:], ['line C', 'line D'],
loc='lower right', frameon=False)
ax.add_artist(leg);
28.展示色阶
x = np.linspace(0, 10, 1000)
I = np.sin(x) * np.cos(x[:, np.newaxis])
plt.imshow(I)
plt.colorbar();
29.改变配色为'gray'
plt.imshow(I, cmap='gray');
30.将色阶分成6个离散值显示
plt.imshow(I, cmap=plt.cm.get_cmap('Blues', 6))
plt.colorbar()
plt.clim(-1, 1);
31.在一个1010的画布中,(0.65,0.65)的位置创建一个0.20.2的子图
ax1 = plt.axes()
ax2 = plt.axes([0.65, 0.65, 0.2, 0.2])
32.在2个子图中,显示sin(x)和cos(x)的图像
fig = plt.figure()
ax1 = fig.add_axes([0.1, 0.5, 0.8, 0.4], ylim=(-1.2, 1.2))
ax2 = fig.add_axes([0.1, 0.1, 0.8, 0.4], ylim=(-1.2, 1.2))
x = np.linspace(0, 10)
ax1.plot(np.sin(x));
ax2.plot(np.cos(x));
33.用for
创建6个子图,并且在图中标识出对应的子图坐标
for i in range(1, 7):
plt.subplot(2, 3, i)
plt.text(0.5, 0.5, str((2, 3, i)),fontsize=18, ha='center')
# 方法二
# fig = plt.figure()
# fig.subplots_adjust(hspace=0.4, wspace=0.4)
# for i in range(1, 7):
# ax = fig.add_subplot(2, 3, i)
# ax.text(0.5, 0.5, str((2, 3, i)),fontsize=18, ha='center')
34.设置相同行和列共享x,y轴
fig, ax = plt.subplots(2, 3, sharex='col', sharey='row')
35.用[]
的方式取出每个子图,并添加子图座标文字
for i in range(2):
for j in range(3):
ax[i, j].text(0.5, 0.5, str((i, j)),fontsize=18, ha='center')
fig
36.组合绘制大小不同的子图,样式如下
grid = plt.GridSpec(2, 3, wspace=0.4, hspace=0.3)
plt.subplot(grid[0, 0])
plt.subplot(grid[0, 1:])
plt.subplot(grid[1, :2])
plt.subplot(grid[1, 2]);
37.显示一组二维数据的频度分布,并分别在x,y轴上,显示该维度的数据的频度分布
mean = [0, 0]
cov = [[1, 1], [1, 2]]
x, y = np.random.multivariate_normal(mean, cov, 3000).T
# Set up the axes with gridspec
fig = plt.figure(figsize=(6, 6))
grid = plt.GridSpec(4, 4, hspace=0.2, wspace=0.2)
main_ax = fig.add_subplot(grid[:-1, 1:])
y_hist = fig.add_subplot(grid[:-1, 0], xticklabels=[], sharey=main_ax)
x_hist = fig.add_subplot(grid[-1, 1:], yticklabels=[], sharex=main_ax)
# scatter points on the main axes
main_ax.scatter(x, y,s=3,alpha=0.2)
# histogram on the attached axes
x_hist.hist(x, 40, histtype='stepfilled',
orientation='vertical')
x_hist.invert_yaxis()
y_hist.hist(y, 40, histtype='stepfilled',
orientation='horizontal')
y_hist.invert_xaxis()
38.创建一个三维画布
from mpl_toolkits import mplot3d
fig = plt.figure()
ax = plt.axes(projection='3d')
39.绘制一个三维螺旋线
ax = plt.axes(projection='3d')
# Data for a three-dimensional line
zline = np.linspace(0, 15, 1000)
xline = np.sin(zline)
yline = np.cos(zline)
ax.plot3D(xline, yline, zline);
40.绘制一组三维点
ax = plt.axes(projection='3d')
zdata = 15 * np.random.random(100)
xdata = np.sin(zdata) + 0.1 * np.random.randn(100)
ydata = np.cos(zdata) + 0.1 * np.random.randn(100)
ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='Greens');
特别申明:本文为转载文章,转载自Python大数据分析 ,作者王大毛,不代表贪吃的夜猫子立场,如若转载,请注明出处:https://mp.weixin.qq.com/s/7I_7Ad1GGXt47izcPHX46g