Plot Interaction of Categorical Factors
In this example, we will vizualize the interaction between categorical factors. First, we will create some categorical data are initialized. Then plotted using the interaction_plot function which internally recodes the x-factor categories to ingegers.
In [1]:
1 2 3 4 | <span class = "kn" > import < / span> <span class = "nn" >numpy< / span> <span class = "kn" >as< / span> <span class = "nn" >np< / span> <span class = "kn" > import < / span> <span class = "nn" >matplotlib.pyplot< / span> <span class = "kn" >as< / span> <span class = "nn" >plt< / span> <span class = "kn" > import < / span> <span class = "nn" >pandas< / span> <span class = "kn" >as< / span> <span class = "nn" >pd< / span> <span class = "kn" > from < / span> <span class = "nn" >statsmodels.graphics.factorplots< / span> <span class = "kn" > import < / span> <span class = "n" >interaction_plot< / span> |
In [2]:
1 2 3 4 | <span class = "n" >np< / span><span class = "o" >.< / span><span class = "n" >random< / span><span class = "o" >.< / span><span class = "n" >seed< / span><span class = "p" >(< / span><span class = "mi" > 12345 < / span><span class = "p" >)< / span> <span class = "n" >weight< / span> <span class = "o" > = < / span> <span class = "n" >pd< / span><span class = "o" >.< / span><span class = "n" >Series< / span><span class = "p" >(< / span><span class = "n" >np< / span><span class = "o" >.< / span><span class = "n" >repeat< / span><span class = "p" >([< / span><span class = "s" > 'low' < / span><span class = "p" >,< / span> <span class = "s" > 'hi' < / span><span class = "p" >,< / span> <span class = "s" > 'low' < / span><span class = "p" >,< / span> <span class = "s" > 'hi' < / span><span class = "p" >],< / span> <span class = "mi" > 15 < / span><span class = "p" >),< / span> <span class = "n" >name< / span><span class = "o" > = < / span><span class = "s" > 'weight' < / span><span class = "p" >)< / span> <span class = "n" >nutrition< / span> <span class = "o" > = < / span> <span class = "n" >pd< / span><span class = "o" >.< / span><span class = "n" >Series< / span><span class = "p" >(< / span><span class = "n" >np< / span><span class = "o" >.< / span><span class = "n" >repeat< / span><span class = "p" >([< / span><span class = "s" > 'lo_carb' < / span><span class = "p" >,< / span> <span class = "s" > 'hi_carb' < / span><span class = "p" >],< / span> <span class = "mi" > 30 < / span><span class = "p" >),< / span> <span class = "n" >name< / span><span class = "o" > = < / span><span class = "s" > 'nutrition' < / span><span class = "p" >)< / span> <span class = "n" >days< / span> <span class = "o" > = < / span> <span class = "n" >np< / span><span class = "o" >.< / span><span class = "n" >log< / span><span class = "p" >(< / span><span class = "n" >np< / span><span class = "o" >.< / span><span class = "n" >random< / span><span class = "o" >.< / span><span class = "n" >randint< / span><span class = "p" >(< / span><span class = "mi" > 1 < / span><span class = "p" >,< / span> <span class = "mi" > 30 < / span><span class = "p" >,< / span> <span class = "n" >size< / span><span class = "o" > = < / span><span class = "mi" > 60 < / span><span class = "p" >))< / span> |
In [3]:
1 2 3 | <span class = "n" >fig< / span><span class = "p" >,< / span> <span class = "n" >ax< / span> <span class = "o" > = < / span> <span class = "n" >plt< / span><span class = "o" >.< / span><span class = "n" >subplots< / span><span class = "p" >(< / span><span class = "n" >figsize< / span><span class = "o" > = < / span><span class = "p" >(< / span><span class = "mi" > 6 < / span><span class = "p" >,< / span> <span class = "mi" > 6 < / span><span class = "p" >))< / span> <span class = "n" >fig< / span> <span class = "o" > = < / span> <span class = "n" >interaction_plot< / span><span class = "p" >(< / span><span class = "n" >x< / span><span class = "o" > = < / span><span class = "n" >weight< / span><span class = "p" >,< / span> <span class = "n" >trace< / span><span class = "o" > = < / span><span class = "n" >nutrition< / span><span class = "p" >,< / span> <span class = "n" >response< / span><span class = "o" > = < / span><span class = "n" >days< / span><span class = "p" >,< / span> <span class = "n" >colors< / span><span class = "o" > = < / span><span class = "p" >[< / span><span class = "s" > 'red' < / span><span class = "p" >,< / span> <span class = "s" > 'blue' < / span><span class = "p" >],< / span> <span class = "n" >markers< / span><span class = "o" > = < / span><span class = "p" >[< / span><span class = "s" > 'D' < / span><span class = "p" >,< / span> <span class = "s" > '^' < / span><span class = "p" >],< / span> <span class = "n" >ms< / span><span class = "o" > = < / span><span class = "mi" > 10 < / span><span class = "p" >,< / span> <span class = "n" >ax< / span><span class = "o" > = < / span><span class = "n" >ax< / span><span class = "p" >)< / span> |
Please login to continue.