【问题标题】:Dot-plot/Ggplot with Pandas DataFrame; Column names as x-values and and the corresponding columns values as y-values带有 Pandas DataFrame 的点图/Ggplot;列名作为 x 值,对应的列值作为 y 值
【发布时间】:2020-09-20 09:35:49
【问题描述】:

我正在尝试在 Python 中为如下所示的 DataFrame 创建一个点图/ggplot:

layers_time = {'2 layers': [20.6885, 25.1765, 18.7503, 19.2090, 19.2548],
               '3 layers': [20.4360, 27.5285, 20.1958, 18.9536, 20.1673],
               '4 layers': [18.1703, 21.9665, 17.0667, 17.3353, 16.2638],
               '5 layers': [19.3265, 25.7931, 17.9827, 18.5448, 18.8970],
               '6 layers': [19.7180, 24.7097, 20.7460, 19.7300, 18.2760]
                }

layers_time = pd.DataFrame(layers_time, columns=['2 layers', '3 layers', 
                           '4 layers', '5 layers', '6 layers'])

我想要的只是将列名作为 x 值,并将它们对应的值作为 y 轴上的点。
另外,是否有一种聪明的方法可以将这些值的平均值包含为具有不同颜色的点?
我尝试过类似问题的答案,但由于 x 和 y 尺寸不同,它们不起作用?

【问题讨论】:

    标签: python pandas matplotlib plot seaborn


    【解决方案1】:

    让我们尝试melt 数据帧,然后将variable 绘制成xvalues 作为y 的散点图

    df=layers_time.melt()#.
    plt.scatter(df.variable,df.value)
    

    【讨论】:

      【解决方案2】:

      回答

      您可以使用 df.mean(axis = 0) 评估每列的平均值。
      关于点图,您可以使用seaborn.stripplot()(或seaborn.swarmplot())来实现,但在此之前,您需要通过pandas.melt() 重塑您的数据框。

      代码

      # import
      import pandas as pd
      import matplotlib.pyplot as plt
      import seaborn as sns
      
      # data
      layers_time = {'2 layers': [20.6885, 25.1765, 18.7503, 19.2090, 19.2548],
                     '3 layers': [20.4360, 27.5285, 20.1958, 18.9536, 20.1673],
                     '4 layers': [18.1703, 21.9665, 17.0667, 17.3353, 16.2638],
                     '5 layers': [19.3265, 25.7931, 17.9827, 18.5448, 18.8970],
                     '6 layers': [19.7180, 24.7097, 20.7460, 19.7300, 18.2760]
                      }
      cols = ['2 layers', '3 layers', '4 layers', '5 layers', '6 layers']
      df = pd.DataFrame(layers_time, columns = cols)
      
      # columns means evaluation
      means = df.mean(axis = 0).to_list()
      
      # dataframe reshaping
      df = pd.melt(frame = df,
                   var_name = 'n layers',
                   value_name = 'value')
      
      # figure set-up
      fig, ax = plt.subplots()
      
      # mean plot
      ax.plot(cols, means, color = 'black', marker = 'o', linestyle = '')
      
      # dot-plot
      sns.stripplot(ax = ax,
                    data = df,
                    x = 'n layers',
                    y = 'value')
      
      # show figure
      plt.show()
      

      输出

      stripplot/swarmplot比较

      stripplotswarmplot 之间的选择取决于您的数据量:swarmplot 可防止点重叠,因此当您没有过多的点并允许欣赏分布你的数据。相反,stripplot 允许点重叠,因此您会丢失有关数据分布的信息,但您可以绘制更多的点。

      【讨论】:

      • stripplot 沿 x 添加一个统一的间距,以便以统一的方式填充空间。相反,swarmplot 倾向于在每列的 x 中心保留点,并且它添加 x 间距只是为了避免点重叠。请参阅上述链接中的两个图的文档以了解更多详细信息
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      • 2014-07-03
      • 1970-01-01
      相关资源
      最近更新 更多