简体   繁体   中英

matplotlib getting labels to show decimal

matplot图

So for the life of me i can't figure out how to get the labels to show decimal places and not just 0,1,2

i need them to be in decimal form below is my code in 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()

i am aware it is not pretty as of now and not lazy need to make sore more functions but can't seem to get past this issue. thanks for looking at.

EDIT: For those looking in the future the issue was the S operator here matplotlib documentation . Jay helped clarify below. i am attaching my code and new chart so can be copied for ease. still needs a little tweaking but that is personal preference.

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()

图表已编辑

I think problem is with below statement. Instead of int , use float

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

The solution by Jay did not work for me, but this did:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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