繁体   English   中英

直方图:X 轴和 Y 轴值显示不正确

[英]histogram : X-axis & Y-axis values not displaying correctly

我想绘制直方图,但值显示不正确。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def hist():
    df=pd.read_csv('C:\\Users\Bhuwan Bhatt\Desktop\IP PROJECT\Book1.csv',encoding='ISO-8859-1')
    df=df.sort_values('TotalMedal',ascending=False)
    df1=df.head(n=10)
    df1=df1.loc[:,'Country']
    print(df1) 
    bins=np.arange(len(df1))
    plt.hist(df1,bins,color='lime',edgecolor='k')
    plt.xticks(rotation=20)
    plt.title('HISTOGRAM   COUNTRY V/S MEDALS',color='hotpink')
    plt.xlabel('Country~~~~>',color='navy')
    plt.ylabel('No. of Medals~~~~>',color='navy')
    plt.show()
hist()

输出出现:

y 轴上的值是这样的 0.00-0.25-0.50 到 2.00

阴谋

我希望 y 轴值符合“TotalMedal”,例如500-1000-1500-2000 ....

使用的 CSV:

Country SummerTimesPart Sumgold Sumsilver   Sumbronze   SummerTotal WinterTimesPart Wingold Winsilver   Winbronze   WinterTotal TotalTimesPart  Tbronzemedal    Tsilvermedal    Tgoldmedal  TotalMedal
 Afghanistan    14  0   0   2   2   0   0   0   0   0   14  2   0   0   2
 Algeria    13  5   4   8   17  3   0   0   0   0   16  8   4   5   17
 Argentina  24  21  25  28  74  19  0   0   0   0   43  28  25  21  74
 Armenia    6   2   6   6   14  7   0   0   0   0   13  6   6   2   14
 Australasia    2   3   4   5   12  0   0   0   0   0   2   5   4   3   12
 Australia  26  147 163 187 497 19  5   5   5   15  45  192 168 152 512
 Austria    27  18  33  36  87  23  64  81  87  232 50  123 114 82  319
 Azerbaijan     6   7   11  24  42  6   0   0   0   0   12  24  11  7   42
 Bahamas    16  6   2   6   14  0   0   0   0   0   16  6   2   6   14
 Bahrain    9   2   1   0   3   0   0   0   0   0   9   0   1   2   3
 Barbados   12  0   0   1   1   0   0   0   0   0   12  1   0   0   1
 Belarus    6   12  27  39  78  7   8   5   5   18  13  44  32  20  96
 Belgium    26  40  53  55  148 21  1   2   3   6   47  58  55  41  154
 Bermuda    18  0   0   1   1   8   0   0   0   0   26  1   0   0   1
 Bohemia    3   0   1   3   4   0   0   0   0   0   3   3   1   0   4
 Botswana   10  0   1   0   1   0   0   0   0   0   10  0   1   0   1
 Brazil     22  30  36  63  129 8   0   0   0   0   30  63  36  30  129
 British WestIndies     1   0   0   2   2   0   0   0   0   0   1   2   0   0   2
 Bulgaria   20  51  87  80  218 20  1   2   3   6   40  83  89  52  224
 Burundi    6   1   1   0   2   0   0   0   0   0   6   0   1   1   2
 Cameroon   14  3   1   2   6   1   0   0   0   0   15  2   1   3   6
 Canada 26  64  102 136 302 23  73  64  62  199 49  198 166 137 501
 Chile  23  2   7   4   13  17  0   0   0   0   40  4   7   2   13
 China  10  224 167 155 546 11  13  28  21  62  21  176 195 237 608
 Colombia   19  5   9   14  28  2   0   0   0   0   21  14  9   5   28
 Costa Rica     15  1   1   2   4   6   0   0   0   0   21  2   1   1   4
 Ivory Coast    13  1   1   1   3   0   0   0   0   0   13  1   1   1   3
 Croatia    7   11  10  12  33  8   4   6   1   11  15  13  16  15  44
 Cuba   20  78  68  80  226 0   0   0   0   0   20  80  68  78  226
 Cyprus     10  0   1   0   1   11  0   0   0   0   21  0   1   0   1
 Czech Republic     6   15  17  24  56  7   9   11  11  31  13  35  28  24  87
 Czechoslovakia     16  49  49  45  143 16  2   8   15  25  32  60  57  51  168
 Denmark    27  45  74  75  194 14  0   1   0   1   41  75  75  45  195
 Djibouti   8   0   0   1   1   0   0   0   0   0   8   1   0   0   1
 Dominican Republic     14  3   2   2   7   0   0   0   0   0   14  2   2   3   7
 Ecuador    14  1   1   0   2   1   0   0   0   0   15  0   1   1   2
 Egypt  22  7   10  15  32  1   0   0   0   0   23  15  10  7   32
 Eritrea    5   0   0   1   1   1   0   0   0   0   6   1   0   0   1
 Estonia    12  9   9   16  34  10  4   2   1   7   22  17  11  13  41
 Ethiopia   13  22  11  21  54  2   0   0   0   0   15  21  11  22  54
 Fiji   14  1   0   0   1   3   0   0   0   0   17  0   0   1   1
 Finland    25  101 85  117 303 23  43  63  61  167 48  178 148 144 470
 France     28  212 241 263 716 23  36  35  53  124 51  316 276 248 840
 Gabon  10  0   1   0   1   0   0   0   0   0   10  0   1   0   1
 Georgia    6   8   7   17  32  7   0   0   0   0   13  17  7   8   32
 Germany    16  191 194 230 615 12  92  88  60  240 28  290 282 283 855
 UnitedTeamofGermany    3   28  54  36  118 3   8   6   5   19  6   41  60  36  137
 East Germany   5   153 129 127 409 6   39  36  35  110 11  162 165 192 519
 West Germany   5   56  67  81  204 6   11  15  13  39  11  94  82  67  243
 Ghana  14  0   1   3   4   2   0   0   0   0   16  3   1   0   4
 Great Britain  28  263 295 293 851 23  11  4   17  32  51  310 299 274 883
 Greece     28  33  43  40  116 19  0   0   0   0   47  40  43  33  116
 Grenada    9   1   1   0   2   0   0   0   0   0   9   0   1   1   2
 Guatemala  14  0   1   0   1   1   0   0   0   0   15  0   1   0   1
 Guyana     17  0   0   1   1   0   0   0   0   0   17  1   0   0   1
 Haiti  15  0   1   1   2   0   0   0   0   0   15  1   1   0   2
 Hong Kong  16  1   1   1   3   5   0   0   0   0   21  1   1   1   3
 Hungary    26  175 147 169 491 23  1   2   4   7   49  173 149 176 498
 Iceland    20  0   2   2   4   18  0   0   0   0   38  2   2   0   4
 India  24  9   7   12  28  10  0   0   0   0   34  12  7   9   28
 Indonesia  15  7   13  12  32  0   0   0   0   0   15  12  13  7   32
 Iran   16  21  21  27  69  11  0   0   0   0   27  27  21  21  69
 Iraq   14  0   0   1   1   0   0   0   0   0   14  1   0   0   1
 Ireland    21  9   10  12  31  7   0   0   0   0   28  12  10  9   31
 Israel 16  1   1   7   9   7   0   0   0   0   23  7   1   1   9
 Italy  27  206 178 193 577 23  40  36  48  124 50  241 214 246 701
 Jamaica    17  22  35  21  78  8   0   0   0   0   25  21  35  22  78
 Japan  22  142 136 161 439 21  14  22  22  58  43  183 158 156 497
 Jordan 10  1   0   0   1   0   0   0   0   0   10  0   0   1   1
 Kazakhstan     6   15  21  28  64  7   1   3   4   8   13  32  24  16  72
 Kenya  14  31  38  34  103 4   0   0   0   0   18  34  38  31  103
 Kosovo     1   1   0   0   1   1   0   0   0   0   2   0   0   1   1
 North Korea    10  16  16  22  54  9   0   1   1   2   19  23  17  16  56
 South Korea    17  90  87  90  267 18  31  25  14  70  35  104 112 121 337
 Kuwait     12  0   0   2   2   0   0   0   0   0   12  2   0   0   2
 Kyrgyzstan     6   0   1   3   4   7   0   0   0   0   13  3   1   0   4
 Latvia     11  3   11  5   19  11  1   3   5   9   22  10  14  4   28
 Lebanon    17  0   2   2   4   17  0   0   0   0   34  2   2   0   4
 Liechtenstein  17  0   0   0   0   19  2   2   6   10  36  6   2   2   10
 Lithuania  9   6   6   13  25  9   0   0   0   0   18  13  6   6   25
 Luxembourg 23  1   1   0   2   9   0   2   0   2   32  0   3   1   4
 Malaysia   13  0   7   4   11  1   0   0   0   0   14  4   7   0   11
 Mauritius  9   0   0   1   1   0   0   0   0   0   9   1   0   0   1
 Mexico     23  13  24  32  69  9   0   0   0   0   32  32  24  13  69
 Moldova    6   0   2   3   5   7   0   0   0   0   13  3   2   0   5
 Mongolia   13  2   10  14  26  14  0   0   0   0   27  14  10  2   26
 Montenegro 3   0   1   0   1   3   0   0   0   0   6   0   1   0   1
 Morocco    14  6   5   12  23  7   0   0   0   0   21  12  5   6   23
 Mozambique     10  1   0   1   2   0   0   0   0   0   10  1   0   1   2
 Namibia    7   0   4   0   4   0   0   0   0   0   7   0   4   0   4
 Netherlands    26  85  92  108 285 21  45  44  41  130 47  149 136 130 415
 Netherlands Antilles   13  0   1   0   1   2   0   0   0   0   15  0   1   0   1
 New Zealand    23  46  27  44  117 16  0   1   2   3   39  46  28  46  120
 Niger  12  0   1   1   2   0   0   0   0   0   12  1   1   0   2
 Nigeria    16  3   10  12  25  1   0   0   0   0   17  12  10  3   25
North Macedonia 6   0   0   1   1   6   0   0   0   0   12  1   0   0   1
 Norway     25  56  49  47  152 23  132 125 111 368 48  158 174 188 520
 Pakistan   17  3   3   4   10  3   0   0   0   0   20  4   3   3   10
 Panama     17  1   0   2   3   0   0   0   0   0   17  2   0   1   3
 Paraguay   12  0   1   0   1   1   0   0   0   0   13  0   1   0   1
 Peru   18  1   3   0   4   2   0   0   0   0   20  0   3   1   4
 Philippines    21  0   3   7   10  5   0   0   0   0   26  7   3   0   10
 Poland     21  68  84  132 284 23  7   7   8   22  44  140 91  75  306
 Portugal   24  4   8   12  24  8   0   0   0   0   32  12  8   4   24
 Puerto Rico    18  1   2   6   9   7   0   0   0   0   25  6   2   1   9
 Qatar  9   0   1   4   5   0   0   0   0   0   9   4   1   0   5
 Romania    21  89  95  122 306 21  0   0   1   1   42  123 95  89  307
 Russia     6   149 125 152 426 6   47  38  35  120 12  187 163 196 546
 Russian Empire     3   1   4   3   8   0   0   0   0   0   3   3   4   1   8
 Soviet Union   9   395 319 296 1,010   9   78  57  59  194 18  355 376 473 1204
 Unified Team   1   45  38  29  112 1   9   6   8   23  2   37  44  54  135
 Russia     0   0   0   0   0   1   2   6   9   17  1   9   6   2   17
 Saudi Arabia   11  0   1   2   3   0   0   0   0   0   11  2   1   0   3
 Samoa  9   0   1   0   1   0   0   0   0   0   9   0   1   0   1
 Senegal    14  0   1   0   1   5   0   0   0   0   19  0   1   0   1
 Serbia     4   3   6   6   15  3   0   0   0   0   7   6   6   3   15
 SerbiaandMontenegro    1   0   2   0   2   1   0   0   0   0   2   0   2   0   2
 Singapore  16  1   2   2   5   1   0   0   0   0   17  2   2   1   5
 Slovakia   6   9   12  7   28  7   3   4   1   8   13  8   16  12  36
 Slovenia   7   5   8   10  23  8   2   5   10  17  15  20  13  7   40
 South Africa   19  26  31  29  86  7   0   0   0   0   26  29  31  26  86
 Spain  23  45  64  41  150 20  1   0   3   4   43  44  64  46  154
 Sri Lanka  17  0   2   0   2   0   0   0   0   0   17  0   2   0   2
 Sudan  12  0   1   0   1   0   0   0   0   0   12  0   1   0   1
 Suriname   12  1   0   1   2   0   0   0   0   0   12  1   0   1   2
 Sweden     27  145 170 179 494 23  57  46  55  158 50  234 216 202 652
 Switzerland    28  50  75  67  192 23  56  45  52  153 51  119 120 106 345
 Syria  13  1   1   1   3   0   0   0   0   0   13  1   1   1   3
 Chinese Taipei     14  5   7   12  24  12  0   0   0   0   26  12  7   5   24
 Tajikistan     6   1   1   2   4   4   0   0   0   0   10  2   1   1   4
 Tanzania   13  0   2   0   2   0   0   0   0   0   13  0   2   0   2
 Thailand   16  9   8   16  33  4   0   0   0   0   20  16  8   9   33
 Togo   10  0   0   1   1   2   0   0   0   0   12  1   0   0   1
 Tonga  9   0   1   0   1   2   0   0   0   0   11  0   1   0   1
 TrinidadandTobago  17  3   5   11  19  3   0   0   0   0   20  11  5   3   19
 Tunisia    14  4   2   7   13  0   0   0   0   0   14  7   2   4   13
 Turkey     22  39  24  28  91  17  0   0   0   0   39  28  24  39  91
 Uganda     15  2   3   2   7   0   0   0   0   0   15  2   3   2   7
 Ukraine    6   34  30  56  120 7   3   1   4   8   13  60  31  37  128
 UnitedArabEmirates 9   1   0   1   2   0   0   0   0   0   9   1   0   1   2
 United States  27  1,022   795 706 2,523   23  105 112 88  305 50  794 907 1127    2828
 Uruguay    21  2   2   6   10  1   0   0   0   0   22  6   2   2   10
 Uzbekistan     6   7   6   18  31  7   1   0   0   1   13  18  6   8   32
 Venezuela  18  2   4   9   15  4   0   0   0   0   22  9   4   2   15
 Vietnam    15  1   3   1   5   0   0   0   0   0   15  1   3   1   5
 Virgin Islands     12  0   1   0   1   7   0   0   0   0   19  0   1   0   1
 Yugoslavia     18  28  31  31  90  16  0   3   1   4   34  32  34  28  94
 Zambia     13  0   1   1   2   0   0   0   0   0   13  1   1   0   2
 Zimbabwe   13  3   4   1   1   0   0   0   0   14  1   4   3   8   

图像以这种方式出现是因为查询仅检索国家/地区列表,而不是它们的奖牌。 您需要的是以下查询 -

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def hist():
    df=pd.read_csv('C:\\Users\Bhuwan Bhatt\Desktop\IP PROJECT\Book1.csv',encoding='ISO-8859-1')
    df=df.sort_values('TotalMedal',ascending=False)
    df1=df.head(n=10)
    df1 = df1.loc[:,["Country","TotalMedal"]]
    result = df1.groupby("Country")["TotalMedal"].sum() # (or use mean if you want)
    plt.bar(result.index.tolist(),result.values.tolist())
    # Your formatting goes there
    plt.xticks(rotation=20)
    plt.title('HISTOGRAM   COUNTRY V/S MEDALS',color='hotpink')
    plt.xlabel('Country~~~~>',color='navy')
    plt.ylabel('No. of Medals~~~~>',color='navy')
    plt.show()
hist()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM