網站首頁 編程語言 正文
前言
python畫圖,如果用英文顯示基本沒有問題,但是中文可能會有亂碼或者不顯示的情況。
經過個人的測試,下圖中“橫軸”,“縱軸”字樣的中文顯示沒有什么大問題,主要是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'小明的標題', FontProperties=font_set)
FontProperties中加載的就是本地的字體
路徑:C:\Windows\Fonts
選中字體鼠標右鍵屬性即可復制名字進行替換
font_set = FontProperties(fname=r"c:\windows\fonts\SIMLI.TTF", size=15)
從改變字體的結果來看,橫縱坐標和標題區域受影響,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
# 設置標題大小
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('小明的標題')
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軸標簽
plt.ylabel("縱軸",fontproperties=font_set) #Y軸標簽
plt.legend(prop={'family':'SimHei','size':15})
總結
原文鏈接:https://blog.csdn.net/QAQIknow/article/details/116974056
相關推薦
- 2022-09-22 使用einops簡化數據維度操作
- 2022-06-20 深入淺析C#?11?對?ref?和?struct?的改進_C#教程
- 2022-08-15 python?datetime模塊詳解_python
- 2022-09-19 C語言數據結構之單鏈表存儲詳解_C 語言
- 2022-04-06 React中使用react-player?播放視頻或直播的方法_React
- 2022-08-02 Go語言resty?http包調用jenkins?api實例_Golang
- 2022-01-17 url傳參 中文出現亂碼問題 解決方案
- 2023-04-14 拯救強迫癥Android?Builder模式_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支