python畫圖中文不顯示問題的解決方法
前言
python畫圖,如果用英文顯示基本沒有問題,但是中文可能會有亂碼或者不顯示的情況。
經(jīng)過個人的測試,下圖中“橫軸”,“縱軸”字樣的中文顯示沒有什么大問題,主要是plt.title部分和plt.plot部分的顯示

一、plt.title中文顯示的解決:
from matplotlib.font_manager import FontProperties font_set = FontProperties(fname=r"c:\windows\fonts\SIMLI.TTF", size=15) plt.title(u'小明的標(biāo)題', FontProperties=font_set)
FontProperties中加載的就是本地的字體
路徑:C:\Windows\Fonts

選中字體鼠標(biāo)右鍵屬性即可復(fù)制名字進行替換

font_set = FontProperties(fname=r"c:\windows\fonts\SIMLI.TTF", size=15)

從改變字體的結(jié)果來看,橫縱坐標(biāo)和標(biāo)題區(qū)域受影響,plt.plot不受影響
二、plt.plot中文顯示的解決:
與上述類似,加下列一行即可
plt.legend(prop={'family':'SimHei','size':15})
三、供參考代碼
from matplotlib import pyplot
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=15)
import matplotlib
import numpy as np
%matplotlib inline
plt.rcParams['figure.figsize'] = (12.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
# plt.rcParams['font.sans-serif']=['SimHei']
# plt.rcParams['axes.unicode_minus']=False
# 設(shè)置標(biāo)題大小
plt.rcParams['font.size'] = '16'
fig = plt.figure()
matplotlib.rcParams.update(
{
'text.usetex': False,
'font.family': 'stixgeneral',
'mathtext.fontset': 'stix',
}
)
# myfont = FontProperties(fname='/homenuxidc/.local/fonts/文泉驛正黑.ttf')
x = range(0,102,2)
x = [str(x) for x in list(x)]
x = range(len(x))
#這里如果取得值,最好省略,否則冗余代碼太多
dj1=[0.140885022563663,0.285128051316358,0.331236115292078,0.372582750937596,0.463174450139405,0.586773503380428,0.542598182149781,0.542598182149781,0.564956686389027,0.542598182149781,0.519630577568173,0.428649426296771,0.676284101462148,0.635875231620977,0.564956686389027,0.542598182149781,0.564956686389027,0.62899995822798,0.676284101462148,0.721874028834649,0.676284101462148,0.702515362688485,0.682890636232397,0.557567115066442,0.564956686389027,0.635875231620977,0.656252254277025,0.676284101462148,0.721874028834649,0.62899995822798,0.62899995822798,0.702515362688485,0.759939881400332,0.702515362688485,0.696005029828444,0.656252254277025,0.696005029828444,0.676284101462148,0.608105164606941,0.741002175030875,0.721874028834649,0.702515362688485,0.721874028834649,0.62899995822798,0.608105164606941,0.682890636232397,0.676284101462148,0.702515362688485,0.696005029828444,0.696005029828444,0.696005029828444]
plt.plot(x, dj1,marker='s',label=u'小明')
plt.grid(linestyle='--')
plt.grid(axis="x")
plt.title(u'SimHei', fontproperties=font_set)
plt.title('小明的標(biāo)題')
plt.legend() # 讓圖例生效
y_major_locator=MultipleLocator(0.1)
ax=plt.gca()
ax.yaxis.set_major_locator(y_major_locator)
plt.ylim(0,1)
plt.xlim(0,55)
plt.margins(0)
plt.subplots_adjust(bottom=0.001)
plt.xlabel("橫軸",fontproperties=font_set) #X軸標(biāo)簽
plt.ylabel("縱軸",fontproperties=font_set) #Y軸標(biāo)簽
plt.legend(prop={'family':'SimHei','size':15})
總結(jié)
到此這篇關(guān)于python畫圖中文不顯示問題的解決方法的文章就介紹到這了,更多相關(guān)python畫圖中文不顯示內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pyinstaller打包django項目的實現(xiàn)步驟
本文主要介紹了pyinstaller打包django項目的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
numpy系列之?dāng)?shù)組重塑的實現(xiàn)
本文主要介紹了numpy數(shù)組重塑。所謂數(shù)組重塑就是更改數(shù)組的形狀,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
Java Spring項目國際化(i18n)詳細(xì)方法與實例
這篇文章主要介紹了Java Spring項目國際化詳細(xì)方法與實例,需要的朋友可以參考下2020-03-03
Python練習(xí)之操作MySQL數(shù)據(jù)庫
這篇文章主要介紹了Python練習(xí)之操作MySQL數(shù)據(jù)庫,文章通過如何創(chuàng)建MySQL數(shù)據(jù)表?如何向MySQL表中插入數(shù)據(jù)?如何查詢MySQL中的數(shù)據(jù)?的三個問題展開了詳細(xì)的內(nèi)容介紹2022-06-06

