Dan Mintz
Dan Mintz

Reputation: 69

Seaborn plot not showing all plots with a loop

sns.set(style="darkgrid")
parm = ['ALTERSKATEGORIE_GROB', 'ANREDE_KZ', 'CJT_GESAMTTYP', 'FINANZ_MINIMALIST', 'FINANZ_SPARER', 'FINANZ_VORSORGER']

for y in parm:
    plot =  sns.countplot(x=y, data=azdias_under_20)
    print (plot);

Output:
AxesSubplot(0.125,0.125;0.775x0.755)
AxesSubplot(0.125,0.125;0.775x0.755)
AxesSubplot(0.125,0.125;0.775x0.755)
AxesSubplot(0.125,0.125;0.775x0.755)
AxesSubplot(0.125,0.125;0.775x0.755)

I am getting the above output, and just one plot (not seen here) of the last list item (''FINANZ_VORSORGER'). It is in a Jupyter notebook.
Why I am not seeing all the plots, but just the last one?

Thank you.

Upvotes: 0

Views: 2734

Answers (1)

Mohsen_Fatemi
Mohsen_Fatemi

Reputation: 3391

Because you do not show the plot in each iteration, you can do it using matplotlib :

import matplotlib.pyplot as plt
sns.set(style="darkgrid")
parm = ['ALTERSKATEGORIE_GROB', 'ANREDE_KZ', 'CJT_GESAMTTYP', 'FINANZ_MINIMALIST', 'FINANZ_SPARER', 'FINANZ_VORSORGER']

for y in parm:
    sns.countplot(x=y, data=azdias_under_20)
    plt.show()

UPDATE You asked why your code didn't work. plt.show() is a method that shows all of the plots you draw before you show. so if you draw one plot and write plt.show() it will show it. but if you draw many plots, and then you write plt.show() it will mix all of them and show them all. for more information check this link . look at the following example :

>>> import matplotlib.pyplot as plt
>>> a = [1,2,3,4,5]
>>> b = [1,2,3,4,5]
>>> c = [2,3,4,5,6]
>>> d = [1,4,9,16,25]
>>> plt.plot(a,b)
[<matplotlib.lines.Line2D object at 0x00000180CF934C18>]
>>> plt.plot(a,c)
[<matplotlib.lines.Line2D object at 0x00000180CB39AA58>]
>>> plt.plot(a,d)
[<matplotlib.lines.Line2D object at 0x00000180CF934F60>]
>>> plt.show()

the resulting plot would be :

enter image description here

Upvotes: 1

Related Questions