Position of bar plot xtick labels have irregular spaces

Question:

I’m trying to do a bar plot for many countries and i want the names displayed a bit rotated underneath the bars.
The problem is that the spaces between the labels are irregular.

Here you can see the barplot with the country names as label

Here’s the relevant code:

 plt.bar(i, bar_height, align='center', label=country ,color=cm.jet(1.*counter/float( len(play_list))))
 xticks_pos = scipy.arange( len( country_list)) +1
 plt.xticks(xticks_pos ,country_list, rotation=45 )

Does anyone know a solution?

Asked By: Christian Kaiser

||

Answers:

I think the problem is that the xtick label is aligned to the center of the text, but when it is rotated you care about the end of it. As a side note, you can use the position of the bars to select the xtick positions which better handles gaps/uneven spacing.

Here’s an example that uses a web resource for list of countries (use your own if you don’t trust the arbitrary resource google found for me)

import requests
import numpy as np
import matplotlib.pyplot as plt 

# create sample data
# get a list of countries
website = "http://vbcity.com/cfs-filesystemfile.ashx/__key/CommunityServer.Components.PostAttachments/00.00.61.18.99/Country-List.txt"
r = requests.get(website)

many_countries = r.text.split()

# pick out a subset of them
n = 25
ind = np.random.randint(0, len(many_countries), 25) 
country_list = [many_countries[i] for i in ind]

# some random heights for each of the bars.
heights = np.random.randint(3, 12, len(country_list))

# create plot
plt.figure(1)
h = plt.bar(range(len(country_list)), heights, label=country_list)
plt.subplots_adjust(bottom=0.3)

xticks_pos = [0.65*patch.get_width() + patch.get_xy()[0] for patch in h]

_ = plt.xticks(xticks_pos, country_list,  ha='right', rotation=45)

and results in a bar chart whose labels are evenly spaced and rotated:
matplotlib bar plot with rotated labels

(your example doesn’t give a hint as to what the colors mean so that’s omitted here, but seems immaterial to the question anyway).

many_countries

  • Provided in case the website no longer works
many_countries = ['Abkhazia', 'Afghanistan', 'Akrotiri', 'and', 'Dhekelia', 'Aland', 'Albania', 'Algeria', 'American', 'Samoa', 'Andorra', 'Angola', 'Anguilla', 'Antigua', 'and', 'Barbuda', 'Argentina', 'Armenia', 'Aruba', 'Ascension', 'Island', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas,', 'The', 'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize', 'Benin', 'Bermuda', 'Bhutan', 'Bolivia', 'Bosnia', 'and', 'Herzegovina', 'Botswana', 'Brazil', 'Brunei', 'Bulgaria', 'Burkina', 'Faso', 'Burundi', 'Cambodia', 'Cameroon', 'Canada', 'Cape', 'Verde', 'Cayman', 'Islands', 'Central', 'Africa', 'Republic', 'Chad', 'Chile', 'China', 'Christmas', 'Island', 'Cocos', '(Keeling)', 'Islands', 'Colombia', 'Comoros', 'Congo', 'Cook', 'Islands', 'Costa', 'Rica', 'Cote', "d'lvoire", 'Croatia', 'Cuba', 'Cyprus', 'Czech', 'Republic', 'Denmark', 'Djibouti', 'Dominica', 'Dominican', 'Republic', 'East', 'Timor', 'Ecuador', 'Egypt', 'El', 'Salvador', 'Equatorial', 'Guinea', 'Eritrea', 'Estonia', 'Ethiopia', 'Falkland', 'Islands', 'Faroe', 'Islands', 'Fiji', 'Finland', 'France', 'French', 'Polynesia', 'Gabon', 'Cambia,', 'The', 'Georgia', 'Germany', 'Ghana', 'Gibraltar', 'Greece', 'Greenland', 'Grenada', 'Guam', 'Guatemala', 'Guemsey', 'Guinea', 'Guinea-Bissau', 'Guyana', 'Haiti', 'Honduras', 'Hong', 'Kong', 'Hungary', 'Iceland', 'India', 'Indonesia', 'Iran', 'Iraq', 'Ireland', 'Isle', 'of', 'Man', 'Israel', 'Italy', 'Jamaica', 'Japan', 'Jersey', 'Jordan', 'Kazakhstan', 'Kenya', 'Kiribati', 'Korea,', 'N', 'Korea,', 'S', 'Kosovo', 'Kuwait', 'Kyrgyzstan', 'Laos', 'Latvia', 'Lebanon', 'Lesotho', 'Liberia', 'Libya', 'Liechtenstein', 'Lithuania', 'Luxembourg', 'Macao', 'Macedonia', 'Madagascar', 'Malawi', 'Malaysia', 'Maldives', 'Mali', 'Malta', 'Marshall', 'Islands', 'Mauritania', 'Mauritius', 'Mayotte', 'Mexico', 'Micronesia', 'Moldova', 'Monaco', 'Mongolia', 'Montenegro', 'Montserrat', 'Morocco', 'Mozambique', 'Myanmar', 'Nagorno-Karabakh', 'Namibia', 'Nauru', 'Nepal', 'Netherlands', 'Netherlands', 'Antilles', 'New', 'Caledonia', 'New', 'Zealand', 'Nicaragua', 'Niger', 'Nigeria', 'Niue', 'Norfolk', 'Island', 'Northern', 'Cyprus', 'Northern', 'Mariana', 'Islands', 'Norway', 'Oman', 'Pakistan', 'Palau', 'Palestine', 'Panama', 'Papua', 'New', 'Guinea', 'Paraguay', 'Peru', 'Philippines', 'Pitcaim', 'Islands', 'Poland', 'Portugal', 'Puerto', 'Rico', 'Qatar', 'Romania', 'Russia', 'Rwanda', 'Sahrawi', 'Arab', 'Democratic', 'Republic', 'Saint-Barthelemy', 'Saint', 'Helena', 'Saint', 'Kitts', 'and', 'Nevis', 'Saint', 'Lucia', 'Saint', 'Martin', 'Saint', 'Pierre', 'and', 'Miquelon', 'Saint', 'Vincent', 'and', 'Grenadines', 'Samos', 'San', 'Marino', 'Sao', 'Tome', 'and', 'Principe', 'Saudi', 'Arabia', 'Senegal', 'Serbia', 'Seychelles', 'Sierra', 'Leone', 'Singapore', 'Slovakia', 'Slovenia', 'Solomon', 'Islands', 'Somalia', 'Somaliland', 'South', 'Africa', 'South', 'Ossetia', 'Spain', 'Sri', 'Lanka', 'Sudan', 'Suriname', 'Svalbard', 'Swaziland', 'Sweden', 'Switzerland', 'Syria', 'Tajikistan', 'Tanzania', 'Thailand', 'Togo', 'Tokelau', 'Tonga', 'Transnistria', 'Trinidad', 'and', 'Tobago', 'Tristan', 'da', 'Cunha', 'Tunisia', 'Turkey', 'Turkmenistan', 'Turks', 'and', 'Caicos', 'Islands', 'Tuvalu', 'Uganda', 'Ukraine', 'United', 'Arab', 'Emirates', 'United', 'Kingdom', 'United', 'States', 'Uruguay', 'Uzbekistan', 'Vanuatu', 'Vatican', 'City', 'Venezuela', 'Vietnam', 'Virgin', 'Islands,', 'British', 'Virgin', 'Islands,', 'U.S.', 'Wallis', 'and', 'Futuna', 'Yemen', 'Zambia', 'Zimbabwe']
Answered By: Bonlenfum