- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
下面几行
a1, b1, _ = plt.hist(df['y'], bins='auto')
a2, b2 = np.histogram(df['y'], bins='auto')
print(a1 == a2)
print(b1 == b2)
等于 a1
的所有值都等于 a2
的值,并且 b1
和 b2
的值相同
然后我单独使用 pyplot
创建了一个图(使用 bins=auto
should use the same np.histogram()
function ):
plt.hist(df['y'], bins='auto')
plt.show()
然后我尝试实现相同的直方图,但我自己调用 np.histogram()
,并将结果传递给 plt.hist()
,但我得到空白直方图:
a2, b2 = np.histogram(df['y'], bins='auto')
plt.hist(a2, bins=b2)
plt.show()
根据我对 plt.hist(df['y'], bins='auto')
的理解,我创建的这两个图应该完全相同 - 为什么不一样我使用 Numpy
的方法有效吗?
编辑
根据下面@MSeifert 的回答,我相信对于
counts, bins = np.histogram(df['y'], bins='auto')
bins
是每个 bin 的起始值列表,counts
是每个 bin 中值的相应数量。如上面的直方图所示,这应该会产生近乎完美的正态分布,但是,如果调用 print(counts, bins)
the result of counts
显示第一个和最后一个 bin 的数量相当可观,约为 11,000。为什么这没有反射(reflect)在直方图中——为什么两条尾部都没有两个大尖峰?
编辑 2
这只是一个分辨率问题,而且我的绘图似乎太小以至于两端的尖峰无法正确呈现。放大允许它们显示。
最佳答案
您假设 plt.hist
可以区分包含作为值计数的数组和包含要计数的值的数组。
但事实并非如此,当您将计数传递给 plt.hist
时,它会对它们进行计数并将它们放入提供的容器中。这可能会导致直方图空洞,但也会导致直方图怪异。
因此,虽然 plt.hist
和 numpy.histogram
的工作原理相同,但您不能只是将从 numpy.histogram
获得的数据传递给 plt.hist
因为那会计算值的计数(不是你期望的):
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
f, ax = plt.subplots(1)
arr = np.random.normal(10, 3, size=1000)
cnts, bins = np.histogram(arr, bins='auto')
ax.hist(cnts, bins=bins)
但是,您可以使用 bar
图来可视化 numpy.histogram
获得的直方图:
f, (ax1, ax2) = plt.subplots(2)
cnts, bins = np.histogram(arr, bins='auto')
ax1.bar(bins[:-1] + np.diff(bins) / 2, cnts, np.diff(bins))
ax2.hist(arr, bins='auto')
关于python - plt.hist() vs np.histogram() - 意想不到的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46656010/
.plt :在 RE 能段中,蹦床功能在 plt[n]除了 0,在 plt[0] 处有 .got.plt 解析器链接 .got .got.plt : 在 RW 段中,只是地址 我从这篇文章中学到的:
这是我的代码,我从 plt.savefig 和 plt.show 得到不同的结果 我尝试过使用bbox_inches='tight',但这使得图片尺寸变得不完全是700*400。 x=np.linsp
我一直在通过反汇编一些 C 代码来学习汇编语言。当我用 GDB 反汇编这个基本的 C 代码时: #include void main(void) { printf("Hello World\
在matplotlib.pyplot中,plt.clf()和plt.close()有什么区别?它们会以同样的方式运作吗? 我正在运行一个循环,在每次迭代结束时,我都会生成一个图形并保存该图。在第一次尝
我很好奇:PLT-Scheme(现在称为 Racket )中的“PLT”代表什么?我能得到的最接近答案是 this page 上的““PLT”指的是 Racket 开发团队的核心团队”。 . 最佳答案
我在使用 matplotlib 绘图时需要使用所需的 OTF 字体,但不知道如何访问它。我看到了How to use a (random) *.otf or *.ttf font in matplot
我正在尝试从 2D 矩阵中找出尽可能多的数据可视化工具(加分点是查看 2D 矩阵的任何其他好方法)。 我生成了很多热图,有人告诉我 pcolor 是要走的路(我现在使用 seaborn)。 为什么 p
我想知道matplotlib.pyplot的plt.plot(x,y)和plt.show()命令之间进行的基本 Backbone 流程。 详细说明一下,这段代码: plt.plot(x , y)
鉴于以下情况: import matplotlib.pyplot as plt import numpy as np #http://matplotlib.org/api/pyplot_api.htm
我正在尝试将普通的 matplotlib.pyplot plt.plot(x,y) 与变量 y 组合为变量 x 带有箱线图。但是,我只想在 x 的某些(可变)位置上绘制箱线图,但这在 matplotl
我想知道为什么有些人在 plt.show() 之前将 plt.draw() 放入他们的代码中。对于我的代码, plt.draw() 的行为似乎并没有改变输出的任何内容。我在互联网上进行了搜索,但找不到
我在这个 helpful answer 中发现了当在 y 轴上使用对数刻度时,plt.scatter() 和 plt.plot() 的行为不同。 使用 plot,我可以在使用 plt.show() 之
我正在尝试生成一个简单的 pyplot 条形图,Python 3.6.0。代码如下: import random import matplotlib.pyplot as plt import coll
from matplotlib import pyplot as plt import matplotlib.pyplot as plt 以上说法是否等价?哪种形式更具可读性/更好? 最佳答案 尽管它
这些meshgrid对我来说使用起来有点困惑。我正在尝试使用 x 和 y 坐标绘制散点图,并在散点图上叠加等高线图,并为 z 连续展开> 坐标。类似于高程图。 如果我将 meshgrid 与 x、y
这是我的代码中出现错误的部分(都与图表相关,但要点: plt.figure (figsize = (10,6)) plt.title ("Alfa x CL") plt.plot (Alpha,CL,
尝试构建已签名的 APK 时,失败并重复约 100 行: Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/pre
我想看看函数运行需要多长时间。在 PLT-Scheme 中最简单的方法是什么?理想情况下,我希望能够做这样的事情: > (define (loopy times) (if (zero? times
读完两本 Schemer 书后,我即将开始 HtDP,但也发现了 http://docs.plt-scheme.org/guide Material 。 前面提到的书似乎更针对 Scheme,而后者更
我的数据集如下所示: Month DeviceType AvgRevenue 0 201608 desktop 3.029642 1 201608 mobile
我是一名优秀的程序员,十分优秀!