Reputation: 1
I want to make a plot bar,but when i display values like 1,7,15, dont show up.
df = pd.DataFrame()
df["hijos"]=(0,5,6,0,2,4,0,4,10,15,0,6,2,3,0,4,5,4,8,7,3,5,3,2,6,3,3,0,2,2,6,1,2,3,2,2,4,4,4,6,1,2)
df["frecuencia relativa"] = df["Frecuencia"]/n
plt.bar( df["hijos"],df["frecuencia relativa"])
`
Upvotes: 0
Views: 135
Reputation: 85
It's difficult to see where this fails, because we don't know what df["Frecuencia"]
contains, and the mistake is probably in the creation of this. Consider providing a Minimal reproducible example next time.
What you want to achieve could be done like this, where frecuencia_relativa
is a pandas Series.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame()
df["hijos"]=(0,5,6,0,2,4,0,4,10,15,0,6,2,3,0,4,5,4,8,7,3,5,3,2,6,3,3,0,2,2,6,1,2,3,2,2,4,4,4,6,1,2)
frecuencia_relativa = df.hijos.value_counts()/len(df["hijos"])
plt.bar(frecuencia_relativa.index, frecuencia_relativa.values)
plt.show()
EDIT:
It seems like the problem is that you created df["Frecuencia"]
by
df["Frecuencia"] = df.hijos.value_counts()
This doesn't work, because it assigns the i-th values of the pandas Series df.hijos.value_counts()
to the i-th row of df
, resulting in this:
hijos frecuencia relativa
0 0 0.142857
1 5 0.047619
2 6 0.214286
3 0 0.142857
4 2 0.166667
5 4 0.071429
6 0 0.119048
7 4 0.023810
8 10 0.023810
9 15 NaN
10 0 0.023810
11 6 NaN
12 2 NaN
13 3 NaN
14 0 NaN
15 4 0.023810
16 5 NaN
17 4 NaN
18 8 NaN
19 7 NaN
20 3 NaN
21 5 NaN
22 3 NaN
23 2 NaN
24 6 NaN
25 3 NaN
26 3 NaN
27 0 NaN
28 2 NaN
29 2 NaN
30 6 NaN
31 1 NaN
32 2 NaN
33 3 NaN
34 2 NaN
35 2 NaN
36 4 NaN
37 4 NaN
38 4 NaN
39 6 NaN
40 1 NaN
41 2 NaN
The values are thus in the wrong rows and therefore their positions on the x axis will be wrong, if plotted with plt.bar( df["hijos"],df["frecuencia relativa"])
.
Upvotes: 1