Skip to content

Commit 643c994

Browse files
committed
Add faq for using matplotlib
1 parent d82fcfc commit 643c994

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

docs/faq.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,33 @@ virtualenv.bundle.zip添加到module的搜索路径中,示例代码如下: :
185185
2. 有些包是not-zip-safe的,可能不工作,有待验证。 含有c扩展的package
186186
不能工作。
187187

188+
Matplotlib使用常见问题
189+
-----------------------
190+
191+
SAE环境不支持matplotlib的interative模式,所以无法使用 `pyplot.show()` 直接来显示图像,只能使用
192+
`pyplot.savefig()` 将图像保存到一个输出流中(比如一个cStringIO.StringIO的实例中)。
193+
194+
如果想要在matplotlib中显示中文,可以使用以下任一方法。
195+
196+
方法一: ::
197+
198+
import os.path
199+
from matplotlib.font_manager import FontProperties
200+
zh_font = FontProperties(fname=os.path.abspath('wqy-microhei.ttf'))
201+
import matplotlib.pyplot as plt
202+
plt.title(u'中文', fontproperties=zh_font)
203+
204+
方法二: ::
205+
206+
import os
207+
# 设置自定义字体文件所在目录路径,多条路径之间使用分号(:)隔开
208+
os.environ['TTFPATH'] = os.getcwd()
209+
import matplotlib
210+
# 设置默认字体名
211+
matplotlib.rcParams['font.family'] = 'WenQuanYi Micro Hei'
212+
import matplotlib.pyplot as plt
213+
plt.title(u'中文')
214+
215+
其中方法一适用于ttf和ttc字体,方法二适用于只适用于ttf字体
216+
217+
如果有 `matplotlibrc` 配置文件,请将该文件与index.wsgi放在同一个目录下(默认的当前路径)。

0 commit comments

Comments
 (0)