繁体   English   中英

matplotlib获取标签显示小数

[英]matplotlib getting labels to show decimal

matplot图

所以对于我的生活,我无法弄清楚如何让标签显示小数位而不仅仅是0,1,2

我需要它们以十进制形式下面是我在python 3中的代码

    #ROE and Growth
Tax_Burden = stock.loc['Net Income']/stock.loc['Pre-Tax Income']
Interest_Burden= stock.loc['Pre-Tax Income']/stock.loc['Operating Income']
Operating_Margin= stock.loc['Operating Income']/stock.loc['Revenue']
Asset_Turnover= stock.loc['Revenue']/stock.loc['Total Assets Average']
Leverage_Ratio= stock.loc['Total Assets Average']/stock.loc['Total Equity Average']
roe=Tax_Burden*Interest_Burden*Operating_Margin*Asset_Turnover*Leverage_Ratio
Growth = roe * (1-stock.loc['Dividend Payout Ratio'])
astart = 21
aend = 31
annual = [Operating_Margin[astart:aend],Tax_Burden[astart:aend],Interest_Burden[astart:aend],Asset_Turnover[astart:aend],Leverage_Ratio[astart:aend],roe[astart:aend],Growth[astart:aend]]



N = len(annual[0])


ind = np.arange(N)  # the x locations for the groups
width = .12       # the width of the bars

fig, ax = plt.subplots(figsize=(20,10))

rects1 = ax.bar(ind, annual[0], width, color='y')
rects2 = ax.bar(ind+width, annual[1], width, color='r')
rects3 = ax.bar(ind+width*2, annual[2], width, color='b')
rects4 = ax.bar(ind+width*3, annual[3], width, color='k')
rects5 = ax.bar(ind+width*4, annual[4], width, color='c')
rects6 = ax.bar(ind+width*5, annual[5], width, color='k')
rects7 = ax.bar(ind+width*6, annual[6], width, color='r')



# add some text for labels, title and axes ticks
ax.set_ylabel('Percentage')
ax.set_title('ROE Annual')
ax.set_xticks(ind + width / 2)
ax.set_xticklabels(list(stock.loc['Fiscal Period'][astart:aend]))

#ax.legend((rects1[0], rects2[0]), ('workinprogress'))


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)
autolabel(rects4,ax)
autolabel(rects5,ax)
autolabel(rects6,ax)
autolabel(rects7,ax)

plt.show()

我知道它现在不是很漂亮而且懒惰不需要让更多的功能疼痛,但似乎无法解决这个问题。 谢谢你的关注。

编辑:对于那些未来的问题,这里的问题是S运算符matplotlib文档 杰伊在下面帮助澄清。 我附加了我的代码和新图表,因此可以轻松复制。 仍然需要一点调整,但这是个人喜好。

astart = 21
aend = 31
annual = [Operating_Margin[astart:aend],Tax_Burden[astart:aend],Interest_Burden[astart:aend],Asset_Turnover[astart:aend],Leverage_Ratio[astart:aend],roe[astart:aend],Growth[astart:aend]]



N = len(annual[0])


ind = np.arange(N)  # the x locations for the groups
width = .12       # the width of the bars

fig, ax = plt.subplots(figsize=(20,10),facecolor='#c8f2e5')

rects1 = ax.bar(ind, annual[0], width, color='#f29ca2')
rects2 = ax.bar(ind+width, annual[1], width, color='#61eaf2')
rects3 = ax.bar(ind+width*2, annual[2], width, color='#6da4d9')
rects4 = ax.bar(ind+width*3, annual[3], width, color='#f2bb12')
rects5 = ax.bar(ind+width*4, annual[4], width, color='c')
rects6 = ax.bar(ind+width*5, annual[5], width, color='#ce44f2')
rects7 = ax.bar(ind+width*6, annual[6], width, color='r')

ax.set_facecolor('#a7cff2')

# add some text for labels, title and axes ticks
ax.set_ylabel('Percentage',size=20)
ax.set_title('ROE Annual',size=30)
ax.set_xticks(ind + width / 2)
ax.set_xticklabels(list(stock.loc['Fiscal Period'][astart:aend]),size=14)
vals = ax.get_yticks()
ax.set_yticklabels(['{:3.2f}%'.format(x*100) for x in vals])
ax.legend((rects1[0], rects2[0], rects3[0], rects4[0], rects5[0], rects6[0], rects7[0]),('Operating Margin', 'Tax Burden','Interest Burden','Asset Turnover', 'Leverage Ratio','ROE','Growth'))


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,
                '%.2f' % float(height),
                ha='center', va='bottom',color='k',fontsize=12)
#


autolabel(rects1,ax)
autolabel(rects2,ax)
autolabel(rects3,ax)
autolabel(rects4,ax)
autolabel(rects5,ax)
autolabel(rects6,ax)
autolabel(rects7,ax)

plt.show()

图表已编辑

我认为问题在于以下陈述。 而不是int ,使用float

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

周杰伦的解决方案对我不起作用,但这样做:

for p in ax.patches:
    ax.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.002))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM