使用 matplotlib .pyplot 绘制直方图和位于直方图上的平滑曲线
using the matplotlib .pylot for drawing histogram and the smooth curve which lies on the histogram
我曾尝试使用 matplotlib 和 pandas 绘制直方图,但是在绘制平滑曲线时它给了我一个错误,请你帮忙解决这个问题,也许给我一些绘制平滑曲线的方法使用 matplotlib 在直方图上绘制曲线我正在尝试不使用任何其他库(seaborn)这里是代码
mu,sigma = 100,15
plt.style.use('dark_background')
x = mu + sigma * np.random.randn(10000)
n,bins,patches = plt.hist(x,bins=50,density=1,facecolor='g',alpha = 0.5)
zee=bins[:-1]
plt.plot(np.round(zee),patches,'ro')
plt.xlabel('Smarts')
plt.ylabel('Probablity')
plt.title('Histogram of the Iq')
plt.axis([40,160,0,0.03])
plt.grid(1)
plt.show()
显示的错误是
python3 -u "/home/somesh/Downloads/vscode_code/python ml course /firstml.py"
Traceback (most recent call last):
File "/home/somesh/Downloads/vscode_code/python ml course /firstml.py", line 149, in <module>
plt.plot(np.round(zee),patches,'ro')
File "/home/somesh/.local/lib/python3.8/site-packages/matplotlib/pyplot.py", line 2840, in plot
return gca().plot(
File "/home/somesh/.local/lib/python3.8/site-packages/matplotlib/axes/_axes.py", line 1745, in plot
self.add_line(line)
File "/home/somesh/.local/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 1964, in add_line
self._update_line_limits(line)
File "/home/somesh/.local/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 1986, in _update_line_limits
path = line.get_path()
File "/home/somesh/.local/lib/python3.8/site-packages/matplotlib/lines.py", line 1011, in get_path
self.recache()
File "/home/somesh/.local/lib/python3.8/site-packages/matplotlib/lines.py", line 658, in recache
y = _to_unmasked_float_array(yconv).ravel()
File "/home/somesh/.local/lib/python3.8/site-packages/matplotlib/cbook/__init__.py", line 1289, in _to_unmasked_float_array
return np.asarray(x, float)
File "/home/somesh/.local/lib/python3.8/site-packages/numpy/core/_asarray.py", line 85, in asarray
return array(a, dtype, copy=False, order=order)
TypeError: float() argument must be a string or a number, not 'Rectangle'
是否可以仅使用 matplotlib 库绘制平滑曲线
编辑 1:感谢您的回答,我终于能够发现错误
n
与zee
大小相同,即length(bins)-1
:
mu,sigma = 100,15
plt.style.use('dark_background')
x = mu + sigma * np.random.randn(10000)
n,bins,patches = plt.hist(x,bins=50,density=1,facecolor='g',alpha = 0.5)
zee=bins[:-1]
## this
plt.plot(np.round(zee),n,'ro')
输出:
在您的代码中,zee
是一个 matplotlibobject 矩形对象。但是,绘图函数需要 float
作为输入。
因为您绘制的是正态分布。此外,您希望曲线平滑。那么为什么不生成正态分布并将其绘制到同一个图中。这是您的代码的修改版本。
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
mu,sigma = 100,15
plt.style.use('dark_background')
x = mu + sigma * np.random.randn(10000)
n,bins,patches = plt.hist(x,bins=50,density=1,facecolor='g',alpha = 0.5)
# zee=bins[:-1]
# plt.plot(np.round(zee),patches,'ro')
x_overlay = np.linspace(mu - 3*sigma, mu + 3*sigma, 100)
plt.plot(x_overlay, stats.norm.pdf(x_overlay, mu, sigma),"ro")
plt.xlabel('Smarts')
plt.ylabel('Probablity')
plt.title('Histogram of the Iq')
plt.axis([40,160,0,0.03])
plt.grid(1)
plt.show()
绘图输出:
我曾尝试使用 matplotlib 和 pandas 绘制直方图,但是在绘制平滑曲线时它给了我一个错误,请你帮忙解决这个问题,也许给我一些绘制平滑曲线的方法使用 matplotlib 在直方图上绘制曲线我正在尝试不使用任何其他库(seaborn)这里是代码
mu,sigma = 100,15
plt.style.use('dark_background')
x = mu + sigma * np.random.randn(10000)
n,bins,patches = plt.hist(x,bins=50,density=1,facecolor='g',alpha = 0.5)
zee=bins[:-1]
plt.plot(np.round(zee),patches,'ro')
plt.xlabel('Smarts')
plt.ylabel('Probablity')
plt.title('Histogram of the Iq')
plt.axis([40,160,0,0.03])
plt.grid(1)
plt.show()
显示的错误是
python3 -u "/home/somesh/Downloads/vscode_code/python ml course /firstml.py"
Traceback (most recent call last):
File "/home/somesh/Downloads/vscode_code/python ml course /firstml.py", line 149, in <module>
plt.plot(np.round(zee),patches,'ro')
File "/home/somesh/.local/lib/python3.8/site-packages/matplotlib/pyplot.py", line 2840, in plot
return gca().plot(
File "/home/somesh/.local/lib/python3.8/site-packages/matplotlib/axes/_axes.py", line 1745, in plot
self.add_line(line)
File "/home/somesh/.local/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 1964, in add_line
self._update_line_limits(line)
File "/home/somesh/.local/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 1986, in _update_line_limits
path = line.get_path()
File "/home/somesh/.local/lib/python3.8/site-packages/matplotlib/lines.py", line 1011, in get_path
self.recache()
File "/home/somesh/.local/lib/python3.8/site-packages/matplotlib/lines.py", line 658, in recache
y = _to_unmasked_float_array(yconv).ravel()
File "/home/somesh/.local/lib/python3.8/site-packages/matplotlib/cbook/__init__.py", line 1289, in _to_unmasked_float_array
return np.asarray(x, float)
File "/home/somesh/.local/lib/python3.8/site-packages/numpy/core/_asarray.py", line 85, in asarray
return array(a, dtype, copy=False, order=order)
TypeError: float() argument must be a string or a number, not 'Rectangle'
是否可以仅使用 matplotlib 库绘制平滑曲线
编辑 1:感谢您的回答,我终于能够发现错误
n
与zee
大小相同,即length(bins)-1
:
mu,sigma = 100,15
plt.style.use('dark_background')
x = mu + sigma * np.random.randn(10000)
n,bins,patches = plt.hist(x,bins=50,density=1,facecolor='g',alpha = 0.5)
zee=bins[:-1]
## this
plt.plot(np.round(zee),n,'ro')
输出:
在您的代码中,zee
是一个 matplotlibobject 矩形对象。但是,绘图函数需要 float
作为输入。
因为您绘制的是正态分布。此外,您希望曲线平滑。那么为什么不生成正态分布并将其绘制到同一个图中。这是您的代码的修改版本。
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
mu,sigma = 100,15
plt.style.use('dark_background')
x = mu + sigma * np.random.randn(10000)
n,bins,patches = plt.hist(x,bins=50,density=1,facecolor='g',alpha = 0.5)
# zee=bins[:-1]
# plt.plot(np.round(zee),patches,'ro')
x_overlay = np.linspace(mu - 3*sigma, mu + 3*sigma, 100)
plt.plot(x_overlay, stats.norm.pdf(x_overlay, mu, sigma),"ro")
plt.xlabel('Smarts')
plt.ylabel('Probablity')
plt.title('Histogram of the Iq')
plt.axis([40,160,0,0.03])
plt.grid(1)
plt.show()
绘图输出: