I need to build multiple graphs of a DataFrame
in the same window. A summarized example of my DataFrame
general is as follows:
codigo sexo ciclo
0 1 M 11
1 2 F 5
2 3 M 11
3 2 F 10
4 4 F 8
5 1 F 11
6 0 F 9
7 0 F 2
8 0 F 8
9 0 M 5
The multiple graphs are the normalized frequency of the codes by gender and cycle.
To obtain the normalized frequencies and the graph, first I create one DataFrame
for women and one for men, then using crosstab
calculate the normalized frequency and I graph it independently. Here is the code for women:
f= df[df['sexo']=='F']
f_normal = pd.crosstab(f.codigo,f.ciclo,normalize='columns')
f_normal.plot( style= 'o', stacked=True)
plt.yscale('log')
plt.ylabel('Probabilidad')
plt.legend(title='ciclo',fontsize='small' )
plt.title('Mujeres')
plt.show()
Now I have the independent graphs, what I need is that the graphs of women and men remain in the same graph. The result I want is this:
I tried to use the subplots with the following instruction but it came out empty
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
ax1 = f.plot( style= 'o', stacked=True)
ax2 = m.plot( style= 'o', stacked=True)
I'm learning to make graphics, I appreciate the help you can give me.
Your idea is really good, you just need to pass the axis for each plot to pandas.DataFrame.plot using the argument
ax
.A reproducible example including the rest of the configuration: