Chr*_*ser 8 python label matplotlib bar-chart
我正在尝试为许多国家做一个条形图,我希望名字显示在条形图下方有点旋转.问题是标签之间的空间是不规则的.

这是相关的代码:
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 )
Run Code Online (Sandbox Code Playgroud)
有谁知道解决方案?
谢谢!您的帮助.
基督教
Bon*_*fum 19
我认为问题是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)
Run Code Online (Sandbox Code Playgroud)
并产生一个条形图,其标签均匀分布并旋转:

(你的例子没有给出关于颜色含义的暗示,因此这里省略了,但无论如何这对问题似乎并不重要).
| 归档时间: |
|
| 查看次数: |
17361 次 |
| 最近记录: |