Matplotlib 获取水平条边缘顶部和底部的坐标 [英] Matplotlib get coordinates of top and bottom of horizontal bar edges

查看:46
本文介绍了Matplotlib 获取水平条边缘顶部和底部的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于此示例水平条形图:

Given this sample horizontal bar chart:

"""
Simple demo of a horizontal bar chart.
"""
import matplotlib.pyplot as plt
plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as 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 do you want to go today?')

plt.show()

如何获取顶部条的顶部边缘和底部条的底部边缘的y坐标(假设条形图实际上是一个轴,像这样:ax.barh ...).

How can I get the y coordinates of the top edge of the top bar and the bottom edge of the bottom bar (assume the bar chart is actually an axis, like this: ax.barh...).

提前致谢!

推荐答案

每个条形都是一个 Rectangle 对象:

Every bar is a Rectangle object:

br = plt.barh(y_pos, performance, xerr=error, align='center', alpha=0.4)
for 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)

输出:

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)

这篇关于Matplotlib 获取水平条边缘顶部和底部的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆