search
HomeBackend DevelopmentPython TutorialSolution to matplotlib Chinese garbled characters in Python

Matplotlib is a good plotting package for Python, but it does not support Chinese itself (it seems that there is no Chinese font in its default configuration), so if it appears in the drawing Chinese, garbled characters will appear

Matplotlib is a good drawing package for Python, but it does not support Chinese (it seems that there is no Chinese font in its default configuration), so if Chinese appears in the drawing, it will Garbled characters will appear.

When matplotlib draws images with Chinese annotations, there will be garbled characters.

Example code:


import matplotlib
import matplotlib.pyplot as plt

#定义文本框和箭头格式
decisionNode =dict(boxstyle="sawtooth",fc="0.8")
leafNode=dict(boxstyle="round4",fc="0.8")
arrow_args=dict(arrowstyle="<-")

#绘制带箭头的注解
def plotNode(nodeTxt,centerPt,parentPt,nodeType):
  createPlot.axl.annotate(nodeTxt,xy=parentPt,xycoords=&#39;axes fraction&#39;,xytext=centerPt,textcoords=&#39;axes fraction&#39;,va="center",ha="center",bbox=nodeType,arrowprops=arrow_args)

def createPlot():
  fig =plt.figure(1,facecolor=&#39;white&#39;)
  fig.clf()
  createPlot.axl=plt.subplot(111,frameon=False)
  plotNode(U&#39;决策点&#39;,(0.5,0.1),(0.1,0.5),decisionNode)
  plotNode(U&#39;叶节点&#39;,(0.8,0.1),(0.3,0.8),leafNode)
  plt.show()

Solution: Introduce font

import matplotlib.pyplot as plt
import matplotlib

#定义自定义字体,文件名是系统中文字体
myfont = matplotlib.font_manager.FontProperties(fname=&#39;C:/Windows/Fonts/simkai.ttf&#39;) 
#解决负号&#39;-&#39;显示为方块的问题 
matplotlib.rcParams[&#39;axes.unicode_minus&#39;]=False 

decisionNode =dict(boxstyle="sawtooth",fc="0.8")
leafNode=dict(boxstyle="round4",fc="0.8")
arrow_args=dict(arrowstyle="<-")

def plotNode(nodeTxt,centerPt,parentPt,nodeType):
  createPlot.axl.annotate(nodeTxt,xy=parentPt,xycoords=&#39;axes fraction&#39;,xytext=centerPt,textcoords=&#39;axes fraction&#39;,va="center",ha="center",bbox=nodeType,arrowprops=arrow_args,fontproperties=myfont)

def createPlot():
  fig =plt.figure(1,facecolor=&#39;white&#39;)
  fig.clf()
  createPlot.axl=plt.subplot(111,frameon=False)
  plotNode(U&#39;决策点&#39;,(0.5,0.1),(0.1,0.5),decisionNode)
  plotNode(U&#39;叶节点&#39;,(0.8,0.1),(0.3,0.8),leafNode)
  plt.show()

## into the code #

The above is the detailed content of Solution to matplotlib Chinese garbled characters in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何使用Python和Matplotlib创建三维折线图如何使用Python和Matplotlib创建三维折线图Apr 22, 2023 pm 01:19 PM

1.0简介三维图像技术是现在国际最先进的计算机展示技术之一,任何普通电脑只需要安装一个插件,就可以在网络浏览器中呈现三维的产品,不但逼真,而且可以动态展示产品的组合过程,特别适合远程浏览。立体图视觉上层次分明色彩鲜艳,具有很强的视觉冲击力,让观看的人驻景时间长,留下深刻的印象。立体图给人以真实、栩栩如生,人物呼之欲出,有身临其境的感觉,有很高的艺术欣赏价值。2.0三维图画法与类型首先要安装Matplotlib库可以使用pip:pipinstallmatplotlib假设已经安装了matplotl

pycharm如何安装Matplotlibpycharm如何安装MatplotlibDec 18, 2023 pm 04:32 PM

安装步骤:1、打开PyCharm集成开发环境;2、转到“File”菜单,然后选择“Settings”;3、在“Settings”对话框中,选择“Project: <your_project_name>”下的“Python Interpreter”;4、单击右上角的加号按钮“+”,在弹出的对话框中搜索“matplotlib”;5、选择“matplotlib”安装即可。

深入研究matplotlib的色彩映射表深入研究matplotlib的色彩映射表Jan 09, 2024 pm 03:51 PM

深入学习matplotlib颜色表,需要具体代码示例一、引言matplotlib是一个功能强大的Python绘图库,它提供了丰富的绘图函数和工具,可以用于创建各种类型的图表。而颜色表(colormap)是matplotlib中一个重要的概念,它决定了图表的配色方案。深入学习matplotlib颜色表,将帮助我们更好地掌握matplotlib的绘图功能,使绘

Python中Matplotlib图像怎么添加标签Python中Matplotlib图像怎么添加标签May 12, 2023 pm 12:52 PM

一、添加文本标签plt.text()用于在绘图过程中,在图像上指定坐标的位置添加文本。需要用到的是plt.text()方法。其主要的参数有三个:plt.text(x,y,s)其中x、y表示传入点的x和y轴坐标。s表示字符串。需要注意的是,这里的坐标,如果设定有xticks、yticks标签,则指的不是标签,而是绘图时x、轴的原始值。因为参数过多,不再一一解释,根据代码学习其用法。ha=&#39;center&rsquo;表示垂直对齐方式居中,fontsize=30表示字体大小为3

如何安装matplotlib如何安装matplotlibDec 20, 2023 pm 05:54 PM

安装教程:1、打开命令行窗口,确保已经安装了Python和pip;2、​输入“pip install matplotlib”命令安装matplotlib;3、等待安装完成后,通过import matplotlib.pyplot as plt代码验证matplotlib是否成功安装,若没有报错,说明matplotlib已经成功安装。

matplotlib显示中文的方法有哪些matplotlib显示中文的方法有哪些Nov 22, 2023 pm 05:34 PM

显示中文的方法有安装中文字体、配置字体路径、使用中文字符等。详细介绍:1、安装中文字体:首先,您需要安装支持中文字符的字体文件。常用的中文字体有SimHei、SimSun、Microsoft YaHei等;2、配置字体路径:在代码中,需要指定字体文件的路径;3、使用中文字符:在代码中,直接使用中文字符即可。

怎么在python安装matplotlib怎么在python安装matplotlibDec 04, 2023 pm 02:20 PM

python安装matplotlib的步骤:1、确保你已经安装了Python,可以用“python --version”命令来检查Python是否已安装;2、打开终端或命令提示符,输入“pip install matplotlib”安装Matplotlib;3、等待安装完成;4、使用“import matplotlib.pyplot as plt”代码导入Matplotlib。

解密matplotlib颜色表:揭秘色彩背后的故事解密matplotlib颜色表:揭秘色彩背后的故事Jan 09, 2024 am 11:38 AM

matplotlib颜色表详解:揭秘色彩背后的秘密引言:作为Python中最常用的数据可视化工具之一,matplotlib拥有强大的绘图功能和丰富的颜色表。本文将介绍matplotlib中的颜色表,探寻色彩背后的秘密。我们将深入研究matplotlib中常用的颜色表,并给出具体代码示例。一、Matplotlib中的颜色表颜色的表示方式在matplotlib中

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment