漂亮的阴谋条形图python与matplotlib

漂亮的阴谋条形图python与matplotlib

问题描述:

我写了一个python代码下面为我的数据绘制条形图。我调整了参数但未能使它变得美丽(见附图)。漂亮的阴谋条形图python与matplotlib

的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] 

与此问题产生的PIC Generated pic以上是:

  1. x-aixs的标签太长,会被截断(超出图形边界)。
  2. 用别人的颜色来区分酒吧。由于图片将被打印,因此按颜色区分将不起作用。如何填写不同风格的组(例如,figure中的最后一个栏)。

如果有人能帮助调整此照片的前景,我将不胜感激。谢谢!

+0

你不应该混合对象API('ax.legend()')与MATLAB般的API('PLT。 xticks','plt.savefig')。你可以通过调用'fig.tight_layout()'来解决你的标签问题 – MaxNoe

我会考虑清理一下名称,这应该有所帮助。一旦你这样做,你可以改变旋转到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') 

enter image description here