不同版本中Python matplotlib.pyplot.draw()界面绘制异常问题的解决
作者:默默的点滴 时间:2021-04-23 03:41:16
前言
本文主要给大家介绍了关于不同版本中Python matplotlib.pyplot.draw()界面绘制异常的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
在 Ubuntu系统上进行如下配置:
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install python-dev
$ sudo apt-get install python-pip
$ sudo pip install --upgrade pip
$ sudo pip install --upgrade urllib3
$ sudo pip install numpy
$ sudo pip install matplotlib
之后执行如下测试代码:
import sys
import numpy as np
import matplotlib.pyplot as plt
plt.ion()
(fig, axis) = plt.subplots()
bar_plot = axis.barh(0, 8,linewidth = 0)
bar_plot.color= '#ffff00'
for i in range(20):
axis.set_xlim(xmax = max(i + 1, 10))
plt.draw()
if sys.version_info < (3, 0):
raw_input("Press Enter to continue...")
else:
input("Press Enter to continue...")
上面的测试代码在 Ubuntu 14.04.5版本上是可以正常执行的,对应的 matplotlib的版本是 matplotlib 1.3.1,但是放到 Ubuntu 16.04.2系统上则是无法正常显示的,对应的 matplotlib的版本是 matplotlib 1.5.1。
造成这个问题的原因在于 matplotlib.pyplot.draw()
,这个函数行为的改变,早期这个函数是同步更新界面的,后来的版本却变成了空闲异步更新界面,只有当 matplotlib.pyplot.pause(interval)
被调用的时候才会刷新界面。
所以只需要上面的代码修改成如下即可在不同版本之间兼容:
import sys
import numpy as np
import matplotlib.pyplot as plt
plt.ion()
(fig, axis) = plt.subplots()
bar_plot = axis.barh(0, 8,linewidth = 0)
bar_plot.color= '#ffff00'
for i in range(20):
axis.set_xlim(xmax = max(i + 1, 10))
plt.draw()
plt.pause(0.00001)
if sys.version_info < (3, 0):
raw_input("Press Enter to continue...")
else:
input("Press Enter to continue...")
注意:我们在 matplotlib.pyplot.draw()
调用后面增加了 matplotlib.pyplot.pause(interval)
的调用。
查看 matplotlib的版本使用如下代码:
import matplotlib as mpl
print mpl.__version__
总结
参考链接
real-time plotting in while loop with matplotlib
matplotlib Troubleshooting
How to know the version of installed pylab?
来源:http://www.mobibrw.com/2017/8476
标签:python,matplotlib.pyplot.draw(),界面绘制异常
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Pytorch实现将label变成one hot编码的两种方式
2021-08-26 08:45:00
python3实现带多张图片、附件的邮件发送
2023-05-11 06:51:10
python环形单链表的约瑟夫问题详解
2023-03-02 04:13:10
如何利用Python实现简单C++程序范围分析
2022-07-19 00:32:48
Python+Opencv识别两张相似图片
2022-11-07 09:12:55
![](https://img.aspxhome.com/file/2023/0/97070_0s.jpg)
使用Python的Bottle框架写一个简单的服务接口的示例
2023-09-01 17:29:50
Python中使用异常处理来判断运行的操作系统平台方法
2021-03-28 07:37:23
python snownlp情感分析简易demo(分享)
2021-07-18 04:32:35
![](https://img.aspxhome.com/file/2023/0/72250_0s.jpg)
python concurrent.futures模块的使用测试
2023-01-30 16:48:59
![](https://img.aspxhome.com/file/2023/9/91849_0s.jpg)
微信小程序图片左右摆动效果详解
2023-08-09 13:34:31
![](https://img.aspxhome.com/file/2023/6/56026_0s.gif)
python3 与python2 异常处理的区别与联系
2022-11-06 22:39:30
sqlalchemy实现时间列自动更新教程
2021-08-18 20:12:58
asp如何实现按照输入汉字提示拼音功能?
2010-05-18 18:37:00
python中的不可变数据类型与可变数据类型详解
2022-12-27 21:56:24
蜕变——记QQ医生3.0
2009-09-16 14:41:00
![](https://img.aspxhome.com/file/UploadPic/20099/16/0909-qqdoctor3-02-88s.jpg)
Python 打印自己设计的字体的实例讲解
2021-04-24 14:45:21
基于python3实现倒叙字符串
2023-02-02 17:41:28
![](https://img.aspxhome.com/file/2023/3/109603_0s.png)
利用python+ffmpeg合并B站视频及格式转换的实例代码
2021-06-09 21:14:00
自动生成sql语句
2008-05-09 12:42:00
python处理json字符串(使用json.loads而不是eval())
2023-06-13 11:50:39
![](https://img.aspxhome.com/file/2023/4/70794_0s.png)