This is my dataframe:
data = {'machine': ['machine_a', 'machine_a', 'machine a', 'machine a', 'machine a', 'machine a', 'machine_b', 'machine_b', 'machine_b', 'machine_b', 'machine_b', 'machine_b', 'machine_c', 'machine_c', 'machine_c', 'machine_c', 'machine_c', 'machine_c'], 'bin': ['(0, 200]', '(200, 400]', '(400, 600]', '(600, 800]', '(800, 1000]', '(1000, 1200]', '(0, 200]', '(200, 400]', '(400, 600]', '(600, 800]', '(800, 1000]', '(1000, 1200]', '(0, 200]', '(200, 400]', '(400, 600]', '(600, 800]', '(800, 1000]', '(1000, 1200]'], 'speed': [10, 0, 20, 0, 20, 10, 5, 0, 40, 10, 20, 10, 5, 25, 0, 10, 5, 10], 'Temp': [0, 0, 0, 20, 20, 0, 35, 0, 0, 20, 0, 70, 30, 0, 0, 5, 0, 25]}
df = pd.DataFrame(data)
machine bin speed Temp
0 machine_a (0, 200] 10 0
1 machine_a (200, 400] 0 0
2 machine a (400, 600] 20 0
3 machine a (600, 800] 0 20
4 machine a (800, 1000] 20 20
5 machine a (1000, 1200] 10 0
6 machine_b (0, 200] 5 35
7 machine_b (200, 400] 0 0
8 machine_b (400, 600] 40 0
9 machine_b (600, 800] 10 20
10 machine_b (800, 1000] 20 0
11 machine_b (1000, 1200] 10 70
12 machine_c (0, 200] 5 30
13 machine_c (200, 400] 25 0
14 machine_c (400, 600] 0 0
15 machine_c (600, 800] 10 5
16 machine_c (800, 1000] 5 0
17 machine_c (1000, 1200] 10 25
I want to create different bar plots for columns speed and temp, where x axis is the bins column. I want to do this for every different machine.
so far I've created a for loop to iterate over the last two columns
import seaborn as sns
import matplotlib.pyplot as plt
for column in df.columns[2:]:
sns.set()
fig, ax = plt.subplots()
sns.set(style="ticks")
sns.barplot(x = df.bin, y=column, data=df)
sns.despine(offset=10, trim=True)
fig.set_size_inches(22,14)
This creates 2 bar plots. 1 for speed and 1 for temp. How do I make so I get back 6 bar plots (2 for each machine)? Essentially how do I use groupby in this case?
Complete Synthetic Data
- 50 machines, each with 109 bins
import pandas as pd
import numpy as np
bins = [f'({n*200}, {(n+1)*200}]' for _ in range(50) for n in range(109)]
machines = [f'machine_{n}' for n in range(50) for _ in range(109)]
np.random.seed(365)
speed = np.random.randint(0, 40, size=len(machines))
temp = np.random.choice([0, 30, 70], size=len(machines))
df = pd.DataFrame({'machine': machines, 'bin': bins, 'speed': speed, 'Temp': temp})
df.head()
machine bin speed Temp
0 machine_0 (0, 200] 18 30
1 machine_0 (200, 400] 33 70
2 machine_0 (400, 600] 27 30
3 machine_0 (600, 800] 5 30
4 machine_0 (800, 1000] 34 30
df.tail()
machine bin speed Temp
5445 machine_49 (20800, 21000] 6 0
5446 machine_49 (21000, 21200] 20 30
5447 machine_49 (21200, 21400] 14 0
5448 machine_49 (21400, 21600] 38 30
5449 machine_49 (21600, 21800] 24 70