簡體   English   中英

我有 4 個帶有 4 個不同變量的子圖。 我怎樣才能使它成為一個 function 而不是四個?

[英]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)'''

所以我想讓它成為一個功能而不是四個不同的功能,你是怎么做到的? 所以我想做的是這樣的:

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')

你不必每次都重復你的變量。 有人能幫我嗎? 提前致謝!

我猜是這樣的

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")

順便說一句,我認為您在自己的示例代碼中忘記了右括號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM