我正在尝试为许多国家/地区做条形图,并且我希望名称在条形下方显示出一些旋转。
问题在于标签之间的空间是不规则的。

以下是相关代码:

 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 )

有人知道解决方案吗?

谢谢!您的帮助。

基督教

最佳答案

我认为问题在于xtick标签与文本的中心对齐,但是旋转它时,您会在意它的结尾。作为旁注,您可以使用条形图的位置来选择xtick位置,以更好地处理间隙/不均匀间距。

这是一个使用网络资源列出国家列表的示例(如果您不信任google为我找到的任意资源,请使用您自己的资源)

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

# get a list of countries
website = "http://vbcity.com/cfs-filesystemfile.ashx/__key/CommunityServer.Components.PostAttachments/00.00.61.18.99/Country-List.txt"
response = urllib2.urlopen(website)
page = response.read()
many_countries = page.split('\r\n')

# 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))


plt.figure(1)
h = plt.bar(xrange(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)

并生成条形图,其标签均匀间隔并旋转:

(您的示例并未暗示颜色的含义,因此此处省略了,但是无论如何对这个问题似乎都不重要)。

10-04 11:18