How to change yticks in PSD plot?

Viewed 260

Download Zip with signal.csv

I can create a psd plot like this:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df = pd.read_csv('signal.csv')/1000
dt = df.iloc[1,0] - df.iloc[0,0]
data=df['1']
print(f'time delta: {dt*1e12:.0f} ps')
print(f'time: {(len(data)*dt*1e6):.3f} \u03BCs')


resolution = 2000
plt.psd(data,
        resolution,
        1/0.1,
        lw=1,
        color='red');
plt.xlim(0,5)
plt.xlabel('Frequency / GHz')
plt.ylabel('Power Spectral Density / dB/Hz')
plt.show()

enter image description here

How can I change its ytickrate? When I try adding:

plt.yticks(np.arange(-100,0,10))

the whole graph is morphed:

enter image description here

How can I change the psd plot so that the y axis is represented in 10's without changing the plot?

1 Answers

You just have to set your y-lim again:

plt.ylim((-72,-25))

and you can adjust the limits to match your desired output.

Edit If you want to make it automatic you can use axs.get_ylim():

fig, axs = plt.subplots(figsize=(10, 10),constrained_layout=True)
axs.psd(data,resolution,1/0.1,lw=1,color='red')

axs.set_xlim(0,5)
axs.set_xlabel('Frequency / GHz')
axs.set_ylabel('Power Spectral Density / dB/Hz')

ylim=axs.get_ylim()
axs.set_yticks(np.arange(-100,0,10))
axs.set_ylim(ylim)

enter image description here

Related