使用for循环绘制多个图形

时间:2020-04-16 03:10:29

标签: python-3.x loops for-loop matplotlib

我总共有35个主题,并且我尝试使用for循环为每个主题制作一个单独的图形,但是我的代码为我提供了一个包含所有主题的密集图形。

all_subnames = final['sub'].unique() # this shows all the subjects 1-35

[for i in all_subnames:
    print(i)
    subs = final\[final\['sub'\]== i\]
    sns.lineplot(x = subs.index.values,y = subs\['RT'\])][1]

2 个答案:

答案 0 :(得分:0)

您要为for循环中的每次迭代创建一个新的图形和轴。 这是一种方法。

import seaborn as sns

## made up date with 10 rows, 5 columns
y = np.random.randint(low=1,high=10,size=(10,5))

# iterate over the data
for item in range(y.shape[1]):

    # create a new figure and axis object  
    fig, ax = plt.subplots()

    # give this axis to seaborn so that it can plot in each iteration
    sns.lineplot(np.arange(y.shape[0]) ,y[:,item], ax = ax)

答案 1 :(得分:0)

确保为每个图打开一个新图形:

import matplotlib
import matplotlib.pyplot as plt
import seaborn

all_subnames = final['sub'].unique() # this shows all the subjects 1-35

for i in all_subnames:
    print(i)
    subs = final\[final\['sub'\]== i\]

    # Creates a new figure for the plot of the subject
    plt.figure()
    sns.lineplot(x = subs.index.values,y = subs\['RT'\])][1]