小白教程
所有教程
关于
Search
172.70.178.81
172.70.178.81
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Matplotlib 轴标签和标题”的源代码
本页内容
上一节:
Matplotlib_绘图线
下一节:
Matplotlib_网格线
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:Matplotlib 轴标签和标题}}[[Category:Matplotlib 教程|6]] = Matplotlib 轴标签和标题 = 我们可以使用 '''xlabel()''' 和 '''ylabel()''' 方法来设置 x 轴和 y 轴的标签。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np import matplotlib.pyplot as plt x = np.array([1, 2, 3, 4]) y = np.array([1, 4, 9, 16]) plt.plot(x, y) plt.xlabel("x - label") plt.ylabel("y - label") plt.show() </sample> 显示结果如下: === 标题 === 我们可以使用 '''title()''' 方法来设置标题。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np import matplotlib.pyplot as plt x = np.array([1, 2, 3, 4]) y = np.array([1, 4, 9, 16]) plt.plot(x, y) plt.title("XiaoBai.Wang TEST TITLE") plt.xlabel("x - label") plt.ylabel("y - label") plt.show() </sample> 显示结果如下: === 图形中文显示 === Matplotlib 默认情况不支持中文,我们可以使用以下简单的方法来解决。 这里我们使用思源黑体,思源黑体是 Adobe 与 Google 推出的一款开源字体。 官网:https://source.typekit.com/source-han-serif/cn/ GitHub 地址:https://github.com/adobe-fonts/source-han-sans/tree/release/OTF/SimplifiedChinese 打开链接后,在里面选一个就好了: 你也可以在网盘下载: https://pan.baidu.com/s/10-w1JbXZSnx3Tm6uGpPGOw,提取码:yxqu。 可以下载个 OTF 字体,比如 SourceHanSansSC-Bold.otf,将该文件文件放在当前执行的代码文件中: SourceHanSansSC-Bold.otf 文件放在当前执行的代码文件中: <sample title="" desc="" lang="python" hererun="1"> import numpy as np from matplotlib import pyplot as plt import matplotlib # fname 为 你下载的字体库路径,注意 SourceHanSansSC-Bold.otf 字体的路径 zhfont1 = matplotlib.font_manager.FontProperties(fname="SourceHanSansSC-Bold.otf") x = np.arange(1,11) y = 2 * x + 5 plt.title("小白教程 - 测试", fontproperties=zhfont1) # fontproperties 设置中文显示,fontsize 设置字体大小 plt.xlabel("x 轴", fontproperties=zhfont1) plt.ylabel("y 轴", fontproperties=zhfont1) plt.plot(x,y) plt.show() </sample> 执行输出结果如下图: <blockquote>此外,我们还可以使用系统的字体: <sample title="" desc="" lang="python" hererun="1"> from matplotlib import pyplot as plt import matplotlib a=sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist]) for i in a: print(i) </sample> 打印出你的 font_manager 的 ttflist 中所有注册的名字,找一个看中文字体例如:STFangsong(仿宋),然后添加以下代码即可: <sample title="" desc="" lang="python" hererun="1"> plt.rcParams['font.family']=['STFangsong'] </sample></blockquote> 此外我们还可以自定义字体的样式: <sample title="" desc="" lang="python" hererun="1"> import numpy as np from matplotlib import pyplot as plt import matplotlib # fname 为 你下载的字体库路径,注意 SourceHanSansSC-Bold.otf 字体的路径,size 参数设置字体大小 zhfont1 = matplotlib.font_manager.FontProperties(fname="SourceHanSansSC-Bold.otf", size=18) font1 = {'color':'blue','size':20} font2 = {'color':'darkred','size':15} x = np.arange(1,11) y = 2 * x + 5 # fontdict 可以使用 css 来设置字体样式 plt.title("小白教程 - 测试", fontproperties=zhfont1, fontdict = font1) # fontproperties 设置中文显示,fontsize 设置字体大小 plt.xlabel("x 轴", fontproperties=zhfont1) plt.ylabel("y 轴", fontproperties=zhfont1) plt.plot(x,y) plt.show() </sample> 输出结果如下: === 标题与标签的定位 === '''title()''' 方法提供了 loc 参数来设置标题显示的位置,可以设置为: ''''left', 'right', 和 'center', 默认值为 'center''''。 '''xlabel()''' 方法提供了 loc 参数来设置 x 轴显示的位置,可以设置为: ''''left', 'right', 和 'center', 默认值为 'center''''。 '''ylabel()''' 方法提供了 loc 参数来设置 y 轴显示的位置,可以设置为: ''''bottom', 'top', 和 'center', 默认值为 'center''''。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np from matplotlib import pyplot as plt import matplotlib # fname 为 你下载的字体库路径,注意 SourceHanSansSC-Bold.otf 字体的路径,size 参数设置字体大小 zhfont1 = matplotlib.font_manager.FontProperties(fname="SourceHanSansSC-Bold.otf", size=18) font1 = {'color':'blue','size':20} font2 = {'color':'darkred','size':15} x = np.arange(1,11) y = 2 * x + 5 # fontdict 可以使用 css 来设置字体样式 plt.title("小白教程 - 测试", fontproperties=zhfont1, fontdict = font1, loc="left") # fontproperties 设置中文显示,fontsize 设置字体大小 plt.xlabel("x 轴", fontproperties=zhfont1, loc="left") plt.ylabel("y 轴", fontproperties=zhfont1, loc="top") plt.plot(x,y) plt.show() </sample> 输出结果如下:
返回至“
Matplotlib 轴标签和标题
”。
上一节:
Matplotlib_绘图线
下一节:
Matplotlib_网格线