ubuntu16.04下使用matplotlib出现`TypeError: Couldn't find foreign struct converter for 'cairo.Context'`解决

来源:互联网 发布:软件测试简单吗 编辑:程序博客网 时间:2024/05/19 00:51

博主使用的时ubuntu16.04下的sublime text3配置python2运行环境,已经成功安装numpy,scipy,matplotlib环境,在测试代码
`import numpy as np
from scipy.stats import beta
from matplotlib.pyplot import hist, plot, show

obs = beta.rvs(5, 5, size=2000) # 2000 observations
hist(obs, bins=40, normed=True)
grid = np.linspace(0.01, 0.99, 100)
plot(grid, beta.pdf(grid, 5, 5), ‘k-‘, linewidth=2)
show()
时,遇到了以下问题:
TypeError: Couldn’t find foreign struct converter for ‘cairo.Context’`
并且弹出来一个空白的figure图框
经过’呕心沥血’的探索,终于找到了解决方案:
I assume you have installed the pip matplotlib, there is a directory in you root called ~ /. matplotlib. Create a file called matplotlibrc there and following code

~ / .matplotlib / Matplotlibrc
backend: TkAgg

就是你先进入你的home目录,按ctrl+h显示所有隐藏文件夹,找到一个叫matplotlib的文件夹,在里面创建一个叫做matplotlibrc的文件,在文件里输入以下代码
~ / .matplotlib / Matplotlibrc
backend: TkAgg

关闭并保存文件,把代码中的目录改成你电脑上matplotlib文件夹所在的目录。例如我的matplotlib文件夹路径是”~ / .config / matplotlib / “
我的matplotlibrc文件中代码就是
~ / .config / matplotlib / Matplotlibrc
backend: TkAgg

<注意>:我的代码没有写错,Matplotlibrc的m就是大写。
这种方法在python3中应该也适用(欢迎道友测试)。
最后在sublime text3输入测试代码,图标出来了:这里写图片描述
Perfect!

阅读全文
0 0