python - matplotlib的 "barh"中height变量的单位是什么?

标签 python matplotlib

在matplotlib的函数barh的定义中:

matplotlib.pyplot.barh(bottom, width, height=0.8, left=None, hold=None, **kwargs)

默认的“高度”是 0.8,但是当我绘制一些具有不同图形高度的图形时,例如 (30, 40,..) 和 dpi=100。我看到酒吧的高度改变了。它不是固定的。所以我想知道 barh 的高度单位是什么以及如何固定它(不依赖于图形的高度)。

最佳答案

我将把它分成两部分:

I wonder what is the unit of the height in barh

(显然人们一直在想这个 since 2009 ... 所以我猜你是好伙伴!)

这个问题是比较简单的部分 - 它是分配给图中条形的高度的百分比。例如,默认的 height=0.8 表示柱的高度将为 0.8 * (plot_height/n_bars)。您可以通过设置 height=1.0(甚至值 > 1,条形会重叠)来看到这一点。

如果你真的想确定,这里是 source of axes.barh .这只是调用 axes.bar - 看看 these lines :

nbars = len(bottom)
if len(left) == 1:
    left *= nbars
if len(height) == 1:
    height *= nbars

later on ...

args = zip(left, bottom, width, height, color, edgecolor, linewidth)
for l, b, w, h, c, e, lw in args:
    if h < 0:
        b += h
        h = abs(h)
    if w < 0:
        l += w
        w = abs(w)
    r = mpatches.Rectangle(
        xy=(l, b), width=w, height=h,
        facecolor=c,
        edgecolor=e,
        linewidth=lw,
        label='_nolegend_',
        margins=margins
        )
    r.update(kwargs)
    r.get_path()._interpolation_steps = 100
    #print r.get_label(), label, 'label' in kwargs
    self.add_patch(r)
    patches.append(r)

所以您看到高度按 nbars 缩放,并且当您绘制矩形时,它们按此高度隔开。

how to make it fixed

这比较难,你必须手动设置它。图表上的条形图最终是 matplotlib.patches.Rectangle 对象,它们具有宽度和高度......这也是一个百分比。我认为最好的解决方案是手动计算适当的百分比。

这是一个基于 barh demo 的简短示例:

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.figure(figsize=(5,5), dpi=80)
myplot = plt.barh(y_pos, performance, height=0.8, 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?')

for obj in myplot:
    # Let's say we want to set height of bars to always 5px..
    desired_h = 5
    current_h = obj.get_height()
    current_y = obj.get_y()
    pixel_h = obj.get_verts()[2][1] - obj.get_verts()[0][1]
    print("current position = ", current_y)
    print("current pixel height = ", pixel_h)

    # (A) Use ratio of pixels to height-units to calculate desired height
    h = desired_h / (pixel_h/current_h)
    obj.set_height(h)
    pixel_h = obj.get_verts()[2][1] - obj.get_verts()[0][1]
    print("now pixel height = ", pixel_h)

    # (B) Move the rectangle so it still aligns with labels and error bars
    y_diff = current_h - h # height is same units as y
    new_y = current_y + y_diff/2
    obj.set_y(new_y)
    print("now position = ", obj.get_y())
plt.show()

A 部分计算 pixel_h/current_h 以获得像素和高度单位之间的转换。然后我们可以将 desired_h(像素)除以该比率以获得以高度单位表示的 desired_h。这会将条形宽度设置为 5 像素,但条形底部保持在同一位置,因此它不再与标签和误差条对齐。

B 部分计算新的 y 位置。由于 yheight 的单位相同,我们只需添加一半的高度差 (y_diff) 即可获得新位置。这会使条形图以其原来的 y 位置为中心。

请注意,这仅设置初始大小。例如,如果您调整图的大小,条形图仍会缩放 - 您必须覆盖该事件才能适本地调整条形图的大小。

关于python - matplotlib的 "barh"中height变量的单位是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38793456/

相关文章:

python - Boto3查找未使用的安全组

python - 从 Windows 迁移到 Linux 时 WXPython 接口(interface)无法正常工作

python - 如何在 Bokeh 中设置默认样式?

python - seaborn jointplot 按密度颜色

python - 使用颜色热图绘制 2D 散点图

python - 删除缺失值的插值时间序列图

python - 使用 xlrd 时读取包含公式的单元格值返回 0.0

python - 如何使用 pyqt5 在模型/ View qml 中查看我的数据

python - 在 Python 中为散点图设置图例

python - 使用现有规则生成更多事件集