Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib获取标签以显示十进制_Python_Matplotlib - Fatal编程技术网

Python matplotlib获取标签以显示十进制

Python matplotlib获取标签以显示十进制,python,matplotlib,Python,Matplotlib,所以对于我来说,我不知道如何让标签显示小数点,而不仅仅是0,1,2 我需要它们是十进制的,下面是我在Python3中的代码 #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[

所以对于我来说,我不知道如何让标签显示小数点,而不仅仅是0,1,2

我需要它们是十进制的,下面是我在Python3中的代码

    #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操作员。杰伊在下面帮助澄清。我附上我的代码和新图表,以便可以轻松复制。仍然需要一些调整,但这是个人偏好

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

Jay的解决方案对我不起作用,但它确实:

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

嘿,谢谢,我也这么想,但没有改变。更改屏幕截图和图表相同@Atropos只需检查一次,get_height()函数,可以添加到这里吗?看起来,它正在返回int。只要检查一下。。