Matplotlib在ubuntu 14.04上没有显示正确的字体

时间:2014-08-13 20:25:27

标签: python ubuntu matplotlib

我从源

安装了所有依赖于ubuntu 14.04的matplotlib
Processing dependencies for matplotlib==1.3.1
Searching for nose==1.3.3
Best match: nose 1.3.3
Processing nose-1.3.3-py2.7.egg
Removing nose 1.3.1 from easy-install.pth file
nose 1.3.3 is already the active version in easy-install.pth
Installing nosetests script to /usr/local/bin
Installing nosetests-2.7 script to /usr/local/bin
Using /usr/local/lib/python2.7/dist-packages/nose-1.3.3-py2.7.egg
Searching for pyparsing==2.0.1
Best match: pyparsing 2.0.1
Adding pyparsing 2.0.1 to easy-install.pth file
Using /usr/lib/python2.7/dist-packages
Searching for tornado==3.1.1
Best match: tornado 3.1.1
tornado 3.1.1 is already the active version in easy-install.pth
Using /usr/lib/python2.7/dist-packages
Searching for python-dateutil==1.5
Best match: python-dateutil 1.5
python-dateutil 1.5 is already the active version in easy-install.pth
Using /usr/lib/python2.7/dist-packages
Searching for numpy==1.8.1
Best match: numpy 1.8.1
numpy 1.8.1 is already the active version in easy-install.pth

当我尝试绘制某些内容时,matplotlib没有显示正确的x刻度,你可以从这里看到 http://bayanbox.ir/id/4106587232464013527?view

来源:

import matplotlib.pyplot as plt
import matplotlib
import pandas as pd
import numpy as np
df=pd.DataFrame({'Val': np.random.random(50)})
df.index=pd.date_range('2000-01-02', periods=50)
plt.plot_date(df.index.to_pydatetime(), df.Val, fmt='-')
ax=plt.gca()
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%y%b\n%d'))
plt.show()

1 个答案:

答案 0 :(得分:2)

Matplotlib使用strftime来处理刻度中的日期格式。 strftime将使用您计算机的区域设置选择某些日期格式的正确版本,例如英语与德语的“1月”与“Januar”。

您的问题出现是因为您的默认语言是波斯语。 Matplotlib试图使用波斯语进行绘图,但未能正确编码(导致正方形)。

您可以选择更改计算机的默认语言,或使用locale内置模块设置Python代码的locale