Matplotlib–Python三维绘图–解决中文显示乱码问题

发布于 2022-10-06  814 次阅读


Please refresh the page if equations are not rendered correctly.
---------------------------------------------------------------

方式一

import matplotlib as mpl
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimSun'] 
#该语句解决图像中的中文乱码,字体根据电脑配置决定
plt.rcParams["axes.unicode_minus"]=False 
#该语句解决图像中的“-”负号的乱码问题
import numpy as np
###################################
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = plt.axes(projection='3d')
import matplotlib as mpl

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimSun'] #该语句解决图像中的中文乱码,字体根据电脑配置决定
plt.rcParams["axes.unicode_minus"]=False #该语句解决图像中的“-”负号的乱码问题

mpl.rcParams['legend.fontsize'] = 10
#############绘图###################
x=[1,2,3,4,5]
y=[2,1,4,3,5]
z=[6,5,4,3,1]
ax.scatter3D(x,y,z, c='yellow',alpha=0.5,marker='o',label='parametric1 curve')  #绘制散点图,alpha为marker透明度
ax.legend()
ax.set_title('地点') # plt.title('TiTle')
ax.set_xlabel('X');ax.set_ylabel('Y');ax.set_zlabel('Z')
ax.set_xlim(0,6);ax.set_ylim(0,6);ax.set_zlim(0,7)
plt.show()
###########标注点名称##########
name=[[3,9],'A',(2,5),15,'武汉']
for i in range(len(x)):
    ax.text(x[i], y[i], z[i],name[i])

方式二

import numpy as np
###################################
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = plt.axes(projection='3d')
import matplotlib as mpl
mpl.rcParams['legend.fontsize'] = 10
#############绘图###################
x=[1,2,3,4,5]
y=[2,1,4,3,5]
z=[6,5,4,3,1]
ax.scatter3D(x,y,z, c='yellow',alpha=0.5,marker='o',label='parametric1 curve')  #绘制散点图,alpha为marker透明度
ax.legend()
ax.set_title('标题(TiTle)',fontproperties="SimSun") # plt.title('TiTle')
ax.set_xlabel('X');ax.set_ylabel('Y');ax.set_zlabel('Z')
ax.set_xlim(0,6);ax.set_ylim(0,6);ax.set_zlim(0,7)
plt.show()
###########标注点名称##########
name=[[3,9],'A',(2,5),15,'武汉']
for i in range(len(x)):
    ax.text(x[i], y[i], z[i],name[i])

*正确选择自己电脑上安装的字体,部分中文字体的英文名
宋体 SimSun
黑体 SimHei
微软雅黑 Microsoft YaHei
微软正黑体 Microsoft JhengHei
新宋体 NSimSun
新细明体 PMingLiU
细明体 MingLiU
标楷体 DFKai-SB
仿宋 FangSong
楷体 KaiTi
隶书:LiSu
幼圆:YouYuan
华文细黑:STXihei
华文楷体:STKaiti
华文宋体:STSong
华文中宋:STZhongsong
华文仿宋:STFangsong
方正舒体:FZShuTi
方正姚体:FZYaoti
华文彩云:STCaiyun
华文琥珀:STHupo
华文隶书:STLiti
华文行楷:STXingkai
华文新魏:STXinwei

Everything not saved will be lost.
最后更新于 2022-10-06