简体   繁体   English

Python-Matplotlib:标准化y轴以显示标准差的倍数

[英]Python - Matplotlib: normalizing y-axis to show multiples of standard deviation

I would like to renormalize my y-axis to show my signal in multiples of sigma (standard deviation). 我想重新标准化我的y轴,以sigma(标准偏差)的倍数显示信号。 For example, one then could say at 50Hz there's a 3 sigma signal while at 3Hz there's a 0.5 sigma signal. 例如,有人可以说在50Hz时有一个3 sigma信号,而在3Hz时有0.5 sigma信号。

I thought using plt.yticks() could be the way to go: 我认为使用plt.yticks()可能是一种方法:

import numpy as np
import matplotlib.pyplot as plt

X = range(0,50,2)
Y = range(0,50,2)

signal_sigma = np.std(Y)

plt.figure()
plt.plot(X, Y)
plt.yticks(np.arange(0, 25*signal_sigma, signal_sigma))
y_labels = [r"${} \sigma$".format(i) for i in range(0, 26)]
plt.ylabel(y_labels)
plt.show()

But this doesn't seem quite right yet. 但这似乎还不太正确。 What am I missing? 我想念什么?

UPDATE: 更新:

This is what I would like to do: What does a 1-sigma, a 3-sigma or a 5-sigma detection mean? 我要这样做: 1σ,3σ或5σ检测是什么意思? The bit right below the probability table. 概率表正下方的位。

You want to set the yticklabels which is different than setting the axis label with plt.ylabel : 您要设置的yticklabels与使用plt.ylabel设置轴标签plt.ylabel

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(1000)
y = 42 * np.random.randn(1000)

signal_sigma = y.std()

num_sigma = 3
sigma_values = np.arange(-num_sigma, num_sigma+1)
yticks = signal_sigma * sigma_values
yticklabels = ['$'+str(k)+'\sigma$' if k != 0 else '$\mu$' for k in sigma_values]

plt.figure()
plt.plot(x, y)
plt.yticks(yticks, yticklabels)
plt.ylabel('the axis label')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM