如何使用 matplotlib 将轴格式化为 dB(分贝)
在我们之前的文章 Matplotlib 自定义 SI 前缀单位刻度格式化器 中,我们展示了如何使用带 SI 前缀的自定义单位格式化 matplotlib Y 轴。
类似地,我们可以使用 UliEngineering.Math.Decibel 将图(最值得注意的是具有对数 Y 轴的图)格式化为分贝:
decibel_formatter.py
from UliEngineering.Math.Decibel import *
import matplotlib.ticker as mtick
def decibel_formatter(v0=1.0, unit='dB'):
def format_value(value, pos=None):
dB = value_to_dB(value, v0=v0)
return f'{dB:.0f} {unit}'
return format_value
# 使用示例:
plt.gca().set_yscale("log") # Optional
plt.gca().yaxis.set_major_formatter(mtick.FuncFormatter(decibel_formatter()))Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow