简体   繁体   中英

Remove spaces between bars in barchart

I want there to be no spaces between the bars on my chart. I searched this question before I posted, and while there are a variety of answers, none have an effect on my chart. I assume I'm doing something wrong, but I can't figure out how to make it work.

I tried both of these methods and the spaces between the bar remained unchanged:

    plt.axis('tight')

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

Here is my code:

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

我的图表

I was able to figure it a solution to this question. I add the part in bold, and that finally allowed me to adjust the size of the chart to get rid of the excess white space.

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

The aptly named variable width is what you need to modify. You can also provided colors as a future reference in case your list gets a bit long.

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

Setting the width to 1.0 is exactly no space, but it might be visually more appealing at 0.99.

Setting the xlim to scale with a buffer will allow you to zoom in or out per your preference.

压扁缩放条,情节

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