matplotlib 动画 绘图 延时与暂停
2016-07-29 13:46
363 查看
matplotlib快速入门
显示所有支持的中文字体
# -*- coding: utf-8 -*-
print "fonts"
import os
from os import path
import matplotlib.pyplot as plt
from matplotlib.font_manager import fontManager
def show_chinese_fonts():
fig = plt.figure(figsize=(12, 6))
ax = fig.add_subplot(111)
plt.subplots_adjust(0, 0, 1, 1, 0, 0)
plt.xticks([])
plt.yticks([])
x, y = 0.05, 0.05
fonts = [font.name for font in fontManager.ttflist if
path.exists(font.fname) and os.stat(font.fname).st_size>1e6] #❶
font = set(fonts)
dy = (1.0 - y) / (len(fonts) // 4 + (len(fonts)%4 != 0))
for font in fonts:
t = ax.text(x, y + dy / 2, u"中文字体",
{'fontname':font, 'fontsize':14}, transform=ax.transAxes) #❷
ax.text(x, y, font, {'fontsize':12}, transform=ax.transAxes)
x += 0.25
if x >= 1.0:
y += dy
x = 0.05
plt.show()
if __name__ == '__main__':
show_chinese_fonts()
绘图延时举例
import numpy as np
import matplotlib.pyplot as plt
plt.axis([0, 10, 0, 1])
plt.ion()
for i in range(10):
y = np.random.random()
plt.scatter(i, y)
plt.pause(0.05)
while True:
plt.pause(0.05)
调用 plt.ion() 可以使能交互绘图功能. plt.show( ) 显示所绘制的图形
调用 plt.pause(0.05) 不仅可以绘图而且鼠标可以继续交互
最后的while循环保证界面不消失
暂停动画
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
pause = False
def simData():
t_max = 10.0
dt = 0.05
x = 0.0
t = 0.0
while t < t_max:
if not pause:
x = np.sin(np.pi*t)
t = t + dt
yield x, t
def onClick(event):
global pause
pause ^= True
def simPoints(simData):
x, t = simData[0], simData[1]
time_text.set_text(time_template%(t))
line.set_data(t, x)
return line, time_text
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot([], [], 'bo', ms=10) # I'm still not clear on this stucture...
ax.set_ylim(-1, 1)
ax.set_xlim(0, 10)
time_template = 'Time = %.1f s' # prints running simulation time
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
fig.canvas.mpl_connect('button_press_event', onClick)
ani = animation.FuncAnimation(fig, simPoints, simData, blit=False, interval=10,
repeat=True)
plt.show()
绘制表格
# -*- coding: utf-8 -*-
import numpy as np
def draw_grid(grid, size=6, margin=0.02, fontsize=16):
from matplotlib import pyplot as plt
cols = len(grid[0])
rows = len(grid)
aspect = rows / float(cols)
fig, ax = plt.subplots(figsize=(6, 6*aspect))
fig.subplots_adjust(0, 0, 1, 1)
ax.set_axis_off()
fig.patch.set_facecolor("white")
line_props = dict(transform=ax.transAxes, lw=1, color="#777777")
width = (1-2*margin) / cols
height = (1-2*margin) / rows
for i in np.linspace(margin, 1-margin, rows+1):
line = plt.Line2D([margin, 1-margin], [i, i], **line_props)
ax.add_artist(line)
for i in np.linspace(margin, 1-margin, cols+1):
line = plt.Line2D([i, i], [margin, 1-margin], **line_props)
ax.add_artist(line)
for (r, c), v in np.ndenumerate(grid):
text = plt.Text(margin + c*width + width*0.5,
margin + (rows-r-1)*height + height*0.5,
"%s" % v, transform=ax.transAxes,
va="center", ha="center", fontsize=fontsize)
ax.add_artist(text)
fig.show()
if __name__ == "__main__":
import numpy as np
grid = np.random.randint(0, 10, (4, 6))
print grid
draw_grid(grid)
显示所有支持的中文字体
# -*- coding: utf-8 -*-
print "fonts"
import os
from os import path
import matplotlib.pyplot as plt
from matplotlib.font_manager import fontManager
def show_chinese_fonts():
fig = plt.figure(figsize=(12, 6))
ax = fig.add_subplot(111)
plt.subplots_adjust(0, 0, 1, 1, 0, 0)
plt.xticks([])
plt.yticks([])
x, y = 0.05, 0.05
fonts = [font.name for font in fontManager.ttflist if
path.exists(font.fname) and os.stat(font.fname).st_size>1e6] #❶
font = set(fonts)
dy = (1.0 - y) / (len(fonts) // 4 + (len(fonts)%4 != 0))
for font in fonts:
t = ax.text(x, y + dy / 2, u"中文字体",
{'fontname':font, 'fontsize':14}, transform=ax.transAxes) #❷
ax.text(x, y, font, {'fontsize':12}, transform=ax.transAxes)
x += 0.25
if x >= 1.0:
y += dy
x = 0.05
plt.show()
if __name__ == '__main__':
show_chinese_fonts()
绘图延时举例
import numpy as np
import matplotlib.pyplot as plt
plt.axis([0, 10, 0, 1])
plt.ion()
for i in range(10):
y = np.random.random()
plt.scatter(i, y)
plt.pause(0.05)
while True:
plt.pause(0.05)
调用 plt.ion() 可以使能交互绘图功能. plt.show( ) 显示所绘制的图形
调用 plt.pause(0.05) 不仅可以绘图而且鼠标可以继续交互
最后的while循环保证界面不消失
暂停动画
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
pause = False
def simData():
t_max = 10.0
dt = 0.05
x = 0.0
t = 0.0
while t < t_max:
if not pause:
x = np.sin(np.pi*t)
t = t + dt
yield x, t
def onClick(event):
global pause
pause ^= True
def simPoints(simData):
x, t = simData[0], simData[1]
time_text.set_text(time_template%(t))
line.set_data(t, x)
return line, time_text
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot([], [], 'bo', ms=10) # I'm still not clear on this stucture...
ax.set_ylim(-1, 1)
ax.set_xlim(0, 10)
time_template = 'Time = %.1f s' # prints running simulation time
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
fig.canvas.mpl_connect('button_press_event', onClick)
ani = animation.FuncAnimation(fig, simPoints, simData, blit=False, interval=10,
repeat=True)
plt.show()
绘制表格
# -*- coding: utf-8 -*-
import numpy as np
def draw_grid(grid, size=6, margin=0.02, fontsize=16):
from matplotlib import pyplot as plt
cols = len(grid[0])
rows = len(grid)
aspect = rows / float(cols)
fig, ax = plt.subplots(figsize=(6, 6*aspect))
fig.subplots_adjust(0, 0, 1, 1)
ax.set_axis_off()
fig.patch.set_facecolor("white")
line_props = dict(transform=ax.transAxes, lw=1, color="#777777")
width = (1-2*margin) / cols
height = (1-2*margin) / rows
for i in np.linspace(margin, 1-margin, rows+1):
line = plt.Line2D([margin, 1-margin], [i, i], **line_props)
ax.add_artist(line)
for i in np.linspace(margin, 1-margin, cols+1):
line = plt.Line2D([i, i], [margin, 1-margin], **line_props)
ax.add_artist(line)
for (r, c), v in np.ndenumerate(grid):
text = plt.Text(margin + c*width + width*0.5,
margin + (rows-r-1)*height + height*0.5,
"%s" % v, transform=ax.transAxes,
va="center", ha="center", fontsize=fontsize)
ax.add_artist(text)
fig.show()
if __name__ == "__main__":
import numpy as np
grid = np.random.randint(0, 10, (4, 6))
print grid
draw_grid(grid)
相关文章推荐
- CSS3眨眼效果(animation动画循环间的“暂停”、“延时”执行)
- 暂停 继续动画实现方法
- 动画暂停和继续
- 【Android API指南】动画和图像(4) - 画布和绘图资源
- WPF控制动画开始、停止、暂停和恢复
- 动画—CALayer的初步学习二:CALayer绘图—通过代理方法绘图(学会从博客上自学)
- android动画实现一张图自动旋转、可随时暂停、停止和重置,类似秒表指针旋转动画
- 纯 CSS 方式实现 CSS 动画的暂停与播放
- 暂停、恢复CALayer 动画
- gifplayer-可控制GIF动画图片播放和暂停jQuery插件
- iOS --- 如何暂停和继续CALayer上的动画
- 切换界面之后 SwipeRefreshLayout 动画效果暂停,以及不消失的解决方案
- 绘图与动画学习笔记(二)
- 学习Canvas绘图与动画基础 制作弧和圆(五)
- Android绘图之渐隐动画
- ios 动画与2D、3D绘图
- Swift使用CoreAnimation实现旋转动画,包括暂停旋转和继续旋转方法
- 暂停/继续 动画
- HTML5-炫丽的时钟效果Canvas绘图与动画