簡體   English   中英

Matplotlib條形圖,條形圖相互重疊,如何創建空間

[英]Matplotlib bar plot, bars is on top of each other, how to create space

我具有用於繪制一些等待時間值的條形圖的此功能:

import pandas as pd
import numpy as np
%matplotlib inline
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt 


def plot_bar(g):
    auto = [1.36, 5.34, 10.2, 16.48, 24.3, 45.6, 83.89, 155.19, 289.68, 598.85]
    four = [1.81, 5.57, 11.48, 18, 27.69, 47.72, 89.11, 164.74, 315.24, 637.89]
    eight = [1.44, 5.45, 8.56, 16.64, 26.85, 43.44, 82.41, 152.32, 294.11, 598.57]
    sixteen = [2.29, 5.79, 19.99, 18.44, 33.73, 75.31, 177.74, 365.39, 774.57, 1619.99]
    thirtytwo = [3.62, 13.84, 25.39, 42.21, 80.14, 150.41, 311.46, 645.37, 1330.94, 2688.48]   

    N = 10
    fig, ax = plt.subplots()

    ind = np.arange(N)    # the x locations for the groups
    width = 0.30         # the width of the bars
    p1 = ax.bar(ind, auto, width, color='r')
    p2 = ax.bar(ind+width, four, width, color='y')
    p3 = ax.bar(ind+width+width, eight, width, color='b')
    p4 = ax.bar(ind+width+width+width, sixteen, width, color='k')
    p5 = ax.bar(ind+width+width+width+width, thirtytwo, width, color='g')



    #ax.set_title('Scores by group and gender')
    ax.set_xticks(ind * (5 * width))
    ax.set_xticklabels(('1MB', '4MB', '8MB', '16MB', '32MB', '64MB', '128MB', '256MB', '512MB', '1GB'))

    plt.xticks(rotation=75)

    ax.legend((p1[0], p2[0], p3[0], p4[0], p5[0]), ('Automatic t=8', 't=4','t=8', 't=16', 't=32'))
    ax.autoscale_view()
    plt.ylabel('time (ms)')
    plt.xlabel('Data Size')
    plt.yscale("log", nonposy='clip')
    plt.tight_layout()
    fig.savefig('./graphs/nope_{!s}.eps'.format(g))

結果是這樣的: 在此處輸入圖片說明

我想避免條形重疊的地方。 我試過身材大小,但沒有運氣。 我也嘗試過更改set_xticks以了解其set_xticks ,並且我對如何解決此問題不了解。

所提供的代碼應該可以工作,請指教。

另一種替代解決方案是對中心桿使用廣泛分開的x位置 繪圖的問題在於,條形寬度為0.3,並且有5條,因此每組5 * 0.3 = 1.5。 而且,由於居中鋼筋在x位置之間的間距為1,所以每組鋼筋之間有0.5的重疊。

為避免這種情況,可以使用以下方法在x索引之間使用2的間距來使條居中。 我還注意到您沒有正確使用所有的x-ticklabel。 添加以下兩行以使外觀看起來不錯。

ind = np.arange(0,2*N, 2)    # the x locations for the groups
ax.set_xticks(ind + 2*width)

在此處輸入圖片說明

基本上,條的寬度要寬。 xticks的寬度為1個單位,您嘗試繪制5個寬度為.3的條形圖,該條形超過1個單位,因此重疊。 將您的寬度減小到0.2以保持5條。

width = 0.20         # the width of the bars

輸出:

在此處輸入圖片說明

暫無
暫無

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

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