python - Matplotlib get coordinates of top and bottom of horizontal bar edges -
given sample horizontal bar chart:
""" simple demo of horizontal bar chart. """ import matplotlib.pyplot plt plt.rcdefaults() import numpy np import matplotlib.pyplot plt # example data people = ('tom', 'dick', 'harry', 'slim', 'jim') y_pos = np.arange(len(people)) performance = 3 + 10 * np.random.rand(len(people)) error = np.random.rand(len(people)) plt.barh(y_pos, performance, xerr=error, align='center', alpha=0.4) plt.yticks(y_pos, people) plt.xlabel('performance') plt.title('how fast want go today?') plt.show()
how can y coordinates of top edge of top bar , bottom edge of bottom bar (assume bar chart axis, this: ax.barh...).
thanks in advance!
every bar rectangle object:
br = plt.barh(y_pos, performance, xerr=error, align='center', alpha=0.4) b in br: print b w,h = b.get_width(), b.get_height() # lower left vertex x0, y0 = b.xy # lower right vertex x1, y1 = x0+w,y0 # top left vertex x2, y2 = x0,y0+h # top right vertex x3, y3 = x0+w,y0+h print (x0,y0), (x1,y1), (x2,y2), (x3,y3)
output:
rectangle(0,-0.4;10.9438x0.8) (0.0, -0.4) (10.943792845675922, -0.4) (0.0, 0.4) (10.943792845675922, 0.4) rectangle(0,0.6;3.87667x0.8) (0.0, 0.6) (3.8766706874993693, 0.6) (0.0, 1.4) (3.8766706874993693, 1.4) rectangle(0,1.6;6.13112x0.8) (0.0, 1.6) (6.131123295324526, 1.6) (0.0, 2.4000000000000004) (6.131123295324526, 2.4000000000000004) rectangle(0,2.6;10.875x0.8) (0.0, 2.6) (10.875021819863152, 2.6) (0.0, 3.4000000000000004) (10.875021819863152, 3.4000000000000004) rectangle(0,3.6;10.9706x0.8) (0.0, 3.6) (10.970580117194409, 3.6) (0.0, 4.4) (10.970580117194409, 4.4)