matplotlib绘图时设置坐标轴标签上下标,并缩小角标字号

平时用matplotlib绘图总会遇到坐标轴单位存在角标的问题,在网上找了一圈设置上下角标的方法还是很多人提的,此处就不细说了,但是角标字号怎么缩小没找到有人说,笔者是个小菜鸡也是备受折磨,找了一个凑合能用的法子分享出来。

首先,我们先来画个普普通通的折线图:

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHSun']  
plt.rcParams['axes.unicode_minus'] = False

plt.plot([1,2])
plt.ylabel(r'土壤含水量 (m$^3$/m$^3$)', fontsize=12)

plt.show()

在这里插入图片描述

方法一:用LaTeX语法

可以看到,这里我用的是“ 3 ^3 3”(LaTeX语法)设置上标,角标字体实际上和标题其他文字一样都是12,现在对代码做一些调整:

import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHSun']  
plt.rcParams['axes.unicode_minus'] = False

plt.plot([1,2])
plt.ylabel('土壤含水量 (m$^{{\scr 3}}$/m$^{{\scr 3}})$', fontsize=12) 

plt.show()

在这里插入图片描述
效果很凑合但也不是不能用。

方法二:用Alt编码

先输入"m",然后按住Alt键,再输入(Alt码),然后释放Alt键,就会出现上标字符。²³这俩就是这么打出来的。
常用的Alt编码组合主要有:

数学符号:
加减号 Alt+0177: ±
平方 Alt+0178: 2
立方 Alt+0179: 3
乘号 Alt+0215: ×
除号 Alt+0247: ÷
不等于 Alt+0186: ≠
大于等于 Alt+0236: ≥
小于等于 Alt+0235: ≤
希腊字母:
α Alt+0224: α
β Alt+0225: β
π Alt+0227: π
特殊符号:
° Alt+0176: °
¥ Alt+0165: ¥
© Alt+0169: ©
字母符号:
æ Alt+0230: æ
ø Alt+0248: ø
å Alt+0197: å

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Matplotlib中,可以使用LaTeX语法来设置坐标轴标签格式为上标,并且可以通过修改字体属性来改变标签及其上标的样式。具体实现步骤如下: 1. 导入Matplotlib库和字体库 ```python import matplotlib.pyplot as plt from matplotlib import font_manager ``` 2. 设置LaTeX格式 ```python plt.rcParams['text.usetex'] = True ``` 3. 设置字体 ```python font = font_manager.FontProperties(family='Times New Roman', weight='bold', size=14) ``` 4. 绘制形,并设置坐标轴标签 ```python plt.plot(x, y) plt.xlabel(r'$\mathrm{Time\ (s)}$', fontproperties=font) plt.ylabel(r'$\mathrm{Speed\ (m/s^{\mathbf{2}})}$', fontproperties=font) ``` 在这里,使用了LaTeX语法将坐标轴标签格式设置为上标,并使用了字体属性`fontproperties`来设置标签及其上标的字体样式。其中,`family`参数用于设置字体名称,`weight`参数用于设置字体粗细,`size`参数用于设置字体大小。 完整代码示例: ```python import numpy as np import matplotlib.pyplot as plt from matplotlib import font_manager # 设置LaTeX格式 plt.rcParams['text.usetex'] = True # 设置字体 font = font_manager.FontProperties(family='Times New Roman', weight='bold', size=14) # 生成测试数据 t = np.linspace(0, 10, 100) y = np.sin(t) # 绘制plt.plot(t, y) # 设置坐标轴标签 plt.xlabel(r'$\mathrm{Time\ (s)}$', fontproperties=font) plt.ylabel(r'$\mathrm{Speed\ (m/s^{\mathbf{2}})}$', fontproperties=font) # 显示plt.show() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值