【问题标题】:How to plot a bar chart with a range of values on one axis and the sum of corresponding values of rows of other column in that range on other axis如何在一个轴上绘制具有一系列值的条形图以及在另一个轴上该范围内其他列的行的相应值的总和
【发布时间】:2021-05-28 01:31:18
【问题描述】:

假设我有类似下面的数据,它有四列。我想将 ['R'] 列分成十个相等的范围(例如 1-10、11-20、21-30..等)并绘制这样的条形图:

在 x 轴上,我想使用 pandas 和 matplotlib 显示范围,并在 Y 轴上显示该范围内从列 ['V'] 中的相应行值的总和。

I    R  V   C   D
27  1   21  13  16
89  1   5   27  10
80  1   9   21  10
64  2   20  7   26
65  2   29  11  17
5   2   4   27  3
48  3   26  7   17
16  4   11  28  12
66  4   29  7   18
71  4   20  3   18
43  4   21  10  26
76  4   22  9   5
53  4   10  26  5
96  4   12  27  14
44  4   16  25  19
24  5   27  15  26
13  6   8   15  18
95  6   23  24  28
33  7   7   27  3
11  7   26  10  1
93  7   11  12  4
4   7   14  5   3
97  8   1   20  26
28  8   2   6   8
52  8   29  12  13
67  8   14  1   23
77  9   26  9   23
78  9   7   24  19
45  9   11  26  13
73  11  4   28  20
2   11  14  14  2
57  11  17  19  23
99  12  9   14  22
72  12  2   17  19
25  12  9   24  4
69  12  1   14  1
79  12  4   12  7
94  13  14  26  8
60  13  15  27  1
42  13  22  6   2
49  13  12  24  11
6   13  11  10  4
21  14  20  19  24
38  14  19  6   27
62  14  21  9   16
61  14  19  26  12
81  14  18  24  18
22  14  4   27  26
7   14  12  1   19
68  15  21  27  14
58  15  27  3   7
59  16  27  26  1
56  16  4   15  17
55  16  7   5   10
0   16  26  25  17
17  18  10  28  21
1   18  13  20  1
50  19  13  10  17
83  19  25  14  8
14  19  17  13  24
91  19  15  2   24
36  19  7   26  4
90  19  20  28  3
26  19  5   15  5
84  19  11  26  28
8   19  6   13  14
98  20  2   23  4
3   20  18  17  5
51  20  6   22  18
92  21  27  22  11
35  21  1   20  14
47  21  18  22  12
46  21  5   22  20
85  21  26  22  20
75  22  14  11  17
70  22  23  10  25
18  22  16  18  24
30  22  29  22  9
32  23  6   9   9
31  23  29  6   9
20  23  24  4   5
41  23  12  6   16
82  24  5   4   7
19  24  3   15  4
74  24  25  28  19
63  24  5   6   9
54  25  7   9   23
12  27  26  6   21
37  27  15  2   5
23  28  3   5   6
88  28  8   3   13
9   28  10  20  19
10  28  13  27  25
40  28  8   27  16
15  28  1   13  26
29  28  14  11  16
87  28  6   19  10
86  29  15  9   25
34  29  13  25  29
39  29  14  2   25
​

【问题讨论】:

    标签: pandas dataframe matplotlib


    【解决方案1】:

    这就是我所要求的实现方式(欢迎提出改进建议)

    import numpy as np
    import pandas as pd
    
    
    data=np.random.randint(1, 30, (100, 4)) # create numpy array with random integer between 1 and 30.
    df = pd.DataFrame(data, columns=list('RVCD')) #create Data frame 
    bins=[1, 5, 10, 20, 30,40]
    data1=df.groupby([pd.cut(df.R,bins).astype(str)])[['V']].sum().reset_index()
    data1=data1.set_index('R')
    data1.plot.bar(xlabel='Range of "R"', ylabel='Sum of "V" in the range', rot=0)
    plt.show()
    

    Here is my Bar graph

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-15
      • 1970-01-01
      • 2020-03-31
      相关资源
      最近更新 更多