'I have 4 subplots with 4 different variables. How could I make this to be one function instead of four?
fig, axes = plt.subplots(2,2, figsize=(10, 10))
sns.boxplot(data = dna_complete,
ax=axes[0,0],
x = "Category",
y = "x1",
palette="coolwarm",
axes[0,0].set_title("title")
axes[0,0].tick_params(labelrotation=20)
#second
fig, axes = plt.subplots(2,2, figsize=(10, 10))
sns.boxplot(data = dna_complete,
ax=axes[0,1],
x = "Category",
y = "x2",
palette="coolwarm",
axes[0,0].set_title("title")
axes[0,0].tick_params(labelrotation=20)
#third
fig, axes = plt.subplots(2,2, figsize=(10, 10))
sns.boxplot(data = dna_complete,
ax=axes[1,0],
x = "Category",
y = "x3",
palette="coolwarm",
axes[0,0].set_title("title")
axes[0,0].tick_params(labelrotation=20)
#fourth
fig, axes = plt.subplots(2,2, figsize=(10, 10))
sns.boxplot(data = dna_complete,
ax=axes[1,1],
x = "Category",
y = "x4",
palette="coolwarm",
axes[0,0].set_title("title")
axes[0,0].tick_params(labelrotation=20)'''
So I would want to make this to be one funtion instead of four different functions, how do you do that? So what I would like to make is something like this:
for i in range(1, 5):
plt.subplot(2, 2, i)
plt.text(0.5, 0.5, str((2, 2, i)),
fontsize=18, ha='center')
where you dont have to repeat your variable each time. Can someone help me? thanks in advance!
Solution 1:[1]
I guess something along the lines of
for i in range(4):
sns.boxplot(data=dna_complete,
ax=axes[i%2, i//2],
x="Category",
y=f"x{i+1}",
palette="coolwarm")
I think you've forgotten a closing parenthesis in your own example code, by the way.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | 9769953 |
