您的位置:首页 > 编程语言 > PHP开发

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  matplotlib python