【莫烦Python】Matplotlib学习笔记(二)

【莫烦Python】Matplot学习笔记(一)

【莫烦Python】Matplotlib学习笔记(二)

一、Bar柱状图/条形图

二、Contours等高线图

三、Image图像

四、3D图像

五、subplot多合一显示

六、subplot分格显示

七、图中图

八、主次坐标轴

九、Animation动画


一、Bar柱状图/条形图

import matplotlib.pyplot as plt
import numpy as np

n = 12
X = np.arange(n)
Y1 = (1-X/float(n))*np.random.uniform(0.5, 1.0, n)
Y2 = (1-X/float(n))*np.random.uniform(0.5, 1.0, n)

plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')

for x, y in zip(X, Y1):
    plt.text(x, y+0.01, '%.2f'%y, ha='center', va='bottom')
             # 位置               # ha: horizontal alignment
for x, y in zip(X, Y2):
    plt.text(x, -y-0.01, '-%.2f'%y, ha='center', va='top')
plt.xlim((-.5, n))
plt.ylim((-1.25, 1.25))
plt.xticks(())  # 去掉坐标点
plt.yticks(())
plt.show()

二、Contours等高线图

import matplotlib.pyplot as plt
import numpy as np

def f(x, y):    # the height function  即根据x,y返回一个高度值,无需特别关心下面的公式
    return (1 - x/2 + x**5 + y**3)*np.exp(-x**2-y**2)

n = 256
x = np.linspace(-3, 3, n)
y = np.linspace(-3, 3, n)
X, Y = np.meshgrid(x, y)   # 将x,y绑定成网格的输入值

# use plt.contourf to filling contours
plt.contourf(X, Y, f(X, Y), 8, alpha=0.75, cmap=plt.cm.hot)  # alpha为透明度;cmap可换不同类型,例如plt.cm.cool

# use plt.contour to add contour lines
C = plt.contour(X, Y, f(X, Y), 8, colors='black', linewidths=.5)  # 其中 8 代表登高线分为10部分,若将8替换为0则对应2部分

# adding label
plt.clabel(C, inline=True, fontsize=10)

plt.xticks(())  # 去掉坐标点
plt.yticks(())
plt.show()

三、Image图像

import matplotlib.pyplot as plt
import numpy as np

# image data
a = np.array([0.3136608, 0.3653484, 0.4237331,
              0.3652484, 0.4395999, 0.5250837,
              0.4237331, 0.5250837, 0.6515363]).reshape(3, 3)

plt.imshow(a, interpolation='nearest', cmap='bone', origin='upper')
# 若需更换interpolation类型
# 可查看 http://matplotlib.org/examples/images_contours_and_fields/interpolation_methods.html
plt.colorbar()

plt.xticks(())  # 去掉坐标点
plt.yticks(())
plt.show()

其他interpolation效果图:

四、3D图像

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = Axes3D(fig)
# X,Y value
X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
# height value
Z = np.sin(R)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))
ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap='rainbow')  # 底面等高线图,zdir也可为x或y并相应的修改offset
ax.set_zlim((-2, 2))

plt.show()

五、subplot多合一显示

import matplotlib.pyplot as plt
import numpy as np

plt.figure()

plt.subplot(2, 2, 1)
plt.plot([0, 1], [0, 1])

plt.subplot(2, 2, 2)
plt.plot([0, 1], [0, 2])

plt.subplot(2, 2, 3)
plt.plot([0, 1], [0, 3])

plt.subplot(2, 2, 4)
plt.plot([0, 1], [0, 4])
plt.show()

import matplotlib.pyplot as plt
import numpy as np

plt.figure()

plt.subplot(2, 1, 1)  
plt.plot([0, 1], [0, 1])

plt.subplot(2, 3, 4)
plt.plot([0, 1], [0, 2])

plt.subplot(2, 3, 5)
plt.plot([0, 1], [0, 3])

plt.subplot(2, 3, 6)
plt.plot([0, 1], [0, 4])
plt.show()

六、subplot分格显示

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec  # method2

# method 1: subplot2gird
plt.figure()
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3, rowspan=1)  # colspan列跨度, rowspan行跨度
ax1.plot([1, 2], [1, 2])
ax1.set_title('ax1_title')   # 与figure的区别是,需要加set_, 例如set_xlabel() 等
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=1)
ax3 = plt.subplot2grid((3, 3), (1, 2), colspan=1, rowspan=2)
ax4 = plt.subplot2grid((3, 3), (2, 0), colspan=1, rowspan=1)
ax5 = plt.subplot2grid((3, 3), (2, 1), colspan=1, rowspan=1)

# # method 2 girdspec
# plt.figure()
# gs = gridspec.GridSpec(3, 3)
# ax1 = plt.subplot(gs[0, :])
# ax2 = plt.subplot(gs[1, :2])
# ax3 = plt.subplot(gs[1:, 2])
# ax4 = plt.subplot(gs[2, 0])
# ax5 = plt.subplot(gs[2, 1])

# # method 3 easy to define structure
# f, ((ax11, ax12), (ax21, ax22)) = plt.subplots(2, 2, sharex=True, sharey=True)
# ax11.scatter([1, 2], [2, 1])

plt.show()

七、图中图

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
x = [1, 2, 3, 4, 5, 6, 7]
y = [1, 3, 4, 2, 5, 8, 6]
# 大图
left, bottom, width, height = 0.1, 0.1, 0.8, 0.8   # 相对于figure的比例
ax1 = fig.add_axes([left, bottom, width, height])
ax1.plot(x, y, 'r')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_title('title')

# 小图 1
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')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_title('title inside 1')

# 小图2 method 2
plt.axes([0.6, 0.2, 0.25, 0.25])
plt.plot(y[::-1], x, 'g')
plt.xlabel('x')
plt.ylabel('y')
plt.title('title inside 2')

plt.show()

八、主次坐标轴

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 10, 0.1)
y1 = 0.05 * x**2
y2 = -1 * y1

fig, ax1 = plt.subplots()
ax2 = ax1.twinx()   # 在右侧镜像产生1个y轴
ax1.plot(x, y1, 'g-')
ax2.plot(x, y2, 'b--')

ax1.set_xlabel('X data')
ax1.set_ylabel('Y1', color='g')
ax2.set_ylabel('Y2', color='b')

plt.show()

九、Animation动画

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import animation

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))


def animate(i):
    line.set_ydata(np.sin(x + i/10))
    return line

def init():
    line.set_ydata(np.sin(x))
    return line

ani = animation.FuncAnimation(fig=fig, func=animate, frames=100, init_func=init, interval=20, blit=False)
                                                     # 100 帧;20ms更新一次
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值