0

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"])
` 

Link plot:
enter image description here

3
  • 1
    The height of these bars might be zero, thus not showing on the plot. Commented Mar 18, 2022 at 7:10
  • can you share the df["frecuencia relativa"] column? Commented Mar 18, 2022 at 7:11
  • You could plot the relative frequencies via (df["hijos"].value_counts() / len(df)).plot.bar(rot=0). Or (df["hijos"].value_counts() / len(df)).sort_index().plot.bar(rot=0) to have the x-axis in numerical order (instead of ordered by bar heights). Commented Mar 18, 2022 at 8:42

1 Answer 1

1

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

enter image description here

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"]).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the comments, my error was in df["Frecuencia"]. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.