我今天在kaggle上跑titanic的例子,发现matplotlib输出图像有问题,导出都是框框,所以怀疑是字体不对
import matplotlib
a=sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])
print(a)
结果:
['DejaVu Math TeX Gyre', 'DejaVu Sans', 'DejaVu Sans', 'DejaVu Sans', 'DejaVu Sans', 'DejaVu Sans', 'DejaVu Sans', 'DejaVu Sans', 'DejaVu Sans', 'DejaVu Sans', 'DejaVu Sans Display', 'DejaVu Sans Mono', 'DejaVu Sans Mono', 'DejaVu Sans Mono', 'DejaVu Sans Mono', 'DejaVu Serif', 'DejaVu Serif', 'DejaVu Serif', 'DejaVu Serif', 'DejaVu Serif', 'DejaVu Serif', 'DejaVu Serif', 'DejaVu Serif', 'DejaVu Serif Display', 'Droid Sans Fallback', 'Liberation Mono', 'Liberation Mono', 'Liberation Mono', 'Liberation Mono', 'Liberation Sans', 'Liberation Sans', 'Liberation Sans', 'Liberation Sans', 'Liberation Sans Narrow', 'Liberation Sans Narrow', 'Liberation Sans Narrow', 'Liberation Sans Narrow', 'Liberation Serif', 'Liberation Serif', 'Liberation Serif', 'Liberation Serif', 'Noto Mono', 'STIXGeneral', 'STIXGeneral', 'STIXGeneral', 'STIXGeneral', 'STIXNonUnicode', 'STIXNonUnicode', 'STIXNonUnicode', 'STIXNonUnicode', 'STIXSizeFiveSym', 'STIXSizeFourSym', 'STIXSizeFourSym', 'STIXSizeOneSym', 'STIXSizeOneSym', 'STIXSizeThreeSym', 'STIXSizeThreeSym', 'STIXSizeTwoSym', 'STIXSizeTwoSym', 'cmb10', 'cmex10', 'cmmi10', 'cmr10', 'cmss10', 'cmsy10', 'cmtt10']
import matplotlib.pyplot as plt
plt.rcParams['font.family'] = ['Droid Sans Fallback']# 用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号
import pandas as pd #数据分析
import numpy as np #科学计算
from pandas import Series,DataFrame
data_train = pd.read_csv("../input/titanic/train.csv")
# data_train
import matplotlib.pyplot as plt
fig = plt.figure()
fig.set(alpha=0.2) # 设定图表透明度alpha参数
# plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示中文标签
import matplotlib.pyplot as plt
myfont = matplotlib.font_manager.FontProperties(family="Droid Sans Fallback")#同一目录下,不同目录改用绝对路径
plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号
plt.subplot2grid((2,3),(0,0)) # 在一张大图里分列几个小图
data_train.Survived.value_counts().plot(kind='bar')# 柱状图
plt.title("获救情况(1为获救)",fontproperties=myfont) # 标题
plt.ylabel("人数",fontproperties=myfont)
网上看了好久,看了一篇文章
文章地址
# encoding=utf-8
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
plt.rcParams['font.family'] = ['Times New Roman']
plt.rcParams.update({'font.size': 8})
x = range(11)
y = range(11)
plt.plot(x, y)
plt.show()
plt.rcParams['font.family'] = ['Times New Roman']
plt.rcParams.update({'font.size': 8})
import pandas as pd #数据分析
import numpy as np #科学计算
from pandas import Series,DataFrame
import matplotlib
data_train = pd.read_csv("../input/titanic/train.csv")
# data_train
import matplotlib.pyplot as plt
fig = plt.figure()
fig.set(alpha=0.2) # 设定图表透明度alpha参数
# plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示中文标签
import matplotlib.pyplot as plt
# plt.rcParams['font.family'] = ['Droid Sans Fallback']
myfont = matplotlib.font_manager.FontProperties(family="Droid Sans Fallback")#同一目录下,不同目录改用绝对路径
# myfont = matplotlib.font_manager.fontManager(fname="cmr10.ttf")
plt.rcParams['font.family'] = ['Times New Roman']
plt.rcParams.update({'font.size': 8})
plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号
plt.subplot2grid((2,3),(0,0)) # 在一张大图里分列几个小图
data_train.Survived.value_counts().plot(kind='bar')# 柱状图
plt.title("获救情况 1为获救",fontproperties=myfont) # 标题
plt.ylabel("人数",fontproperties=myfont)
# plt.xlabel("人数",fontproperties=myfont)
最后输出:
myfont = matplotlib.font_manager.FontProperties(fname="Droid Sans Fallback.ttf")
会报错:FileNotFoundError: [Errno 2] No such file or directory: ‘Droid Sans Fallback.ttf’
如果您知道怎么解决,欢迎给我留言~~
虽然说还有一个框,但是不想管了。== 这个真的解决了好久~ 如果有用!给我转发评论点赞蟹蟹~