Achse als dB (Dezibel) mit matplotlib formatieren

In unserem vorherigen Beitrag Matplotlib benutzerdefinierter SI-Präfix-Einheiten-Tick-Formatter haben wir gezeigt, wie man eine matplotlib-Y-Achse mit einer benutzerdefinierten Einheit mit SI-Präfixen formatiert.

Ebenso können wir UliEngineering.Math.Decibel verwenden, um Plots (insbesondere Plots mit logarithmischer Y-Achse) als Dezibel zu formatieren:

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

# Anwendungsbeispiel:
plt.gca().set_yscale("log") # Optional

plt.gca().yaxis.set_major_formatter(mtick.FuncFormatter(decibel_formatter()))

Check out similar posts by category: Python