Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of setting matplotlib to display Chinese correctly by python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "python setting matplotlib correct display Chinese method", the editor shows you the operation process through the actual case, the operation method is simple, fast and practical. I hope this "python setting matplotlib correct display Chinese method" article can help you solve the problem.

I. Preface

Matplotlib is an excellent third-party library for data visualization in Python. Matplotlib is a set of Python toolkits based on numpy. This package provides a wealth of data drawing tools, mainly used to draw some statistical graphics.

Matplotlib library is composed of various visualization classes, and its internal structure is complex. Inspired by Matlab, matplotlib.pyplot is a command sublibrary for drawing all kinds of visual graphics, which is equivalent to a shortcut.

Import matplotlib.pyplot as plt

But matplotlib does not support Chinese display. If there is a Chinese display, the following problems will occur:

#-*-coding: UTF-8-*-"@ Author: Ye Tingyun @ official account: modify Python@CSDN: https://yetingyun.blog.csdn.net/ 70% discount line black and white gray style label label mark point shape" import matplotlib.pyplot as plt# generates x-axis data list deductive x_data = [i for i in range (0,55,5)] # construct y-axis data y_data1 = [0.5,0.62 0.72, 0.78, 0.85, 0.7, 0.64, 0.44, 0.29, 0.15, 0.09] y_data2 = [0.5,0.67, 0.71, 0.76, 0.79, 0.66, 0.58, 0.44, 0.38, 0.26, 0.18] y_data3 = [0.5,0.59,0.72, 0.74,0.76,0.68,0.58, 0.48,0.4,0.36] 0.3] # set graphic display style plt.style.use ('ggplot') # set figure size pixel plt.figure (figsize= (8,5), dpi=100) # draw the shape color label of three broken line points: for legend display plt.plot (x_data, y_data1, marker=' ^', color= "k", label= "device 1") plt.plot (x_data, y_data2, marker= "o", color= "k") Label= "device 2") plt.plot (x_data, y_data3, marker= "s", color= "k", label= "device 3") # x y axis label font size plt.xlabel ("time period / min", fontsize=13) plt.ylabel ("Direct trust value", fontsize=13) # display legend plt.legend () # Save picture display showplt.savefig ("line chart 01.png", dpi=200) plt.show ()

But matplotlib does not support Chinese display. If there is a Chinese display, the following problems will occur:

#-*-coding: UTF-8-*-"@ Author: Ye Tingyun @ official account: modify Python@CSDN: https://yetingyun.blog.csdn.net/ 70% discount line black and white gray style label label mark point shape" import matplotlib.pyplot as plt# generates x-axis data list deductive x_data = [i for i in range (0,55,5)] # construct y-axis data y_data1 = [0.5,0.62 0.72, 0.78, 0.85, 0.7, 0.64, 0.44, 0.29, 0.15, 0.09] y_data2 = [0.5,0.67, 0.71, 0.76, 0.79, 0.66, 0.58, 0.44, 0.38, 0.26, 0.18] y_data3 = [0.5,0.59,0.72, 0.74,0.76,0.68,0.58, 0.48,0.4,0.36] 0.3] # set graphic display style plt.style.use ('ggplot') # set figure size pixel plt.figure (figsize= (8,5), dpi=100) # draw the shape color label of three broken line points: for legend display plt.plot (x_data, y_data1, marker=' ^', color= "k", label= "device 1") plt.plot (x_data, y_data2, marker= "o", color= "k") Label= "device 2") plt.plot (x_data, y_data3, marker= "s", color= "k", label= "device 3") # x y axis label font size plt.xlabel ("time period / min", fontsize=13) plt.ylabel ("Direct trust value", fontsize=13) # display legend plt.legend () # Save picture display showplt.savefig ("line chart 01.png", dpi=200) plt.show ()

We need to manually set ~ ~ in order to solve the problem of Chinese display.

II. Solutions

1. Method 1: from matplotlib.font_manager import FontProperties # imports FontPropertiesfont = FontProperties (fname= "SimHei.ttf") Size=14) # set font # set the font #-*-coding: UTF-8-*-"@ Author: Ye Tingyun @ official account: practice Python@CSDN: https://yetingyun.blog.csdn.net/ 70% discount line black-and-white gray style label label marker shape"import matplotlib.pyplot as pltfrom matplotlib.font_manager import FontProperties # step 1 # generate x-axis" Data list derived x_data = [i for i in range (0) 55, 5)] # construct y-axis data y_data1 = [0.5,0.62,0.78,0.85,0.7,0.64,0.44,0.29,0.15,0.09] y_data2 = [0.5,0.67,0.71,0.76,0.79,0.58,0.38,0.26,0.18] y_data3 = [0.5,0.59,0.72,0.74] 0.76,0.68,0.58,0.48,0.4,0.36,0.3] # set graphic display style plt.style.use ('ggplot') font = FontProperties (fname= "SimHei.ttf", size=14) # step 2 # set figure size pixel plt.figure (figsize= (8,5), dpi=100) # draw the shape color label of three broken line points: for legend display plt.plot (x_data, y_data1) Marker=' ^', color= "k", label= "device 1") plt.plot (x_data, y_data2, marker= "o", color= "k", label= "device 2") plt.plot (x_data, y_data3, marker= "s", color= "k", label= "device 3") # x y axis label font size plt.xlabel ("time period / min", fontsize=13, fontproperties=font) plt.ylabel ("Direct Trust value", fontsize=13 Fontproperties=font) # display legend plt.legend (prop=font) # Save picture display showplt.savefig ("line chart 01.png", dpi=200) plt.show () 2. Mode two

Set through the fontdict dictionary parameter

Fontdict= {"family": "KaiTi", "size": 15, "color": "r"} #-*-coding: UTF-8-*-"" @ Author: Ye Tingyun @ official account: training Python@CSDN: https://yetingyun.blog.csdn.net/ 70% discount line black-and-white gray style label label marking point shape "" import matplotlib.pyplot as plt# generates x-axis data list derived x_data = [i for i in range (0) " 55, 5)] # construct y-axis data y_data1 = [0.5,0.62,0.78,0.85,0.7,0.64,0.44,0.29,0.15,0.09] y_data2 = [0.5,0.67,0.71,0.76,0.79,0.58,0.38,0.26,0.18] y_data3 = [0.5,0.59,0.72,0.74] 0.76,0.68,0.58,0.48,0.4,0.36,0.3] # set graphic display style plt.style.use ('ggplot') # set figure size pixel plt.figure (figsize= (8,5), dpi=100) # draw the shape and color label of three broken line points: for legend display plt.plot (x_data, y_data1, marker=' ^', color= "k") Label= "device 1") plt.plot (x_data, y_data2, marker= "o", color= "k", label= "device 2") plt.plot (x_data, y_data3, marker= "s", color= "k", label= "device 3") # x y axis label font size plt.xlabel ("time period / min", fontsize=13, fontdict= {"family": "KaiTi", "size": 15 "color": "r"}) plt.ylabel ("direct trust value", fontsize=13, fontdict= {"family": "KaiTi", "size": 15, "color": "k"}) # display legend plt.legend (prop= {'family':' SimHei', 'size': 16}) # Save picture display showplt.savefig ("line chart 01.png", dpi=200) plt.show () 3. Mode 3

Change the global font

# matplotlib actually does not support displaying Chinese. Display Chinese requires one line of code to set font mpl.rcParams ['font.family'] =' SimHei'plt.rcParams ['axes.unicode_minus'] = False # step 2 (solve the problem of negative sign display of negative numbers on axes) #-*-coding: UTF-8-*-"@ Author: Ye Tingyun @ official account: training Python@CSDN: https://yetingyun .blog.csdn.net/ Triple Line Black, White and Grey style label label Mark Point shape "" import matplotlib.pyplot as pltimport matplotlib as mpl# generates x-axis data list derived x_data = [i for i in range (0) " 55, 5)] # construct y-axis data y_data1 = [0.5,0.62,0.78,0.85,0.7,0.64,0.44,0.29,0.15,0.09] y_data2 = [0.5,0.67,0.71,0.76,0.79,0.58,0.38,0.26,0.18] y_data3 = [0.5,0.59,0.72,0.74] 0.76, 0.68, 0.58, 0.48, 0.4, 0.36 0.3] # matplotlib actually does not support the display of Chinese characters that require one line of code to set the font mpl.rcParams ['font.family'] =' SimHei'plt.rcParams ['axes.unicode_minus'] = False # step 2 (solve the problem of negative sign display of negative numbers on axes) # set graphic display style plt.style.use (' ggplot') # set figure size pixel plt.figure (figsize= (8,5)) Dpi=100) # draw the shape color label of three broken line points: used to display plt.plot (x_data, y_data1, marker=' ^', color= "k", label= "device 1") plt.plot (x_data, y_data2, marker= "o", color= "k", label= "device 2") plt.plot (x_data, y_data3, marker= "s", color= "k") Label= "device 3") # x y axis label font size plt.xlabel ("time period / min", fontsize=13) plt.ylabel ("direct trust value", fontsize=13) # display legend plt.legend () # Save picture display showplt.savefig ("line chart 01.png", dpi=200) plt.show () 4. Mode 4

It is also a way to change fonts globally.

Font = {'family':' SimHei', 'weight':' bold', 'size':' 16'} plt.rc ('font', * * font) # step 1 (set more properties of the font) plt.rc (' axes' Unicode_minus=False) # step 2 (solve the problem of negative sign display of negative numbers on axes) #-*-coding: UTF-8-*-"@ Author: Ye Tingyun @ official account: training Python@CSDN: https://yetingyun.blog.csdn.net/ 70% discount line black-white-gray style label label marking point shape"import matplotlib.pyplot as plt# generates x-axis data list guided x_data = [i for i in range (0,55)" 5)] # construct y-axis data y_data1 = [0.5,0.62,0.72,0.78,0.85,0.7,0.64,0.44,0.29,0.15,0.09] y_data2 = [0.5,0.67,0.71,0.76,0.76,0.58,0.44,0.38,0.26,0.18] y_data3 = [0.5,0.59,0.72,0.74] 0.76,0.68,0.58,0.48,0.4,0.36,0.3] font = {'family':' SimHei', 'weight':' bold', 'size':' 16'} plt.rc ('font', * * font) # step 1 (set more properties of the font) plt.rc (' axes' Unicode_minus=False) # step 2 (solve the problem of negative sign display of negative axes) # set graphic display style plt.style.use ('ggplot') # set figure size pixel plt.figure (figsize= (8,5), dpi=100) # draw the shape and color label of three broken line points: for legend display plt.plot (x_data, y_data1, marker=' ^', color= "k", label= "device 1") plt.plot (x_data Y_data2, marker= "o", color= "k", label= "device 2") plt.plot (x_data, y_data3, marker= "s", color= "k", label= "device 3") # x y axis label font size plt.xlabel ("time period / min", fontsize=13) plt.ylabel ("direct trust value", fontsize=13) # display legend plt.legend () # Save picture display showplt.savefig ("line chart 01.png" Dpi=200) plt.show () this is the end of the introduction of "how to set matplotlib to display Chinese correctly by python". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report