This tutorial shows how you can plot Two-Way ANOVA interaction in Python. In particular, you can use `interaction_plot()`

function from statsmodels.graphics to plot the Two-way ANOVA.

## Step 1: Prepare the data

Suppose that there are two categorical variables, namely `city`

(city 1 and city 2) and `store`

(store 1 and store 2). The dependent variable is `sales`

. We can generate a hypothetical data using Python.

```
# import the module of Numpy
import numpy as np
# generate two arrays of city and store
# generate IV of city
city = np.repeat(['City1','City2'],5)
print("IV of city: \n", city)
# generate IV of store
store = np.tile(['store1','store2'], 5)
print("IV of store: \n",store)
# generate DV of sales
sales=[10,20,20,50,30,10,5,4,12,4]
print("DV of sales: \n",sales)
```

Output:

IV of city: ['City1' 'City1' 'City1' 'City1' 'City1' 'City2' 'City2' 'City2' 'City2' 'City2'] IV of store: ['store1' 'store2' 'store1' 'store2' 'store1' 'store2' 'store1' 'store2' 'store1' 'store2'] DV of sales: [10, 20, 20, 50, 30, 10, 5, 4, 12, 4]

## Step 2: Use `interaction_plot()`

```
# import the module of matplotlib
import matplotlib.pyplot as plt
# import interaction_plot from statsmodels
from statsmodels.graphics.factorplots import interaction_plot
# set the figure size, and you can change the numbers if you prefer
fig, ax = plt.subplots(figsize=(6, 6))
# the following is the key plot statement
# IV of city on the x-axis
# IV of store on the y-axis
# DV of sales as "response"
fig = interaction_plot(
x=city,
trace=store,
response=sales,
colors=["red", "blue"],
markers=["D", "^"],
ms=10,
ax=ax,
)
```

The following is the output, namely the plot of the two-way ANOVA.

As we can see, this plots the interaction of city and store. The four points on the plots actually are the means (see below). Thus, this method also shows how you can plot four means in Python.

sales city store City1 store1 20.0 store2 35.0 City2 store1 8.5 store2 6.0

## Further Reading

- Two-Way ANOVA (R, Python)
- Tutorial of data visualization using Python