【python问题解决】seaborn中文显示问题,以及负号显示问题

plt.rcParams['font.sans-serif'] = ['SimHei']  # 中文字体设置-黑体
plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题
sns.set(font='SimHei',font_scale=1.5)  # 解决Seaborn中文显示问题并调整字体大小

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
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
### 回答1: 在Python中画图时不显示中文的原因是因为默认的字体库不支持中文字符。解决这个问题的方法有两种。 第一种方法是更换字体库。可以使用以下代码来更换字体库: ``` python import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置字体为黑体 plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题 # 之后就可以正常使用中文字符了 plt.plot([1, 2, 3, 4], [3, 4, 5, 6], label='中文测试') plt.legend() plt.show() ``` 在这个例子中,我们使用了SimHei字体库来显示中文字符。可以根据需要更换其他的中文字体库。 另一种方法是使用Unicode转义字符来显示中文。在字符串前加上`u`表示字符串为Unicode格式,然后使用字符的Unicode编码来表示中文字符。比如: ``` python import matplotlib.pyplot as plt # 使用Unicode转义字符显示中文字符 plt.plot([1, 2, 3, 4], [3, 4, 5, 6], label=u'\u4e2d\u6587\u6d4b\u8bd5') plt.legend() plt.show() ``` 这个例子中使用了`\u4e2d\u6587\u6d4b\u8bd5`来表示中文字符"中文测试"的Unicode编码。 无论使用哪种方法,都可以让Python画图显示中文字符。 ### 回答2: 在Python中画图不显示中文的原因是因为默认情况下,Python的绘图库matplotlib不支持使用中文显示。这是因为matplotlib库默认使用的是英文的字体库,导致无法渲染中文字符。 要解决这个问题,需按照以下步骤操作: 1. 安装中文字体库:需要下载适用于matplotlib的中文字体库。常用的字体库有思源黑体、宋体等。可以在网上搜索并下载相应字体库的压缩文件。 2. 解压字体库文件:将下载的压缩文件解压缩到任意目录,得到字体文件(通常以.ttf为后缀)。 3. 查找字体安装路径:查看操作系统中字体文件的安装路径,通常是在"C:\Windows\Fonts"文件夹中(对于Windows系统)。 4. 复制字体文件:将字体文件复制到matplotlib的字体文件夹中。该文件夹通常位于Python的安装目录下的“Lib\site-packages\matplotlib\mpl-data\fonts\ttf”文件夹中。如有需要,可以创建一个新的文件夹并将字体文件放在其中。 5. 修改matplotlib配置文件:打开matplotlib的配置文件,通常位于Python的安装目录下的“Lib\site-packages\matplotlib\mpl-data”文件夹中的matplotlibrc文件。找到其中的“font.family”一行,并将其值修改为字体文件的名称(去除后缀),例如“font.family: SimSun”。 6. 保存并关闭文件:将修改后的配置文件保存并关闭。 7. 重新运行绘图程序:重新运行绘图程序,此时就可以正常显示中文了。 需要注意的是,以上步骤的具体路径和文件名可能因操作系统和Python版本的不同而有所不同。因此,在实际操作中需要根据具体情况进行相应的调整。 以上就是解决Python绘图不显示中文的方法。通过安装中文字体库,并修改matplotlib的配置文件,可以顺利实现中文显示。 ### 回答3: Python画图不显示中文可以通过以下几种方法来解决。 第一种方法是使用Matplotlib库的rcParams方法来设置字体,代码如下: ``` import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置字体为黑体 plt.plot([1, 2, 3], [4, 5, 6]) plt.title('标题') plt.show() ``` 第二种方法是使用FontProperties方法,代码如下: ``` import matplotlib.pyplot as plt from matplotlib.font_manager import FontProperties font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf") # 设置字体为黑体 plt.plot([1, 2, 3], [4, 5, 6]) plt.title('标题', fontproperties=font) plt.show() ``` 第三种方法是使用Seaborn库,该库是在Matplotlib的基础上进行了更高级的封装,可以更方便地设置中文显示,代码如下: ``` import seaborn as sns sns.set_style("whitegrid", {"font.sans-serif": ['SimHei']}) # 设置字体为黑体 sns.lineplot([1, 2, 3], [4, 5, 6]) plt.title('标题') plt.show() ``` 需要注意的是,以上的方法都需要事先安装相应的字体文件,例如SimHei是黑体字体的一种。可以通过在系统中搜索相关字体文件进行安装,或者从互联网上下载相应的字体文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

斑点鱼 SpotFish

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值