麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Python > 正文

Matplotlib中文亂碼的3種解決方案

2020-02-15 23:41:21
字體:
來源:轉載
供稿:網友

前言

Matplotlib是一個Python 2D繪圖庫,它可以在各種平臺上以各種硬拷貝格式和交互式環境生成出具有出版品質的圖形。 Matplotlib可用于Python腳本,Python和IPython shell,Jupyter筆記本,Web應用程序服務器和四個圖形用戶界面工具包。

然而最近在使用matplotlib默認情況會出現亂碼問題,原則上matplotlib是支持中文的,只是在配置信息里沒有中文字體的相關信息。

解決方法如下:

解決方案一:修改配置文件

matplotlib 從配置文件 matplotlibrc 中讀取配置,字體相關內容也在其中。查詢當前matplotlibrc 所在目錄,可以用 get_configdir()函數:

import matplotlibmatplotlib.get_configdir()

通常存放位置:lib/site-packages/matplotlib/mpl-data/matplotlibrc

涉及到字體部分的設置內容為:

#font.family : sans-serif#font.style : normal#font.variant : normal#font.weight : normal#font.stretch : normal## note that font.size controls default text sizes. To configure## special text sizes tick labels, axes, labels, title, etc, see the rc## settings for axes and ticks. Special text sizes can be defined## relative to font.size, using the following values: xx-small, x-small,## small, medium, large, x-large, xx-large, larger, or smaller#font.size : 10.0#font.serif : DejaVu Serif, Bitstream Vera Serif, Computer Modern Roman, New Century Schoolbook, Century Schoolbook L, Utopia, ITC Bookman, Bookman, Nimbus Roman No9 L, Times New Roman, Times, Palatino, Charter, serif#font.sans-serif : DejaVu Sans, Bitstream Vera Sans, Computer Modern Sans Serif, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif#font.cursive : Apple Chancery, Textile, Zapf Chancery, Sand, Script MT, Felipa, cursive#font.fantasy : Comic Sans MS, Chicago, Charcoal, ImpactWestern, Humor Sans, xkcd, fantasy#font.monospace : DejaVu Sans Mono, Bitstream Vera Sans Mono, Computer Modern Typewriter, Andale Mono, Nimbus Mono L, Courier New, Courier, Fixed, Terminal, monospace

matplotlib 默認使用的 font.family 是 sans-serif,即無襯線字體,可以看到在font.sans-serif中設置的全部為西文字體,這里的設置和css樣式文件中設置差不多,只需要添加系統存在的字體名稱即可(需要注意的是,matplotlib:

只支持ttf格式的字體),設置時需要將注釋符號#去除。

解決方案二:重載配置文件

import matplotlib as mplmpl.rcParams['font.sans-serif'] = ['SimHei']mpl.rcParams['font.serif'] = ['SimHei']mpl.rcParams['axes.unicode_minus'] = False # 解決保存圖像是負號'-'顯示為方塊的問題,或者轉換負號為字符串

解決方案三:自定義字體

import numpy as npimport pylab as plimport matplotlib.font_manager as fmmyfont = fm.FontProperties(fname=r'D:/Fonts/simkai.ttf') # 設置字體t = np.arange(0.0,2.0 * np.pi,0.01) # 自變量取值范圍s = np.sin(t) # 計算正弦函數值z = np.cos(t) # 計算余弦函數值pl.plot(t,s,label='正弦')pl.plot(t,z,label='余弦')pl.xlabel('x-變量',fontproperties=myfont,fontsize=24) #設置標簽pl.ylabel('y-正弦余弦函數值',fontproperties=myfont,fontsize=24)pl.title('sin-cos函數圖像',fontproperties=myfont,fontsize=32) #圖像標題pl.legend(prop=myfont)pl.show()            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲精品午夜国产va久久成人 | 久久久久久久亚洲视频 | 国产欧美一区二区三区免费看 | 日韩精品久久久久久 | 中文黄色一级片 | 日韩视频一| 国产精品免费麻豆入口 | 中文字幕一区久久 | 日本精品久久久一区二区三区 | 99最新网址 | 亚洲人成综合第一网 | 一级黄片毛片免费看 | free性欧美hd另类 | 免费视频99 | 一级免费在线视频 | 免费a级网站 | 黄色免费小网站 | 99pron| 日本a大片 | 女教师~淫辱の动漫在线 | 女教师~淫辱の动漫在线 | 免费a级毛片大学生免费观看 | 一级黄色影片在线观看 | h视频免费在线观看 | 国产精品视频二区不卡 | 国产精品一区二区三区在线 | 成人免费福利网站 | 欧美日韩a∨毛片一区 | 黄片毛片一级 | 午夜精品久久久久久中宇 | 日韩精品久久久久久久电影99爱 | 中文欧美日韩 | 欧美日本一 | 黄色羞羞视频在线观看 | 99欧美视频| 久久精品视频黄色 | 超碰97国产在线 | 久久久久国产一区二区三区不卡 | 日韩高清影视 | 久久国产精品久久精品国产演员表 | av黄色片网站 |