python - Improve ticking and grid using matplotlib -
i have following code:
import datetime matplotlib.ticker import formatstrformatter pylab import * hits=array([100,250,130,290]) misses=array([13,18,105,15]) x = np.arange(len(hits)) base=datetime.date(2014, 8, 1) date_list=array([base + datetime.timedelta(days=x) x in range(0,len(hits))]) fig,ax = plt.subplots(1,1,1,figsize=(15,10)) bar_handles=[] in range(len(hits)): bar_handles.append( ax.barh( -x[i],hits[i],facecolor='#89e07e', edgecolor='white', align='center',label="impressions")) bar_handles.append( ax.barh(-x[i],-misses[i],facecolor='#f03255', edgecolor='white', align='center',label="misses")) in range(len(bar_handles)): patch = bar_handles[i].get_children()[0] bl = patch.get_xy() percent_x = 0.5*patch.get_width() + bl[0] percent_y = 0.5*patch.get_height() + bl[1] percentage=0 if i%2==0: j=i/2 percentage = 100*(float(hits[j])/float(hits[j]+misses[j])) else: j=(i-1)/2 percentage = 100*(float(misses[j])/float(hits[j]+misses[j])) ax.text(percent_x,percent_y,"%d%%" % percentage,ha='center',va='center') in range(len(hits)): plt.yticks(-x,date_list) plt.tick_params(which='both', width=0) max_hits_num=round(np.amax(hits),-2) max_miss_num=round(np.amax(misses),-2) xticks=np.arange(-max_miss_num,max_hits_num,50) minorlocator = fixedlocator(xticks) majorlocator = fixedlocator([0]) ax.xaxis.set_major_locator(majorlocator) ax.xaxis.set_minor_locator(minorlocator) ax.xaxis.set_minor_formatter(formatstrformatter('%d')) ax.yaxis.grid(false) ax.xaxis.grid(b=true,which='minor', color='0.5', linestyle='-',linewidth=1) ax.xaxis.grid(b=true,which='major', color='b', linestyle='-',linewidth=2.5) # ax2 = plt.twinx() # ax2.grid(false) # in range(len(hits)): # plt.yticks(-x,hits+misses) plt.show()
this generates following image:
i left 1 big issue , 2 minor problems. big issue want add on right y-axis sums of values. add 113,268,235 , 305. trying along lines of twinx
or share
subplots
did not work out me.
the minor issues are:
- on x-axis, values left of
0
should without minus sign. if closely, see the blue major vertical grid line coincides gray minor one. nice have blue one.can solved first finding index of0
inxticks
usingnumpy.where
, removing element usingnumpy.delete
.
Comments
Post a Comment