X轴绘图频率

2024-05-15 03:02:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须画一个倒v形或帐篷形曲线(拉普拉斯)。你知道吗

这里提供了python语法的示例数据

import matplotlib.pyplot as plt
#x = np.linspace(-8., 8., 5000)
x= np.array([0, 2, 4, 5, 1, 0, 5, 1, 4, 5, 8, 3, 6])
pdf = np.exp(-abs(x-loc)/scale)/(2.*scale)
plt.plot(x, pdf)

当我画这个的时候,它给了我一个奇怪的画面。有没有一个简单的方法来绘制,这样我就有了x轴中x元素的频率,y轴中pdf元素的频率?你知道吗

进一步澄清- 我需要将pdf索引到x中元素的相应值,然后x的相应值具有n-frequency。我需要用n-frequencypdf作图。你知道吗

对于debug,您可以取消注释x = np.linspace(-8., 8., 5000)和注释x= np.array([0, 2, 4, 5, 1, 0, 5, 1, 4, 5, 8]),您将看到倒V曲线。你知道吗

我对R或python中的解决方案或建议都没有意见。你知道吗

曲线应该是什么样的示例

enter image description here


Tags: 数据import元素示例pdfnp语法plt
1条回答
网友
1楼 · 发布于 2024-05-15 03:02:11
import numpy as np 
import matplotlib.pyplot as plt
x = np.random.normal(scale=5, size=50000) # create example data
bins = np.linspace(-15, 15, 31) # make bins to count occurrences within 
counts, bins = np.histogram(x, bins) # do the counting
# counts is the number of occurences of x in each bin 
bins = (bins[1:]+bins[:-1])/2.0 # take the midpoints of the bins for plotting
plt.loglog(bins, counts) # you'll only get the triangle shape in log-log space

用正确的数据替换x我想这就是你想要的。你需要计算每个箱子里的元素数量。你所附的链接告诉你箱子是什么。可以使用np.linspacenp.arange创建此数组。你知道吗

我最近学到的一个补充,如果你想在对数空间中绘制直方图,使用对数间隔的容器是明智的,这样点在对数空间中的间隔是均匀的。你可以用np.geomspace来做这个。你知道吗

相关问题 更多 >

    热门问题