2

I am generating a sorted bar chart from a very small dataframe. 54 rows.

Everything works except I noticed my xtick labels were not centered under the bars in the chart.

Upon further inspection, my bar chart is generating 55 columns.

There are only 54 values.

The lowest value is: 11720.226825 for IDN.

Where is that left most column coming from?

          count         mean          std          min          25%          50%          75%           max
TIME  54.000000  2016.000000     0.000000  2016.000000  2016.000000  2016.000000  2016.000000   2016.000000
Value 54.000000 36964.931286 16417.626336 11720.226825 25605.549959 36949.610508 46129.782953 102019.458319 

33      EST       GDP     TOT  USD_CAP         A  2016  29740.839398        NaN
34      IDN       GDP     TOT  USD_CAP         A  2016  11720.226825        NaN
35      ISR       GDP     TOT  USD_CAP         A  2016  37269.565838        NaN

Full code to generate the bar chart:

x = df1.sort_values('Value', ascending=True)
names = x['LOCATION'].to_list()
fig, axe = plt.subplots(figsize=(16, 8), facecolor='0.9')
axe.set_facecolor("lightgrey")
x.plot(kind='bar', width=2, color='darkblue', edgecolor='white', ax=axe, legend=False)
plt.title("Gross Domestic Product (GDP), Total, US Dollars/Capita, 2016", size=16, color='black', loc='left')
plt.yticks([0, 5000, 10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000, 50000,
            55000, 60000, 65000, 70000, 75000, 80000, 85000, 90000, 95000, 100000],
           ["0", "", "10K", "", "20K", "", "30K", "", "40K", "", "50K",
            "", "60K", "", "70K", "", "80K", "", "90K", "", "100K"])
plt.xticks([i for i in range(len(names))], names, rotation=90)
axe.set_axisbelow(True)
plt.grid(color='white')
plt.show()

enter image description here

The data:

   LOCATION INDICATOR SUBJECT  MEASURE FREQUENCY  TIME         Value Flag Codes
0       AUS       GDP     TOT  USD_CAP         A  2016  48021.877366        NaN
1       AUT       GDP     TOT  USD_CAP         A  2016  50503.328406        NaN
2       BEL       GDP     TOT  USD_CAP         A  2016  46566.544596        NaN
3       CAN       GDP     TOT  USD_CAP         A  2016  44819.498023        NaN
4       CZE       GDP     TOT  USD_CAP         A  2016  34714.434668        NaN
5       DNK       GDP     TOT  USD_CAP         A  2016  49020.543891        NaN
6       FIN       GDP     TOT  USD_CAP         A  2016  43446.124099        NaN
7       FRA       GDP     TOT  USD_CAP         A  2016  41357.835264          P
8       DEU       GDP     TOT  USD_CAP         A  2016  48942.907149        NaN
9       GRC       GDP     TOT  USD_CAP         A  2016  26764.908341          P
10      HUN       GDP     TOT  USD_CAP         A  2016  26700.756023        NaN
11      ISL       GDP     TOT  USD_CAP         A  2016  50751.583533        NaN
12      IRL       GDP     TOT  USD_CAP         A  2016  70879.955583        NaN
13      ITA       GDP     TOT  USD_CAP         A  2016  38380.162379        NaN
14      JPN       GDP     TOT  USD_CAP         A  2016  42292.681306        NaN
15      KOR       GDP     TOT  USD_CAP         A  2016  36629.655177        NaN
16      LUX       GDP     TOT  USD_CAP         A  2016 102019.458319        NaN
17      MEX       GDP     TOT  USD_CAP         A  2016  18535.166890          E
18      NLD       GDP     TOT  USD_CAP         A  2016  50961.235694          P
19      NZL       GDP     TOT  USD_CAP         A  2016  39241.025516          E
20      NOR       GDP     TOT  USD_CAP         A  2016  58791.758233        NaN
21      POL       GDP     TOT  USD_CAP         A  2016  27055.192120        NaN
22      PRT       GDP     TOT  USD_CAP         A  2016  30658.466294          P
23      SVK       GDP     TOT  USD_CAP         A  2016  30460.320190        NaN
24      ESP       GDP     TOT  USD_CAP         A  2016  36331.525570          P
25      SWE       GDP     TOT  USD_CAP         A  2016  48853.279783        NaN
26      CHE       GDP     TOT  USD_CAP         A  2016  63888.708682          P
27      TUR       GDP     TOT  USD_CAP         A  2016  25655.471775          E
28      GBR       GDP     TOT  USD_CAP         A  2016  42757.048917        NaN
29      USA       GDP     TOT  USD_CAP         A  2016  57591.197652        NaN
30      CHL       GDP     TOT  USD_CAP         A  2016  23003.911170        NaN
31      CHN       GDP     TOT  USD_CAP         A  2016  15477.980833        NaN
32      COL       GDP     TOT  USD_CAP         A  2016  14011.612739        NaN
33      EST       GDP     TOT  USD_CAP         A  2016  29740.839398        NaN
34      IDN       GDP     TOT  USD_CAP         A  2016  11720.226825        NaN
35      ISR       GDP     TOT  USD_CAP         A  2016  37269.565838        NaN
36      RUS       GDP     TOT  USD_CAP         A  2016  24788.679645        NaN
37      SVN       GDP     TOT  USD_CAP         A  2016  32729.606427        NaN
38      ZAF       GDP     TOT  USD_CAP         A  2016  13334.987018        NaN
39     EU28       GDP     TOT  USD_CAP         A  2016  39652.936403        NaN
40     OECD       GDP     TOT  USD_CAP         A  2016  42151.375096          E
41    OECDE       GDP     TOT  USD_CAP         A  2016  39216.988089          E
42      LVA       GDP     TOT  USD_CAP         A  2016  25588.909354        NaN
43      BRA       GDP     TOT  USD_CAP         A  2016  15224.547440          E
44      LTU       GDP     TOT  USD_CAP         A  2016  29862.344559        NaN
45      SAU       GDP     TOT  USD_CAP         A  2016  54304.320339        NaN
46     EA19       GDP     TOT  USD_CAP         A  2016  42151.339298        NaN
47      ARG       GDP     TOT  USD_CAP         A  2016  20382.379902        NaN
48      CRI       GDP     TOT  USD_CAP         A  2016  16381.382700        NaN
49      BGR       GDP     TOT  USD_CAP         A  2016  19242.642129        NaN
50      HRV       GDP     TOT  USD_CAP         A  2016  23859.838656        NaN
51      CYP       GDP     TOT  USD_CAP         A  2016  32882.262558          P
52      MLT       GDP     TOT  USD_CAP         A  2016  37337.621659        NaN
53      ROU       GDP     TOT  USD_CAP         A  2016  23197.339903          P 
MarkS
  • 1,455
  • 2
  • 21
  • 36

1 Answers1

0
  • width seems to be to large. Don't go over width=1
  • To more succinctly format the y-axis major ticks, use this answer, and use this answer to add the minor ticks.
import matplotlib.ticker as tkr
import pandas as pd

data = {'INDICATOR': ['AUS', 'AUT', 'BEL', 'CAN', 'CZE', 'DNK', 'FIN', 'FRA', 'DEU', 'GRC', 'HUN', 'ISL', 'IRL', 'ITA', 'JPN', 'KOR', 'LUX', 'MEX', 'NLD', 'NZL', 'NOR', 'POL', 'PRT', 'SVK', 'ESP', 'SWE', 'CHE', 'TUR', 'GBR', 'USA', 'CHL', 'CHN', 'COL', 'EST', 'IDN', 'ISR', 'RUS', 'SVN', 'ZAF', 'EU28', 'OECD', 'OECDE', 'LVA', 'BRA', 'LTU', 'SAU', 'EA19', 'ARG', 'CRI', 'BGR', 'HRV', 'CYP', 'MLT', 'ROU'], 'Flag': [48021.877366, 50503.328406, 46566.544596, 44819.498023, 34714.434668, 49020.543891, 43446.124099, 41357.835264, 48942.907149, 26764.908341, 26700.756023, 50751.583533, 70879.955583, 38380.162379, 42292.681306, 36629.655177, 102019.458319, 18535.16689, 50961.235694, 39241.025516, 58791.758233, 27055.19212, 30658.466294, 30460.32019, 36331.52557, 48853.279783, 63888.708682, 25655.471775, 42757.048917, 57591.197652, 23003.91117, 15477.980833, 14011.612739, 29740.839398, 11720.226825, 37269.565838, 24788.679645, 32729.606427, 13334.987018, 39652.936403, 42151.375096, 39216.988089, 25588.909354, 15224.54744, 29862.344559, 54304.320339, 42151.339298, 20382.379902, 16381.3827, 19242.642129, 23859.838656, 32882.262558, 37337.621659, 23197.339903]}
df = pd.DataFrame(data)

x = df.sort_values('Flag', ascending=True)

ax = x.plot(x='INDICATOR', y='Flag', kind='bar', color='darkblue', width=1, edgecolor='white', legend=False, figsize=(16, 8))
ax.set_title("Gross Domestic Product (GDP), Total, US Dollars/Capita, 2016", size=16, color='black', loc='left')

# format the y-axis major ticks
ax.set_yticks(range(0, 100001, 10000))
ax.yaxis.set_major_formatter(tkr.EngFormatter())

# add a y-axis minor ticks
ax.yaxis.set_minor_locator(tkr.AutoMinorLocator(n=2))

# format the plot face
ax.set_facecolor("lightgrey")
ax.set_axisbelow(True)

ax.grid(which='both', color='white')

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158