亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在水平堆積條形圖中顯示精確的小數

如何在水平堆積條形圖中顯示精確的小數

慕斯王 2022-06-14 10:18:23
ntal_barchart_distribution.html 上找到了創建堆疊條形圖的代碼。問題是圖中未顯示小數位。在代碼中,我發現列表中沒有小數,但如果將它們更改為小數,則不會顯示(參見下面的代碼)。如果嘗試將 str 更改為在 ax.text(x, y, str(int(c)) 行中浮動。這實際上顯示小數,但它們都是 0。非常感謝。import numpy as npimport matplotlib.pyplot as pltcategory_names = ['Strongly disagree', 'Disagree','Neither agree nor   agree', 'agree', 'Strongly agree']results = {    'Question 1': [(10), (14.99), (17.01), (32), (26)]}def survey(results, category_names):    labels = list(results.keys())    data = np.array(list(results.values()))    data_cum = data.cumsum(axis=1)    category_colors = plt.get_cmap('RdYlGn')(        np.linspace(0.15, 0.85, data.shape[1]))    fig, ax = plt.subplots(figsize=(9.2, 5))    ax.invert_yaxis()    ax.xaxis.set_visible(False)    ax.set_xlim(0, np.sum(data, axis=1).max())    for i, (colname, color) in enumerate(zip(category_names, category_colors)):        widths = data[:, i]        starts = data_cum[:, i] - widths        ax.barh(labels, widths, left=starts, height=0.5,                label=colname, color=color)        xcenters = starts + widths / 2        r, g, b, _ = color        text_color = 'white' if r * g * b < 0.5 else 'darkgrey'        for y, (x, c) in enumerate(zip(xcenters, widths)):            ax.text(x, y, str(int(c)), ha='center', va='center',                color=text_color)    ax.legend(ncol=len(category_names), bbox_to_anchor=(0, 1),              loc='lower left', fontsize='small')    return fig, axsurvey(results, category_names)
查看完整描述

1 回答

?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

編寫str(int(c))時,首先將數字轉換為整數,從而去掉小數,然后將其轉換為字符串。要獲得所需的輸出,請使用str(c),或者如果您希望能夠更好地控制數字的格式(例如選擇小數點后的位數),請替換str(int(c))為'{:.2f}'.format(c)


category_names = ['Strongly disagree', 'Disagree','Neither agree nor agree', 'agree', 'Strongly agree']


results = {

    'Question 1': [(10), (14.99), (17.01), (32), (26)]

}



def survey(results, category_names):

    labels = list(results.keys())

    data = np.array(list(results.values()))

    data_cum = data.cumsum(axis=1)

    category_colors = plt.get_cmap('RdYlGn')(

        np.linspace(0.15, 0.85, data.shape[1]))


    fig, ax = plt.subplots(figsize=(9.2, 5))

    ax.invert_yaxis()

    ax.xaxis.set_visible(False)

    ax.set_xlim(0, np.sum(data, axis=1).max())


    for i, (colname, color) in enumerate(zip(category_names, category_colors)):

        widths = data[:, i]

        starts = data_cum[:, i] - widths

        ax.barh(labels, widths, left=starts, height=0.5,

                label=colname, color=color)

        xcenters = starts + widths / 2


        r, g, b, _ = color

        text_color = 'white' if r * g * b < 0.5 else 'darkgrey'


        for y, (x, c) in enumerate(zip(xcenters, widths)):

            ax.text(x, y, '{:.2f}'.format(c), ha='center', va='center',

                color=text_color)

    ax.legend(ncol=len(category_names), bbox_to_anchor=(0, 1),

              loc='lower left', fontsize='small')


    return fig, ax



survey(results, category_names)


查看完整回答
反對 回復 2022-06-14
  • 1 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號