你好stackoverflow社区,
我使用Anaconda与python 3.5在mac与os sierra。我有一个广泛的脚本,其中包含来自matplotlib的几个绘图命令。这很好,即使我绘制超过1000个数字。我想下载笔记本,在spyder中使用它作为。py脚本。
我相当肯定,它不包含错误,如果我在jupyter笔记本中运行它,它就能正常工作。但是使用spyder,我得到一堆错误消息:
runfile('/Users/user/imb-buoy-scripts/ProblemsWithPlotting.py',wdir='
/Users/user/imb-buoy-scripts')
<matplotlib.figure.Figure at 0x11f5836a0>
Traceback (most recent call last):
File "/Applications/anaconda/lib/python3.5/site-packages/IPython
/core/formatters.py", line 339, in __call__
return printer(obj)
File "/Applications/anaconda/lib/python3.5/site-packages/IPython
/core/pylabtools.py", line 228, in <lambda>
png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png',
**kwargs))
File "/Applications/anaconda/lib/python3.5/site-packages/IPython
/core/pylabtools.py", line 119, in print_figure
fig.canvas.print_figure(bytes_io, **kw)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/backend_bases.py", line 2180, in print_figure
**kwargs)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/backends/backend_agg.py", line 527, in print_png
FigureCanvasAgg.draw(self)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/backends/backend_agg.py", line 474, in draw
self.figure.draw(self.renderer)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/figure.py", line 1159, in draw
func(*args)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/axes/_base.py", line 2324, in draw
a.draw(renderer)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/artist.py", line 61, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/axis.py", line 1108, in draw
renderer)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/axis.py", line 1058, in _get_tick_bboxes
extent = tick.label1.get_window_extent(renderer)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/text.py", line 961, in get_window_extent
bbox, info, descent = self._get_layout(self._renderer)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/text.py", line 352, in _get_layout
ismath=False)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/backends/backend_agg.py", line 229, in get_text_width_height_descent
renderer=self)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/texmanager.py", line 678, in get_text_width_height_descent
page = next(iter(dvi))
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/dviread.py", line 89, in __iter__
have_page = self._read()
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/dviread.py", line 150, in _read
self._dispatch(byte)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/dviread.py", line 233, in _dispatch
self._fnt_def(k, c, s, d, a, l, n)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/dviread.py", line 388, in _fnt_def
tfm = _tfmfile(fontname)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/dviread.py", line 897, in _tfmfile
return _fontfile(texname, Tfm, '.tfm', _tfmcache)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/dviread.py", line 887, in _fontfile
filename = find_tex_file(texname + suffix)
File "/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/dviread.py", line 868, in find_tex_file
stderr=subprocess.PIPE)
File "/Applications/anaconda/lib/python3.5/subprocess.py", line 950,
in __init__
restore_signals, start_new_session)
File "/Applications/anaconda/lib/python3.5/subprocess.py", line 1544,
in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'kpsewhich'
如果有人能帮助我或给我指出正确的方向,我将非常感激。谢谢你的关注:)
编辑:原因在于我在matplotlib中使用的字体的定义:下面是示例代码:
# coding: utf-8
# # Problems with plots after migrating from .ipynb to .py
# In[2]:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.gridspec as gs
from matplotlib import rc # this is the matplotlib suggestion
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=True)
# In[3]:
mat = np.random.rand(5,5)
# In[4]:
fig = plt.figure()
gsall = gs.GridSpec(6, 1)
ax1 = fig.add_subplot(gsall[:])
source1 = ax1.contourf(mat,extend='both',cmap=mpl.cm.viridis)
plt.title('Random field n test')
plt.colorbar(source1)
# In[5]:
plt.show()
所以如果我注释rc('text'),我得到这个新的错误消息,我被告知必须与matplotlib有关。只需将字体设置为Arial而不是Helvetica就可以解决这个问题。
/Applications/anaconda/lib/python3.5/site-packages/matplotlib
/font_manager.py:1288: UserWarning: findfont: Font family
['sans-serif'] not found. Falling back to Bitstream Vera Sans
(prop.get_family(), self.defaultFamily[fontext]))
上面提到的错误是由这个家伙创建的,关于在matplotlib中使用latex:
rc('text', usetex=True)
http://matplotlib.org/users/usetex.html我在代码中所拥有的正是来自matplotlib的示例。
在in[4]和in[5]中错误地使用了plt而不是plt的"fig"实例
def plot_tst():
'''
'''
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.gridspec as gs
mat = np.random.rand(5,5)
fig = plt.figure()
gsall = gs.GridSpec(6, 1)
ax1 = fig.add_subplot(gsall[:])
source1 = ax1.contourf(mat,extend='both',cmap=mpl.cm.viridis)
fig.suptitle('Random field n test')
fig.colorbar(source1)
plot_tst()
https://i.stack.imgur.com/0GCrv.png 我还将title()更改为suptitle(),但真的不明白为什么title()不起作用但是有关于Windows和Spyder的控制台图形问题的评论
(我在win7上运行Spyder 2.39, Spyder现在有3.0.1版本)
我能够在Ipython控制台上重现这个例子,没有任何问题。
Spyder测试截图
只需要
- 最新版本spyder (3.0.1)
- sudo apt-get install dvipng(用于在matplotlib上管理latex字符转换的库)