8
votes

I'm trying to get the python tables for a bar plot to be aligned. For example in the attached graph, you'll see that x-axis is not properly aligned to the vertical line that is down by the python table.

I tried modifying the scale of the figure

I want the font size of the table to be 40 so that it will be visible when i print the IEEEtran paper.

#!/usr/bin/env python
import csv
import numpy as np
import matplotlib.pyplot as plt
import matplotlib

def plot_bar(dataset):
    matplotlib.rc('font', family='sans-serif')
    matplotlib.rc('font', serif='Helvetica Neue')
    matplotlib.rc('text', usetex='false')
    matplotlib.rcParams.update({'font.size': 30})
    fig = plt.figure()
    ax = fig.add_subplot(111)
    fig = matplotlib.pyplot.gcf()
    fig.set_size_inches(30.0,7.5)
    N = len(dataset[1])

    Load    = dataset[0]
    QoS     = dataset[1]
    Energy  = dataset[2]

    ind = np.arange(N)
    width = 0.35

    plt.tick_params(axis='both', which='major', labelsize=35, pad=15)
    plt.tick_params(axis='y', which='minor', labelsize=35, pad=15)



    rects1 = ax.bar(ind, QoS, width,
                color='0.2',
                label='HP')

    rects3 = ax.bar(ind+width, Energy, width,
                color='0.4',
                label='OM')

    lns = [rects1, rects3]
    labs = [l.get_label() for l in lns]

    ax.legend(lns, labs, ncol=2)

    ax.set_xlim(-width,len(ind)+width)
    ax.set_ylim(0, 16000)

    ax.set_ylabel('RPS/Watt', fontsize=35)
    ax.set_xlabel('Percentage of Max Capacity', fontsize=35)

    xTickMarks = dataset[0]
    ax.set_xticks(ind+width)
    xtickNames = ax.set_xticklabels(xTickMarks)
    plt.setp(xtickNames, rotation=0, fontsize=40)
    plt.xticks([])
    ax.yaxis.grid()

    cell_text = [['2S-0.65GHz', '3S-0.65GHz', '4S-0.65GHz', '4S-0.65GHz', '2B2S-1.15GHz', '2B2S-1.15GHz', '3B2S-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz','2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz'],
            ['2S-0.65GHz', '3S-0.65GHz', '4S-0.65GHz', '4S-0.65GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz', '2B-1.15GHz']]
    colors=['0.2','0.4']
    rows = ['HP','OM']
    Loc='right'
    the_table = plt.table(cellText=cell_text,
                          rowLabels=rows,
                          colLabels=Load,
                          rowColours=colors,
                        cellLoc='right',
                          loc='bottom')
    the_table.scale(1,2.5)

    the_table.auto_set_font_size(False)
    the_table.set_fontsize(12)
    plt.subplots_adjust(left=0.2, bottom=0.2)

    ax.xaxis.labelpad = 70

    ax.yaxis.labelpad = 20
    fig.savefig('rps-watt' +'.eps',format='eps',bbox_inches='tight', pad_inches=0.1, dpi=1000)

dataset = [['29%', '40%', '51%', '63%', '69%', '71%', '74%', '77%', '80%', '83%', '86%', '89%', '91%', '94%', '97%', '100%'], [6524.0, 8749.0, 10470.0, 13096.0, 13126.0, 12965.0, 13493.0, 13717.0, 14351.0, 14993.0, 15308.0, 14320.0, 13179.0, 9809.0, 10168.0, 10621.0], [6524.0, 8749.0, 10470.0, 13096.0, 6827.0, 5586.0, 7697.0, 8205.0, 8298.0, 8733.0, 8887.0, 9278.0, 9659.0, 9809.0, 10168.0, 10621.0]]
plot_bar(dataset)

enter image description here

1
any update please? I am searching on how to change the width on matplotlib.. but didn't find anything - tandem
Try to make your code self-contained so that it executes for us (incluide data) and cut it down as much as possible to only show the essential problam. That way answers are more likely. - tfv
@tfv: I gave a MWE now. maybe now? - tandem

1 Answers

8
votes

I think this is what you are looking for.
-added a parameter "spare_width" that makes it so the bar graph is proper.
-resized the legend font and xlabel spacing to look better and not block the data.
-added the "ggplot" style - I like it better.
-increased font size to 35 by moving the frequency (GHz) to it's own line and setting height through bbox.

import csv
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import matplotlib

def plot_bar(dataset):
    matplotlib.rc('font', family='sans-serif')
    matplotlib.rc('font', serif='Helvetica Neue')
    matplotlib.rc('text', usetex='false')
    matplotlib.rcParams.update({'font.size': 30})
    fig = plt.figure()
    ax = fig.add_subplot(111)
    fig = matplotlib.pyplot.gcf()
    fig.set_size_inches(30.0,7.5)
    N = len(dataset[1])

    Load    = dataset[0]
    QoS     = dataset[1]
    Energy  = dataset[2]

    ind = np.arange(N)
    width = 0.35
    spare_width = (1 - width*2)/2

    plt.tick_params(axis='both', which='major', labelsize=35, pad=15)
    plt.tick_params(axis='y', which='minor', labelsize=35, pad=15)



    rects1 = ax.bar(ind, QoS, width,
                color='0.2',
                label='HP')

    rects3 = ax.bar(ind+width, Energy, width,
                color='0.4',
                label='OM')

    lns = [rects1, rects3]
    labs = [l.get_label() for l in lns]

    ax.legend(lns, labs, ncol=2, fontsize=30,framealpha=0)

    ax.set_xlim(-spare_width,len(ind)-spare_width)
    ax.set_ylim(0, 16000)

    ax.set_ylabel('RPS/Watt', fontsize=35)
    ax.set_xlabel('Percentage of Max Capacity', fontsize=35)

    xTickMarks = dataset[0]
    ax.set_xticks(ind+width)
    xtickNames = ax.set_xticklabels(xTickMarks)
    plt.setp(xtickNames, rotation=0, fontsize=40)
    plt.xticks([])
    ax.yaxis.grid()

    cell_text = [['2S\n0.65', '3S\n0.65', '4S\n0.65', '4S\n0.65', 
                  '2B2S\n1.15','2B2S\n1.15', '3B2S\n1.15', '2S\n1.15', 
                  '2S\n1.15', '2S\n1.15', '2S\n1.15','2S\n1.15', 
                  '2S\n1.15', '2S\n1.15', '2S\n1.15', '2S\n1.15'],
            ['2S\n0.65', '3S\n0.65', '4S\n0.65', '4S\n0.65',
             '2S\n1.15', '2S\n1.15', '2S\n1.15', '2S\n1.15', 
             '2S\n1.15', '2S\n1.15', '2S\n1.15', '2S\n1.15', 
             '2S\n1.15', '2S\n1.15', '2S\n1.15', '2B\n1.15']]
    colors=['0.2','0.4']
    rows = ['HP\nGHz','OM\nGHz']
    Loc='right'
    the_table = plt.table(cellText=cell_text,
                          rowLabels=rows,
                          colLabels=Load,
                          rowColours=colors,
                        cellLoc='center',
                          loc='bottom',
                          bbox=[0,-0.65,1,0.65])#x,y,w,h
    the_table.scale(1,2.5)

    the_table.auto_set_font_size(False)
    the_table.set_fontsize(35)
    plt.subplots_adjust(left=0.2, bottom=0.2)

    ax.xaxis.labelpad = 260

    ax.yaxis.labelpad = 20
    fig.savefig('rps-watt' +'.eps',format='eps',bbox_inches='tight', pad_inches=0.1, dpi=1000)

dataset = [['29%', '40%', '51%', '63%', '69%', '71%', '74%', '77%', '80%', '83%', '86%', '89%', '91%', '94%', '97%', '100%'], [6524.0, 8749.0, 10470.0, 13096.0, 13126.0, 12965.0, 13493.0, 13717.0, 14351.0, 14993.0, 15308.0, 14320.0, 13179.0, 9809.0, 10168.0, 10621.0], [6524.0, 8749.0, 10470.0, 13096.0, 6827.0, 5586.0, 7697.0, 8205.0, 8298.0, 8733.0, 8887.0, 9278.0, 9659.0, 9809.0, 10168.0, 10621.0]]
plot_bar(dataset)

This plot created using WinPython-64bit-3.4.4.1Qt5 download here enter image description here