1

I have tried to draw a histogram using matplotlib and the pandas but while drawing the smooth curve it gave me an error I can you please help to resolve this and maybe give me some method to draw the smooth curve on histogram using matplotlib I am trying not to use any another library (seaborn) here is the code

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()

the error shown is

 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'

and is this possible to draw the smooth curve using only the matplotlib library

edit 1: thanks for the answer I was finally able to spot the error enter image description here

Somesh
  • 31
  • 7

2 Answers2

0

n has the same size with zee, which is 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')

Output:

enter image description here

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
0

In your code, zee is a matplotlibobject Rectangle object. However, the plot function need a float as input.

Since what you are plotting is a normal distribution. Also, you like the curve to be smooth. So why not generate a normal distribution and plot it into same figure. Here is a modified version of your code.

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()

Output of the plot: enter image description here

Wilson.L
  • 91
  • 3
  • Why do we need to find the pdf of the function we can do the same thing with the ` plt.plot(np.round(zee),n,'ro') ` – Somesh Dec 14 '20 at 16:17
  • I though you was looking for a theoretical description of your data. That's what I am guessing your "smooth curve" mean. You can do a fitting too, if you are processing real data. – Wilson.L Dec 14 '20 at 16:24