使用matplotlib python将表格与x轴对齐 [英] Aligning table to x-axis using matplotlib python

查看:87
本文介绍了使用matplotlib python将表格与x轴对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取要对齐的条形图的python表.例如,在所附的图中,您将看到x轴未正确对齐python表向下的垂直线.

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.

我试图修改图形的比例

我希望表格的字体大小为40,以便在打印IEEEtran纸时可以看到它.

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)

推荐答案

我认为这就是您要找的.
-添加了参数"spare_width",使其适合条形图.
- 调整了图例字体和 xlabel 间距的大小以使其看起来更好并且不会阻塞数据.
- 添加了ggplot"样式 - 我更喜欢它.
- 通过将频率 (GHz) 移动到它自己的行并通过 bbox 设置高度,将字体大小增加到 35.

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)

此图使用 WinPython-64bit-3.4.4.1Qt5 创建

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

这篇关于使用matplotlib python将表格与x轴对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆