python - Matplotlib bar chart customisation for multiple values -


i have list of tuples countries , number of times occur. have 175 countries long names.

when chart them, get:

enter image description here

as can see, bunched up, there no space, can barely read anything.

code use (the original data file huge, contains matplotlib specific code):

def tuplecounts2percents(inputlist):      total = sum(x[1] x in inputlist)*1.0      return [(x[0], 1.*x[1]/total) x in inputlist]  def autolabel(rects,labels): # attach text labels     i,(rect,label) in enumerate(zip(rects,labels)):         height = rect.get_height()         plt.text(rect.get_x() + rect.get_width()/2., 1.05*height,             label,             ha='center', va='bottom',fontsize=6,style='italic')  def countrychartlist(inputlist,path):     seen_countries = counter()      dict in inputlist:         seen_countries += counter(dict['location-value-pair'].keys())      seen_countries = seen_countries.most_common()      seen_countries_percentage = map(itemgetter(1), tuplecounts2percents(seen_countries))     seen_countries_percentage = ['{:.2%}'.format(item)for item in seen_countries_percentage]      yvals = map(itemgetter(1), seen_countries)     xvals = map(itemgetter(0), seen_countries)      plt.figure()     countrychart = plt.bar(range(len(seen_countries)), yvals, width=0.9)     plt.xticks(range(len(seen_countries)), xvals,rotation=90)      plot_margin = 0.25     x0, x1, y0, y1 = plt.axis()     plt.axis((x0,               x1,               y0,               y1+plot_margin))      plt.title('countries in dataset')     plt.xlabel('countries in data')     plt.ylabel('occurrences')      plt.tick_params(axis='both', which='major', labelsize=6)     plt.tick_params(axis='both', which='minor', labelsize=6)     plt.tight_layout()      autolabel(countrychart,seen_countries_percentage)      plt.savefig(path)     plt.clf() 

an idea of dict feed in looks is:

    list = [     {         "location-value-pair": {             "austria": 234         }     },     {         "location-value-pair": {             "azerbaijan": 20006.0         }     },     {         "location-value-pair": {             "germany": 4231         }     },     {         "location-value-pair": {             "united states": 12121         }     },     {         "location-value-pair": {             "germany": 65445         }     },     {         "location-value-pair": {             "uk": 846744         }     } } ] 

how i:

  1. make things 1 can read them - answer histogram bins instead of bar plot? maybe stepping every 10%?
  2. how make tick labels , labels above bars (the percentages) don't overlap?
  3. any other insight welcome (e.g. bars gradient colours, red yellow)?

edit

i reduced number of countries top 50, made bars more transparent, , changed ticks rotate 45 degrees. still find first bar has tick crosses y axis unreadable. how can change this?

enter image description here

changed countrychart = plt.bar(range(len(seen_countries)), yvals, width=0.9,alpha=0.6) , rotation=45 .text() argument in autolabel function.

the problem in alignment of autolabels:

def autolabel(rects,labels): # attach text labels     i,(rect,label) in enumerate(zip(rects,labels)):         height = rect.get_height()         plt.text(rect.get_x() + rect.get_width()/2., 1.05*height,             label,             ha='center', va='bottom',fontsize=6,style='italic') 

was changed to:

def autolabel(rects,labels): # attach text labels     i,(rect,label) in enumerate(zip(rects,labels)):         height = rect.get_height()         plt.text(rect.get_x() + rect.get_width()/2., 1.05*height,             label,             ha='left', va='bottom',fontsize=6,style='italic', rotation=45) 

to get:

enter image description here


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -