【问题标题】:Pandas boxplot covers/overlays matplotlib plotPandas boxplot 覆盖/覆盖 matplotlib 图
【发布时间】:2015-11-30 16:57:39
【问题描述】:

根据here 的报道,我尝试创建一个带有叠加散点图的箱线图。

但是当我跑步时:

In [27]: table1.t_in[table1.duration==6]
Out[27]: 
counter
7     308.351304
9     305.354920
14    307.832732
15    332.097405
21    309.711144
22    308.227617
23    317.342377
24    306.140126
25    339.185127
27    336.411869
30    313.287353
Name: t_in, dtype: float64
In [28]: table1.t_in[table1.duration==7]
Out[28]: 
counter
10    401.891105
11    384.290236
13    387.516037
17    369.366080
18    383.584934
19    357.466159
20    380.888071
26    399.989748
34    353.118944
Name: t_in, dtype: float64
In [29]: fig,ax=plt.subplots()
    ...: 
    ...: for i in [6,7]:
    ...:     y = table1.t_in[table1.duration==i]
    ...:     # Add some random "jitter" to the x-axis
    ...:     x = np.random.normal(i, 0.03, size=len(y))
    ...:     ax.plot(x, y, 'r.', alpha=0.5)
    ...:     
    ...: bp = table1.boxplot(column='t_in',by='duration',grid=False,ax=ax)

我得到:

如果我只跳过最后一行,我会得到:

我怎样才能像链接的问题一样绘制?

【问题讨论】:

    标签: python pandas matplotlib plot boxplot


    【解决方案1】:

    使用 ipython 笔记本。我尝试了 matplotlib 的 boxplot-Methode。您不能包含在 for 循环中。但希望它有所帮助。

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    %matplotlib inline
    
    # data
    table1 = pd.DataFrame({"t_in":[308.351304, 305.354920, 307.832732,
                               332.097405, 309.711144, 308.227617,
                               317.342377, 306.140126, 339.185127,
                               336.411869, 313.287353, 401.891105,
                               384.290236, 387.516037, 369.366080,
                               383.584934, 357.466159, 380.888071,
                               399.989748, 353.118944]}, 
                           index=[7,9,14,15,21,22,23,24,25,27,30,
                                  10,11,13,17,18,19,20,26,34])
    
    table1["duration"] = np.where(table1["t_in"]<353, 6, 7)
    
    # plotting
    fig,ax = plt.subplots()
    colors = ["red", "blue"]
    for i in [6,7]:
        y = table1.t_in[table1.duration==i]
        # Add some random "jitter" to the x-axis
        x = np.random.normal(i, 0.03, size=len(y))
        ax.scatter(x, y, c=colors[i-6], alpha=0.5)
    
    ax.boxplot([table1.t_in[table1.duration==6].values,table1.t_in[table1.duration==7].values], positions=[6,7])
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2021-10-27
      • 2018-02-06
      • 2020-08-06
      • 2021-01-19
      • 2019-04-14
      • 2018-12-20
      • 1970-01-01
      • 2011-06-11
      • 2012-01-28
      相关资源
      最近更新 更多