Tools程序员

python之matplotlib中plt.show()不显示图

2016-05-28  本文已影响24137人  ctycheer

问题

最近,我重新安装了ubuntu,使用virtualenv安装了matplotlib。然后,问题来了。当我运行下列代码时,没有图框跳出来。
<code>
import matplotlib.pyplot as plt
plt.show()
plt.bar(left = 0,height = 1)
</code>

原因

我使用%pylab查看matplotlib后端,发现居然是agg。兄弟姐妹们,agg是不会画图的!
<code>
In [4]:%pylab
Using matplotlib backend: agg
Populating the interactive namespace from numpy and matplotlib
</code>

解决

1. 安装Tkinter 和 matplotlib

我的问题在于我原来的python居然没有Tkinter!!
<code>
sudo apt-get install tk-dev
pip uninstall -y matplotlib
pip --no-cache-dir install -U matplotlib #这是最关键的
</code>

2. 设置agg

其实经过上面的步骤,已经可以画图了
补充2种设置agg方法

至此,matplotlib可以正常工作了。

上一篇下一篇

猜你喜欢

热点阅读