matplotlib 条形图xtick标签的位置有不规则的空格[duplicate]

ldioqlga  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(151)

此问题在此处已有答案

Aligning rotated xticklabels with their respective xticks(6个答案)
上个月关门了。
我正在尝试为许多国家做一个条形图,我希望在条形下方显示的名称有点旋转。问题是标签之间的空间是不规则的。

下面是相关代码:

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 )

有人知道解决办法吗?

ie3xauqp

ie3xauqp1#

我认为问题在于xtick标签是与文本的中心对齐的,但是当它旋转时,你关心的是它的结尾。作为一个侧记,你可以使用栏的位置来选择xtick的位置,这样可以更好地处理间隙/不均匀的间距。
这里有一个使用国家列表的网络资源的例子(如果你不信任google为我找到的任意资源,请使用你自己的资源)

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)

并产生标签均匀间隔和旋转的条形图:

(your示例没有给予颜色含义的提示,因此此处省略,但似乎对问题无关紧要)。

x1月0n1x日

  • website不再工作时提供
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']

相关问题