Matplotlib:条形图未与数据表对齐

时间:2018-07-23 08:40:34

标签: python-2.7 matplotlib

我想开发一个组条形图,我的代码如下。

import matplotlib
from PIL.ImageEnhance import Color
matplotlib.use('Agg')
import matplotlib.ticker as mtick
import numpy as np
import matplotlib.pyplot as plt

smdt =   {'OSS': (14, 16, 24, 40, 32, 36, 29, 39, 52, 31, 35, 37, 36, 36), 'GVS': (42, 44, 27, 40, 56, 48, 45, 43, 42, 44, 44, 50, 52, 50), 'ATS': (100, 17, 10, 100, 84, 100, 99, 100, 110, 79, 150, 102, 100, 0), 'POSS': (44, 42, 40, 46, 46, 34, 28, 38, 58, 58, 69, 67, 72, 83), 'TS': (74, 82, 35, 77, 62, 62, 60, 59, 72, 72, 76, 70, 82, 77), 'RS': (27, 30, 32, 35, 29, 33, 45, 25, 26, 25, 30, 33, 38, 34)}

cat = ('2018-03', '2018-02', '2018-01', '2017-12', '2017-11', '2017-10', '2017-09', '2017-08', '2017-07', '2017-06', '2017-05', '2017-04', '2017-03', '2017-02')

def build_plot():


    fig = plt.figure() # Create matplotlib figure
    ax = fig.add_subplot(111) # Create matplotlib axes
    fig.set_size_inches(9, 3.5)
    ind = np.arange(14)

    width = 0.18 #18
    width1 = 0.10
    avg_bar1 = smdt['OSS'] # (10,20,30,60)
    avg_bar2 = smdt['ATS'] #(20,26,31,31)
    avg_bar3 = smdt['POSS'] #(30,38,38,39)
    avg_bar4 = smdt['TS'] #(40,25,25,28)
    avg_bar5 = smdt['RS']#(40,55,35,45)
    avg_bar6 = smdt['GVS']



    # Second subplot
    plt.subplot(2, 1, 2)
    titles = cat 
    y_pos = np.arange(len(titles))
    width = 0.1

    lst =cat
    colLabels = sorted(lst)

    ax.tick_params(
        axis = 'x',
        left = False,
        right = False

         )
    ax.tick_params(
        axis = 'y',
        left = False,
        right = False
        )

    plt.bar(y_pos -2*width, avg_bar1, width, align='center', alpha=0.8, color='#4285F4')
    plt.bar(y_pos-1*width, avg_bar2, width, align='center', alpha=0.8, color='#FFBB00')
    plt.bar(y_pos, avg_bar1, width, align='center', alpha=0.8, color='#b2b2b2')
    plt.bar(y_pos+1*width, avg_bar2, width, align='center', alpha=0.8, color='#f87b02')
    plt.bar(y_pos+2*width, avg_bar2, width, align='center', alpha=0.8, color='#7cb85e')
    ax.set_xticklabels([]) # turn off x ticks

    fig.subplots_adjust(bottom=0.17) # making some room for the table
    cell_text = []
    colors = [["#4285F4","w","w","w" ,"w"],[ "#FFBB00","w","w","w","w" ], ["#b2b2b2","w","w","w" ,"w"], ["#f87b02","w","w","w" ,"w"], ["#2f86ff","w","w","w","w"],["#7cb85e","w","w","w","w"] ]
    ax.tick_params(axis='both', which='both', length=0)

    cell_text.append(list(avg_bar1))
    cell_text.append(list(avg_bar2))
    rows = ['%s' % x for x in ('Organic Search SOV', 'AMG Total SOV', 'Paid + Organic Search SOV','Total SOV','Retail SOV', 'Glance View Share')]
    #plt.legend( loc="lower center",bbox_to_anchor=[0.5, -0.1],
    #            bbox_transform=fig.transFigure, ncol=3)


    the_table = plt.table(cellText=cell_text, colLabels=colLabels)
          #          rowLabels=rows,cellLoc='center')#, bbox=[0, -0.27, 1,

    plt.xticks(y_pos + width/2, titles)
   # img_path = str(str('eleventh')+str(uuid)+str(slide_id)+'.png')
    plt.savefig('abcf', bbox_inches="tight", pad_inches=0)
build_plot()

此代码中的问题是我的条形图未与数据表对齐。

请建议我如何解决这个问题。

This is how my chart looks

我保持原始xax刻度线打开只是为了告诉u条相对于原始x轴刻度线对齐,而不是相对于数据表对齐。 我希望这与数据表一起使用。请帮我解决这个问题。

0 个答案:

没有答案