美文网首页
Matplotlib模块-阶段三

Matplotlib模块-阶段三

作者: 是我真的是我 | 来源:发表于2019-11-15 16:05 被阅读0次
  • image
  • 3D图形
  • 多合一显示最简版
  • 多合一显示(1)
  • 多合一显示(2)
  • 多合一显示(3)
  • 图中图
  • 次坐标轴
  • 动画

image

import numpy as np
from matplotlib import pyplot as plt

a = np.array([0.1, 0.2, 0.3,
              0.4, 0.5, 0.6,
              0.7, 0.8, 0.9]).reshape(3, 3)

'''
    interpolation 取值介绍
        http://matplotlib.org/examples/images_contours_and_fields/interpolation_methods.html
'''
plt.imshow(a, interpolation='nearest', cmap='bone', origin='lower')

plt.colorbar(shrink=1)

# 设置坐标不可见
plt.xticks(())
plt.yticks(())

plt.show()

interpolation 运作结果

3D图形

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

'创建窗口'
fig = plt.figure()
'创建3D坐标'
ax = Axes3D(fig)

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)
z = np.sin(r)

'画图。rstride:跨度'
ax.plot_surface(x, y, z, rstride=1, cstride=1,
                cmap=plt.get_cmap('rainbow'), edgecolor='black')

'画投影,zdir投影方向'
ax.contourf(x, y, z, zdir='z', offset=-2, cmap='rainbow')
ax.set_zlim(-2, 2)

plt.show()

运行结果
pycharm设置图形可以旋转

多合一显示最简版

import numpy as np
from matplotlib import pyplot as plt

plt.figure()

'分为n行m列,该小图位于该划分第k个位置'
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()

运行结果

多合一显示(1)

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

plt.figure()

'创建3行3列,(索引从0开始)从0行0个开始,纵跨度是3'
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3, rowspan=1)
ax1.plot([1, 2], [1, 2])
ax1.set_title('ax1_title')

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))

plt.show()

运行结果

多合一显示(2)

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

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[-1, 0])
ax5 = plt.subplot(gs[-1, -2])

plt.show()

运行结果

多合一显示(3)

import numpy as np
from matplotlib import pyplot as plt

f, ((ax11, ax12), (ax21, ax22)) \
    = plt.subplots(2, 2, sharex=True, sharey=True)

ax11.scatter([1, 2], [1, 2])

plt.tight_layout()
plt.show()

运行结果

图中图

import numpy as np
from matplotlib import pyplot as plt

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
'设置 ax1 对象的位置'
ax1 = fig.add_axes([left, bottom, width, height])
ax1.plot(x, y, 'red')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_title('title')

'设置小图'
left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
'设置 ax1 对象的位置'
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')

'设置小图'
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 numpy as np
from matplotlib import pyplot as plt

'初始化数据'
x = np.arange(0, 10, 0.1)
y1 = 0.05 * x ** 2
y2 = -1 * y1

fig, ax1 = plt.subplots()
'设置ax2是ax1的镜面函数便于展示'
ax2 = ax1.twinx()

'设置两条线'
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()

运行结果

动画

import numpy as np
from matplotlib import pyplot as plt
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 / 100))
    return line,

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

'frames帧, interval频率, blit=True只更新变化的'
ani = animation.FuncAnimation(fig=fig, func=animate,
                              frames=100,
                              init_func=init,
                              interval=20, blit=False)

plt.show()

运行结果

相关文章

网友评论

      本文标题:Matplotlib模块-阶段三

      本文链接:https://www.haomeiwen.com/subject/jfvamctx.html