python在画布上显示动态图片_python – matplotlib – 直接在画布上绘图
由于动态更新的性能问题,我需要在画布上直接绘制很多矩形作为非常低的级别,也就是说不使用matplotlib.patches,因为我们必须使用经典的GUI.
更准确地说,我想只绘制一个矩形,而不仅仅是所有的图形.
可能吗 ?
这是我使用Joe Kington给出的链接的测试代码.
#!/usr/bin/env python3
from random import randint, choice
import time
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.colors as colors
import matplotlib
back_color = "black"
colors = ['red', 'green', 'cyan', 'yellow']
width = 16
height = 16
ax = plt.subplot(111)
canvas = ax.figure.canvas
ax.set_xlim([0, width])
ax.set_ylim([0, height])
def update():
global ax, canvas, colors, width, height
x = randint(0, width - 1)
y = randint(0, height - 1)
rect = mpatches.Rectangle(
(x, y), 1, 1,
facecolor = choice(colors),
edgecolor = back_color
)
start = time.time()
ax.draw_artist(rect)
canvas.blit(ax.bbox)
print("draw >>>", time.time() - start)
timer = canvas.new_timer(interval = 1)
timer.add_callback(update)
timer.start()
plt.show()
在Mac O $下,我获得以下消息.
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/backend_bases.py", line 1203, in _on_timer
ret = func(*args, **kwargs)
File "/Users/xxx/test.py", line 86, in update
ax.draw_artist(rect)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/axes.py", line 2100, in draw_artist
a.draw(self._cachedRenderer)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/artist.py", line 56, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/patches.py", line 393, in draw
gc = renderer.new_gc()
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/matplotlib/backends/backend_macosx.py", line 97, in new_gc
self.gc.save()
RuntimeError: CGContextRef is NULL
对于Maverick用户
对于想要使用以下代码以及动画的Mac用户,我有一个好消息.我在没有任何软件的Mac上重新安装了Maverick OS,这称为干净安装,然后我安装了Anaconda和XQuark(see this).
要使动画有效,只需在任何其他matplotlib导入之前使用以下两行.
import matplotlib
matplotlib.use('TkAgg')
我认为这适用于Anaconda支持的任何Mac OS.只有Maverick才需要使用XQuark.
解决方法:
您当前的代码有两个问题.
>你没有先画出画布而试图搞砸.你得到的错误是后端特定的,但基本上,它说的是画布的渲染器还没有被初始化.
>您没有将矩形艺术家添加到轴.因此,调用ax.draw_artist(rect)时不会被绘制.
这是一个有效的例子:
from random import randint, choice
import time
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
back_color = "black"
colors = ['red', 'green', 'cyan', 'yellow']
width, height = 16, 16
fig, ax = plt.subplots()
ax.set(xlim=[0, width], ylim=[0, height]) # Or use "ax.axis([x0,x1,y0,y1])"
# Be sure to draw the canvas once before we start blitting. Otherwise
# a) the renderer doesn't exist yet, and b) there's noting to blit onto
fig.canvas.draw()
def update():
x = randint(0, width - 1)
y = randint(0, height - 1)
rect = mpatches.Rectangle(
(x, y), 1, 1,
facecolor = choice(colors),
edgecolor = back_color
)
ax.add_artist(rect)
start = time.time()
ax.draw_artist(rect)
fig.canvas.blit(ax.bbox)
print("draw >>>", time.time() - start)
timer = fig.canvas.new_timer(interval=1)
timer.add_callback(update)
timer.start()
plt.show()
但是,在这一点上,每次添加新艺术家都会更有意义.相反,添加初始矩形并每次更新它.例如:
from random import randint, choice
import time
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
back_color = "black"
colors = ['red', 'green', 'cyan', 'yellow']
width, height = 16, 16
fig, ax = plt.subplots()
ax.set(xlim=[0, width], ylim=[0, height]) # Or use "ax.axis([x0,x1,y0,y1])"
rect = mpatches.Rectangle(
(0, 0), 1, 1,
facecolor = choice(colors),
edgecolor = back_color
)
ax.add_artist(rect)
# Be sure to draw the canvas once before we start blitting. Otherwise
# a) the renderer doesn't exist yet, and b) there's noting to blit onto
fig.canvas.draw()
def update():
x = randint(0, width - 1)
y = randint(0, height - 1)
rect.set(xy=[x,y], facecolor=choice(colors))
start = time.time()
ax.draw_artist(rect)
fig.canvas.blit(ax.bbox)
print("draw >>>", time.time() - start)
timer = fig.canvas.new_timer(interval=1)
timer.add_callback(update)
timer.start()
plt.show()
标签:python,matplotlib
来源: https://codeday.me/bug/20190708/1405723.html
python在画布上显示动态图片_python – matplotlib – 直接在画布上绘图相关推荐
- python在画布上显示动态图片_python Tkinter在画布上显示图像,它总是blin
我正在从相机捕捉图像,并将图像显示在Tkinter的画布上. 但是,出现了一个奇怪的现象,画布将闪烁下面的代码def get_image_cam() : global cam,cam_flag,cam ...
- python浮点型数据怎么显示为图片_python数字图像处理(4):图像数据类型及颜色空间转换...
一.图像数据类型及转换 在skimage中,一张图片就是一个简单的numpy数组,数组的数据类型有很多种,相互之间也可以转换.这些数据类型及取值范围如下表所示: Data typeRange uint ...
- python散点图图例只显示一个标记点_python – Matplotlib图例:如何分配多个散点值...
我在python中使用matplotlib库来生成出版品质的xy散点图.我遇到了关于图例中标记的问题.我正在绘制2个不同的xy-scatter系列;一个是形成曲线的一组xy点,另一个是单个xy点. 我 ...
- linux开发板lcd上显示jpg图片,关于在嵌入式开发板上显示一张jpeg图片
#include "lcdjpg.h" static char g_color_buf[FB_SIZE]={0}; static int g_fb_fd; static int ...
- Django在浏览器上显示本地图片
Django项目在浏览器上显示本地图片,关键在于url的配置. 代码如下: from django.views.static import serve url(r'^book/(?P<path& ...
- zigbee无线传感网实训---在LCD屏上显示JPG图片、 触摸屏、相册(The fourth day)
b 承接实训第三天:zigbee无线传感网实训---LCD显示bmp图片及一些简单的c语言编程功能( On the third day) 一.修改实训第三天中练习2中的bug(在原码的基础上增加:ls ...
- java jframe显示图片_java怎么在JFrame中显示动态图片
java怎么在JFrame中显示动态图片 (2012-09-16 23:39:54) 标签: 杂谈 import java.awt.Graphics; import javax.swing.Image ...
- java实现gif动画效果(java显示动态图片)
关闭 关闭 脚本之家 软件下载 源码下载 在线工具 网页教程基础 服务器常用软件 手机版 关注微信 网页制作 网络编程 脚本专栏 脚本下载 数据库 CMS教程 电子书籍 平面设计 媒体动画 操作系统 ...
- java动态图片_java实现gif动画效果(java显示动态图片)
关闭 关闭 java实现gif动画效果(java显示动态图片) 作者: 字体:[增加 减小] 类型:转载 时间:2014-04-29我要评论 这篇文章主要介绍了java实现gif动画效果示例(java ...
最新文章
- “强化学习之父”萨顿:预测学习马上要火,AI将帮我们理解人类意识
- [Nhibernate]对象状态
- python 如何建立图形用户界面_python(五)图形用户界面easyGUI入门
- 贝叶斯网络结构学习之K2算法(基于FullBNT-1.0.4的MATLAB实现)
- Android第三十一期 - 市面上所有引导页的效果
- 微型计算机总线不包括,微型计算机总线不包括( )。
- No module named cv2
- sc169 lecture note
- delete mysql 大表_无语了,直到今天,我才揪出MySQL磁盘消耗迅猛的“真凶”!
- 20171207L09-04老男孩Linux运维实战培训-Lamp系列-Apache服务生产实战应用
- [css] 请使用css写一个多级的下拉菜单
- 浏览器与JavaScript(一)
- 神奇的linux发行版 tiny core linux
- 如何选择WEB报表工具(二)
- C4D立体数字设计灵感,适合庆典应用|这波用得6啊!
- python 消息框架_消息框架message
- Java几款性能分析工具的对比
- android 获取monkey日志_Monkey日志如何分析
- Elasticsearch: Cerebro 用户界面介绍
- django缓存优化(一)