Python3 using Matplotlib to draw Chinese garbled characters

Source: Internet
Author: User

Python3 uses matplotlib paint, because Python3 uses Unicode encoding by default, so writing code is no longer required to write Plt.xlabel (U ' number of people '), but to write Plt.xlabel directly (' number ').

Attention:
In the Chinese language to add Chinese-related fonts, or because there is no font display box, because the default use of the font is not in Chinese, use examples are as follows:

#-*-coding:utf-8-*-ImportPandas as PDImportNumPy as NP fromPandasImportSeries,dataframeImportMatplotlib.pyplot as Plt fromPylabImport*Data_train=pd.read_csv (".. /train.csv")Fig=plt.figure () fig.set (Alpha=0.6)#Plt.subplot2grid ((2,3) (0,0))Data_train. Survived.value_counts (). Plot (kind="Bar")#prevent Chinese garbled characterszhfont1 = Matplotlib.font_manager. Fontproperties (fname= ' C:\WINDOWS\FONTS\SIMSUN.TTC ' )Plt.title ("rescued situation (1 for rescue)",fontproperties= zhfont1) Plt.ylabel ("number",fontproperties= zhfont1) plt.show ()

The above shaded parts are essential and the effect is:


Python3 using Matplotlib to draw Chinese garbled characters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.