簡體   English   中英

刪除條形圖中條形之間的空間

[英]Remove spaces between bars in barchart

我希望圖表上的柱形之間沒有空格。 我在發布之前搜索了這個問題,盡管答案很多種,但都沒有影響到我的圖表。 我以為我做錯了什么,但是我不知道該怎么做。

我嘗試了這兩種方法,條之間的空格保持不變:

    plt.axis('tight')

    bin = np.arange(my_list)
    plt.xlim([0, bin.size])

這是我的代碼:

my_list = [2355, 2259, 683]
plt.rcdefaults()
fig, ax = plt.subplots()
width = .35

Kitchen = ("Cucumber", "Legacy", "Monkey")
y_pos = np.arange(len(Kitchen))

barlist = ax.bar(y_pos, my_list, width, align='center', ecolor='black')
barlist[0].set_color('dodgerblue')
barlist[1].set_color('orangered')
barlist[2].set_color('khaki')

def autolabel(rects):
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()/2., 1*height,
            '%d' % int(height),
            ha='center', va='bottom')

autolabel(barlist)
plt.title('My Bar Chart')
plt.xlabel("Kitchen")
plt.ylabel("Shoes")
plt.xticks(y_pos,("Cucumber", "Legacy", "Monkey",))
plt.show()

我的圖表

我能夠找到解決該問題的方法。 我以粗體添加了該部分,最終使我能夠調整圖表的大小以擺脫多余的空白。

my_list = [2355, 2259, 683]
plt.rcdefaults()
fig, ax = plt.subplots(**figsize=(3, 3.5)**)
width = .35

適當地命名的變量width是您需要修改的。 您還可以提供顏色,以備將來參考,以防您的列表過長。

    import matplotlib.pyplot as plt
    import numpy as np

    my_list = [2355, 2259, 683]
    plt.rcdefaults()
    fig, ax = plt.subplots()

    N = len(my_list)
    ind = np.arange(N)
    width = 0.99

    ## the bars                                                                                                                                                                                                        
    colors = ['dodgerblue','orangered','khaki']
    barlist = ax.bar(ind, my_list, width, color=colors)

    def autolabel(rects):
        for rect in rects:
            height = rect.get_height()
            ax.text(rect.get_x() + rect.get_width()/2., 1*height,
                '%d' % int(height),
                ha='center', va='bottom')

    autolabel(barlist)

    # axes and labels                                                                                                                                                                                                  
    xtick_marks = ["Cucumber", "Legacy", "Monkey"]
    xtick_names = ax.set_xticklabels(xtick_marks)
    ax.set_xticks(ind)
    xbuffer = 3
    ax.set_xlim(-xbuffer,len(ind)-1+xbuffer)

    plt.show()

width設置為1.0時完全沒有空格,但是在0.99時它可能在視覺上更具吸引力。

設置xlim以使用緩沖區縮放將使您可以根據自己的喜好放大或縮小。

壓扁縮放條,情節

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM