簡體   English   中英

使用matplotlib在python中繪制漂亮的條形圖

[英]Pretty plot bar chart in python with matplotlib

我在下面編寫了一個python代碼來為數據繪制條形圖。 我調整了參數,但未能使其美觀(請參見所附圖片)。

python代碼如下所示:

def plotElapsedDis(axis, jvm1, jvm2, ylabel, title, name):
    import matplotlib.pyplot as plt
    import numpy as np
    #fig, ax = plt.subplots(111)
    fig = plt.figure()
    ax = fig.add_subplot(111)
        ## the data
    N = len(jvm1)
    #menMeans = [18, 35, 30, 35, 27]
        #womenMeans = [25, 32, 34, 20, 25]
    ind = np.arange(N)+1
    width = 0.25                      # the width of the bars

    rects1 = ax.bar(ind-width, jvm1, width)

    rects2 = ax.bar(ind, jvm2, width, color='r')

    ax.set_ylabel(ylabel)
    ax.set_title(title)
    plt.xticks(ind , axis, rotation=-90)
    ax.legend( (rects1[0], rects2[0]), ('Originl', 'Optimal') )
    plt.savefig(name)
    plt.close()

plotElapsedDis(keys, y_jvm1, y_jvm2, 'seconds', 'CPU Elapsed', '../tmp/cpu_elapsed.jpg')

plotElapsedDis的前三個列表是:

keys= [u'mergesort_hongli', u'so_object', u'gc_mb', u'socket_transfer_1mb', u'app_factorial', u'string_concat', u'printff', u'so_lists', u'so_lists_small', u'word_anagrams', u'fasta', u'count_multithreaded', u'app_mandelbrot', u'primes', u'nbody', u'app_fib', u'socket_transfer_1mb_noblock', u'nsieve_bits', u'gc_string', u'simple_server', u'gc_array', u'cal', u'spectral_norm', u'app_pentomino', u'so_sieve', u'eval', u'so_matrix', u'mbari_bogus1', u'fractal', u'simple_connect', u'partial_sums', u'pi', u'so_array', u'count_shared_thread', u'fiber_ring', u'list', u'binary_trees', u'app_tarai', u'monte_carlo_pi', u'observ', u'write_large'] 
 y_jvm1= [20.703852000000001, 173.12867899999998, 74.149726000000001, 15.717608999999999, 26.226012000000001, 136.44825599999999, 46.775888000000002, 63.851292000000001, 13.929881, 71.078192999999999, 66.729854000000003, 92.045006000000001, 55.671535999999996, 24.082338, 46.349951999999995, 38.166196999999997, 15.777601000000001, 123.075288, 161.76140800000002, 12.053167, 60.597787000000004, 43.662361000000004, 45.789037999999998, 209.30117999999999, 32.190105000000003, 48.988551000000001, 55.191608000000002, 52.242056999999996, 89.343417000000002, 12.721064999999999, 109.08541600000001, 24.236315000000001, 19.817986000000001, 226.82451600000002, 100.985647, 60.686772999999995, 55.589548000000001, 69.965362999999996, 35.801557000000003, 25.728088, 16.169540999999999] 
 y_jvm2= [19.938967999999999, 178.796818, 67.512734999999992, 15.787599, 26.058038, 137.27913000000001, 12.535093, 59.649929999999998, 13.865891000000001, 60.618783000000001, 68.384602999999998, 283.39391599999999, 56.349432, 24.923209999999997, 44.113292999999999, 40.564831999999996, 12.393115, 120.76664, 152.30684499999998, 12.195145, 64.276227000000006, 18.565175999999997, 48.006701, 212.65967000000001, 32.544051000000003, 49.798428000000001, 58.516103000000001, 17.243377000000002, 92.973864999999989, 12.519096000000001, 111.39406500000001, 27.048887000000001, 20.014955999999998, 280.62933700000002, 86.977775999999992, 61.553642000000004, 50.455328000000002, 70.610264999999998, 28.390682999999999, 28.378685000000001, 17.351361000000001]

這個生成的圖片的問題 生成的圖片 以上是:

  1. x-aix的標簽太長,已被截斷(超出圖形邊框)。
  2. 將條形圖按其他顏色而不是顏色排列。 由於圖片將被打印,因此按顏色區分將無效。 如何填寫一組不同的樣式(例如,在最后一棒的棒 )。

如果有人可以幫助調整此圖片的外觀,我將不勝感激。 謝謝!

我認為您可以稍微整理一下名稱,這會有所幫助。 完成后,您可以將旋轉角度更改為45 ,這樣看起來會更好。

您可以通過將plt.xticks(ind , axis, rotation=90)更改為plt.xticks("range", "custom label list", rotation=90)來做到這一點。

def plotElapsedDis(axis, jvm1, jvm2, ylabel, title, name):
    import matplotlib.pyplot as plt
    import numpy as np
    #fig, ax = plt.subplots(111)
    fig = plt.figure()
    ax = fig.add_subplot(111)
        ## the data
    N = len(jvm1)
    #menMeans = [18, 35, 30, 35, 27]
        #womenMeans = [25, 32, 34, 20, 25]
    ind = np.arange(N)+1
    width = 0.25                      # the width of the bars

    # add "hatch"
    rects1 = ax.bar(ind-width, jvm1, width, color='white', edgecolor='black', hatch="*")

    rects2 = ax.bar(ind, jvm2, width, color='white', edgecolor='black', hatch='//')

    ax.set_ylabel(ylabel)
    ax.set_title(title)
    plt.xticks(ind , axis, rotation=90)
    ax.legend( (rects1[0], rects2[0]), ('Originl', 'Optimal') )
    fig.tight_layout() # make sure it fits
    plt.show()

plotElapsedDis(keys, y_jvm1, y_jvm2, 'seconds', 'CPU Elapsed', '../tmp/cpu_elapsed.jpg')

在此處輸入圖片說明

暫無
暫無

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

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