# How to plot a spectrum with dB as y-axis with PythonPlot

**URL:** https://discourse.julialang.org/t/how-to-plot-a-spectrum-with-db-as-y-axis-with-pythonplot/100172
**Category:** Visualization
**Tags:** question, package, plotting, pythonplot
**Created:** [June 11, 2023, 7:44am UTC](https://discourse.julialang.org/t/how-to-plot-a-spectrum-with-db-as-y-axis-with-pythonplot/100172 "2023-06-11T07:44:38Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 11, 2023, 7:44am UTC](https://discourse.julialang.org/t/how-to-plot-a-spectrum-with-db-as-y-axis-with-pythonplot/100172/1 "2023-06-11T07:44:38Z")

</div>

I am using PythonPlot and want to have a logarithmic y axis formatted in dezibel (dB). In Python I could use the following code:

```python
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
# Usage example:
plt.gca().set_yscale("log") # Optional
plt.gca().yaxis.set_major_formatter(mtick.FuncFormatter(decibel_formatter()))

```

Source: [https://techoverflow.net/2023/03/13/how-to-format-axis-as-db-decibel-using-matplotlib/](https://techoverflow.net/2023/03/13/how-to-format-axis-as-db-decibel-using-matplotlib/)

How can I do this in Julia? In particular, how can I write a custom formatter in Julia?

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [June 11, 2023, 8:01am UTC](https://discourse.julialang.org/t/how-to-plot-a-spectrum-with-db-as-y-axis-with-pythonplot/100172/2 "2023-06-11T08:01:15Z")

</div>

What I have so far:

```julia
using ControlSystemsBase, PythonPlot

function todb(mag)
    20 * log10(mag)
end

P = tf(0.01934, [1, 0.01934])

w = exp10.(LinRange(-6, 1, 200));
mag, w1 = bode(P, w)
mag = mag[:]

db = true
if db
    plot(w, todb.(mag))
    ax = gca()
    ax.set_xscale("log")
    ylabel("Magnitude [dB]", fontsize = 14)
else
    loglog(w, mag)
end
xlabel("Frequency [rad/s]", fontsize = 14)

grid(true, which="both", ls="-.")
nothing

```

![Figure_1](https://global.discourse-cdn.com/julialang/original/3X/f/f/ff138aac3418a8c5dcd8b9f85a9db50533460797.png)

That’s good enough even without custom formatter…
