python画图包seaborn和matplotlib中文字体显示(针对windows系统,,使用ipython notebook)

本文介绍了解决Seaborn和Matplotlib在Windows系统上显示中文的问题,包括编码设置、字体配置及不同场景下的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、seaborn的中文字体显示(针对windows系统,,使用ipythonnotebook)

  • 首先:解决seaborn画图有中文时的编码异常错误

import seaborn as sns

import sys
# printsys.getdefaultencoding()
ipythonnotebook中默认是ascii编码 
reload(sys)
sys.setdefaultencoding('utf8') 

  • 其次:修改matplotlibrc配置文件中的字体名称
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei']#指定默认字体   
mpl.rcParams['axes.unicode_minus'] =False # 解决保存图像是负号'-'显示为方块的问题
#sns.axes_style(),可以看到是否成功设定字体为微软雅黑。

  • 最后:修改seaborn中的一些设置
sns.set_context("talk")
或者sns.set_context("poster")均可,该函数的其他的两个属性"notebook"和"paper"却不能正常显示中文。



二、matplotlib的中文字体显示(针对windows系统,,使用ipythonnotebook)

  • 方法一:临时方法
                优点:直接在代码中指定中文字体文件,在每个出现中文的地方指定  fontproperties为刚才设置的字体;
                缺点:每个出现中文的地方如title都要指定字体,并不是每个地方如legend都提供指定字体的参数。
                适用场合:偶尔使用中文显示,出现中文的地方较少。

myfont =matplotlib.font_manager.FontProperties(fname=r"c:\windows\fonts\simsun.ttc",size=14)

# 直接设置为中文时
ax.set_title(u'中文',  fontproperties = myfont)

# 设置x轴标签所对应的刻度的位置
ax.xaxis.set_major_locator(matplotlib.ticker.FixedLocator(np.arange(1,len(temp_name.name) + 1)))
# 某一列数据中均为中文,用该列作为x轴刻度标签时
ax.set_xticklabels(labels = temp_name.name, fontproperties =myfont, rotation = 'vertical')

  • 方法二:永久方法
              优点:一劳永逸,以后再不用担忧中文问题了,和英文一样如鱼得水。
              缺点:比较复杂,虽然我试验成功,可是原理我也没弄明白。
              适用场合:经常使用中文显示,各种地方都有出现中文的可能,如坐标轴刻度标签,图标等等。

  • 首先:网络上的方法:修改matplotlibrc配置文件()。
              进入Python安装目录下的Lib\site-packages\matplotlib\mpl-data目录,打开matplotlibrc文件,删除font.family和font.sans-serif两行前的#(后来试验,这步可省略),并在font.sans-serif后添加微软雅黑字体(MicrosoftYaHei) ,修改axes.unicode_minus为False,示例如下:
python画图包seaborn和matplotlib中文字体显示(针对windows系统,,使用ipython <wbr>notebook)
python画图包seaborn和matplotlib中文字体显示(针对windows系统,,使用ipython <wbr>notebook)
python画图包seaborn和matplotlib中文字体显示(针对windows系统,,使用ipython <wbr>notebook)
这个方法有时有效,有时失效,估计是ipython notebook开启后未必自动去读这个配置文件。

  • 其次:多次试验发现,当解决seaborn中的中文显示问题时,matplotlib的中文显示也解决了。
由于seaborn本身就建立在matplotlib的基础上,因此seaborn的一些设置可能也修改了matploblib的设置。

# 解决seaborn画图有中文时的编码异常错误
import sys
reload(sys)
#sys.setdefaultencoding('utf-8')
sys.setdefaultencoding('utf8')

# 修改matplotlibrc配置文件中的字体名称
mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei']#指定默认字体   
mpl.rcParams['axes.unicode_minus'] = False #解决保存图像是负号'-'显示为方块的问题 
sns.set_context("talk")
#sns.set_context("poster")
1. 目录 1. 目录 2 2. 绘图函数Plotting functions 4 2.1. 可视化的统计关系Visualizing statistical relationships 4 2.1.1. 用散点图联系变量Relating variables with scatter plots 4 2.1.2. 强调线条图的连续性Emphasizing continuity with line plots 10 2.1.3. 显示与切面的多个关系Showing multiple relationships with facets 21 2.2. 分类数据绘图Plotting with categorical data 24 2.2.1. 分类散点图Categorical scatterplots 26 2.2.2. 分类观测值分布Distributions of observations within categories 31 2.2.3. 分类统计估计Statistical estimation within categories 37 2.2.4. 对“wide-form”数据作图Plotting “wide-form” data 41 2.2.5. 显示与facet的多个关系Showing multiple relationships with facets 43 2.3. 可视化数据集的分布Visualizing the distribution of a dataset 44 2.3.1. 绘制单变量分布Plotting univariate distributions 45 2.3.2. 绘制二元分布Plotting bivariate distributions 51 2.3.3. 在数据集中可视化成对关系Visualizing pairwise relationships in a dataset 55 2.4. 可视化线性关系Visualizing linear relationships 57 2.4.1. 函数绘制线性模型Functions to draw linear regression models 58 2.4.2. 拟合不同种类的模型Fitting different kinds of models 61 2.4.3. 在其他变量上的情况Conditioning on other variables 68 2.4.4. 控制图表的大小形状Controlling the size and shape of the plot 71 2.4.5. 在其他上下文中绘制回归图Plotting a regression in other contexts 73 3. 多图网格Multi-plot grids 76 3.1. 构建结构化的多图网格Building structured multi-plot grids 76 3.2. 有条件的小倍数Conditional small multiples 77 3.3. 使用定制函数Using custom functions 86 3.4. 绘制成对的数据关系Plotting pairwise data relationships 90 4. 绘图美学Plot aesthetics 99 4.1. 控制图表美学Controlling figure aesthetics 99 4.1.1. Seaborn图表风格Seaborn figure styles 101 4.1.2. 删除轴上的小凸起Removing axes spines 104 4.1.3. 临时设置图表样式Temporarily setting figure style 105 4.1.4. 覆盖Seaborn样式的元素Overriding elements of the seaborn styles 106 4.1.5. 缩放图表元素Scaling plot elements 108 4.2. 选择调色板Choosing color palettes 111 4.2.1. 创建颜色调色板Building color palettes 111 4.2.2. 定性调色板Qualitative color palettes 112 4.2.3. 连续调色板Sequential color palettes 116 4.2.4. 不同颜色的调色板Diverging color palettes 122 4.2.5. 设置默认调色板Setting the default color palette 124 5. 教程中的数据集 125
Python中,可以使用不同的库来处理中文字体显示。一个常用的库是`matplotlib`,它可以用于绘图数据可视化。如果你想在绘图中使用中文字体,可以按照以下步骤进行设置: 1. 首先,你需要安装中文字体文件。可以从一些网站上下载中文字体文件,例如`ttf`格式的字体文件。 2. 将字体文件复制到你的Python环境中。你可以将字体文件复制到`/usr/share/fonts/truetype/`目录下(对于Linux系统),或者是将字体文件放在与你的Python脚本相同的目录下。 3. 使用`matplotlib`库来设置中文字体。下面是一个简单的示例代码: ```python import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties # 指定中文字体文件路径 font_path = '/usr/share/fonts/truetype/SimHei.ttf' # 创建字体对象 font = FontProperties(fname=font_path) # 绘制图形 plt.plot([1, 2, 3, 4], [1, 4, 9, 16]) # 设置中文标题 plt.title('中文标题', fontproperties=font) # 显示图形 plt.show() ``` 在上面的代码中,我们使用了`FontProperties`类来指定字体文件路径,并将其应用于标题的显示。这样就能够在图形中显示中文标题了。 除了`matplotlib`库外,还有其他一些库也可以用于在Python中处理中文字体,例如`Pillow`库用于图像处理,`Pygame`库用于游戏开发等。根据你的具体需求使用场景,选择适合的库来处理中文字体即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值