如何在matplotlib中调整自动标记功能,使其正确显示负值?

时间:2017-07-18 22:09:47

标签: python matplotlib

过去几天我一直在玩Python,发现很多关于标签的好resources,但是我没有正确显示负值。由于autolabel()函数采用条形的高度(似乎始终为正值),因此标签在图形中向上显示,当然不会显示为负值。我可以以某种方式获得组成这些条形的值,或者如何将这些标签放在它们所属的位置并将它们显示为负数?

import pandas as pd
import matplotlib.pyplot as plt
from builtins import list
import matplotlib
matplotlib.style.use('ggplot')
import numpy as np

n_groups = 2

# create plot
fig, ax = plt.subplots()
fig.canvas.set_window_title('Mindestlohn Bundesweit')
index = np.arange(n_groups)
bar_width = 0.20
opacity = 0.8

list_reallohn_week_vollzeit = [-8.159698443426123, 11.395025597733763]
list_reallohn_week_teilzeit = [-1.048913873322391, 28.99318154295449]
list_reallohn_week_mini = [-7.552596893170488, 7.959096278017519]


rects1 = plt.bar(index + 0.00, list_reallohn_week_vollzeit, bar_width,
             alpha=opacity,
             color='b',
             label='Vollzeit')
rects2 = plt.bar(index + bar_width, list_reallohn_week_teilzeit, bar_width,
             alpha=opacity,
             color='g',
             label='Teilzeit')
rects3 = plt.bar(index + bar_width * 2,list_reallohn_week_mini, bar_width,
             alpha = opacity,
             color='c',
             label='Mini Job')

label_week_lists = ('2014 vor MdL', '2015 Nicht MdL berechtigt', '2015 mit MdL')

plt.ylabel('EUR')
plt.title('Reallöhne pro Woche')
plt.xticks(index + bar_width, label_week_lists)
plt.legend(bbox_to_anchor=(1, 1),
       bbox_transform=plt.gcf().transFigure)

def autolabel(rects, ax):
# Get y-axis height to calculate label position from.
    (y_bottom, y_top) = ax.get_ylim()
    y_height = y_top - y_bottom

    for rect in rects:
        height = rect.get_height()
        # Fraction of axis height taken up by this rectangle
        p_height = (height / y_height)
        # If we can fit the label above the column, do that;
        # otherwise, put it inside the column.
        if p_height > 0.95: # arbitrary; 95% looked good to me.
            label_position = height - (y_height * 0.05)
        else:
            label_position = height + (y_height * 0.01)

        ax.text(rect.get_x() + rect.get_width() / 2., label_position,
            '%d' % int(height),
            ha='center', va='bottom')

autolabel(rects1, ax)
autolabel(rects2, ax)
autolabel(rects3, ax)

plt.show()

enter image description here

2 个答案:

答案 0 :(得分:3)

如果您打印rect.get_height()值,您会得到类似的内容:

-8.159698443426123
11.395025597733763
-1.048913873322391
28.99318154295449
-7.552596893170488
7.959096278017519

因此,酒吧的高度可能是负数。

要标记负条,请修改自动标签功能中的if语句如下:

if p_height > 0.95: # arbitrary; 95% looked good to me.
    label_position = height - (y_height * 0.05) if (height > 0) else height + (y_height * 0.05) 
else:
    label_position = height + (y_height * 0.01) if (height > 0) else height - (y_height * 0.05) 

您必须手动调整else分支(0.05)中的系数,因为下面标签的位置取决于标签的字体大小。 (正条的标签位置不受字体大小的影响,因为标签位于条形图上方。)

我的字体设置结局:

enter image description here

答案 1 :(得分:3)

mathplotlib在该部门的记录不是很好。尝试使用dir()功能显示您正在处理的容器上的可用选项。我发现有一个.get_y()函数可以在这种情况下返回负数

尝试使用此代码

import pandas as pd
import matplotlib.pyplot as plt
from builtins import list
import matplotlib
matplotlib.style.use('ggplot')
import numpy as np

n_groups = 2

# create plot
fig, ax = plt.subplots()
fig.canvas.set_window_title('Mindestlohn Bundesweit')

index = np.arange(n_groups)
bar_width = 0.20
opacity = 0.8

list_reallohn_week_vollzeit = [-8.159698443426123, 11.395025597733763]
list_reallohn_week_teilzeit = [-1.048913873322391, 28.99318154295449]
list_reallohn_week_mini = [-7.552596893170488, 7.959096278017519]


rects1 = plt.bar(index + 0.00, list_reallohn_week_vollzeit, bar_width,
             alpha=opacity,
             color='b',
             label='Vollzeit')
rects2 = plt.bar(index + bar_width, list_reallohn_week_teilzeit, bar_width,
             alpha=opacity,
             color='g',
             label='Teilzeit')
rects3 = plt.bar(index + bar_width * 2,list_reallohn_week_mini, bar_width,
             alpha = opacity,
             color='c',
             label='Mini Job')

label_week_lists = ('2015 Nicht MdL berechtigt', '2015 mit MdL')

plt.ylabel('EUR')
plt.title('Reallöhne pro Woche')
plt.xticks(index + bar_width, label_week_lists)
plt.legend(bbox_to_anchor=(1, 1),
       bbox_transform=plt.gcf().transFigure)

def autolabel(rects, ax):
    # Get y-axis height to calculate label position from.
    (y_bottom, y_top) = ax.get_ylim()
    y_height = y_top - y_bottom

    for rect in rects:
    # print(dir(rect))

    height = 0
    if rect.get_y() < 0:
        height = rect.get_y()
    else:
        height = rect.get_height()

    print(rect.get_height())
    print( str(rect.get_y()) )

    # Fraction of axis height taken up by this rectangle
    p_height = (height / y_height)
    # If we can fit the label above the column, do that;
    # otherwise, put it inside the column.
    if p_height > 0.95:  # arbitrary; 95% looked good to me.
        label_position = height - (y_height * 0.05)
    else:
        label_position = height + (y_height * 0.01)

    ax.text(rect.get_x() + rect.get_width() / 2., label_position,
            '%d' % int(height),
            ha='center', va='bottom')

autolabel(rects1, ax)
autolabel(rects2, ax)
autolabel(rects3, ax)

plt.show()