【问题标题】:Plotting multiple graph using for loop使用 for 循环绘制多个图
【发布时间】:2020-04-16 03:10:29
【问题描述】:

我总共有 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]

【问题讨论】:

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


    【解决方案1】:

    您想为 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)
    

    【讨论】:

      【解决方案2】:

      确保为每个情节打开一个新图:

      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]
      

      【讨论】:

        猜你喜欢
        • 2021-05-25
        • 1970-01-01
        • 2022-01-05
        • 1970-01-01
        • 2020-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-31
        相关资源
        最近更新 更多