matplotlib 表被裁剪 [英] matplotlib table gets cropped

查看:36
本文介绍了matplotlib 表被裁剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从小程序底部获取一个由numpy数组组成的表格,但该表格被裁剪了,这是代码:

I'm trying to get a table form a numpy array at the barplot bottom but it gets cropped, this is the code:

import matplotlib.pyplot as plt
import pylab as p
import numpy as np

a = np.array([[ 100.        ,  152.84493519,  233.63122532,  263.7018431 ,
                259.22927686,  243.56305545],
              [ 100.        ,  147.64885749,  194.26319507,  156.2985505 ,
                169.1050851 ,  124.84312468],
              [ 100.        ,  195.46940792,  273.37157492,  296.54100691,
                271.93708044,  358.30174259],
              [ 100.        ,  216.44727433,  308.30389994,  243.70797244,
                335.3325307 ,  396.22671612]])
fig = p.figure()
ax = fig.add_subplot(1,1,1)

y = np.mean(a, axis=0)
N = len(y)
ind = range(N)
err = np.std(a, axis=0)/np.sqrt(N)

ax.bar(ind, y, facecolor='#777777',
       align='center', yerr=err, ecolor='black',
       bottom=4)
ax.set_ylabel('ylabel')

ax.set_title('Counts, by group',fontstyle='italic')

tfig = 'Fig 1'
ax.set_title(tfig, fontstyle='italic')

labels = ['a', 'b', 'c', 'd', 'e', 'f']
rowlab = ['row1', 'row2', 'row3', 'row4']
ax.set_xticklabels(labels)
ax.xaxis.set_visible(False)
the_table = ax.table(cellText=a, colLabels=labels, rowLabels=rowlab)
table_props = the_table.properties()
table_cells = table_props['child_artists']
for cell in table_cells: 
    cell.set_fontsize(12)
    cell.set_height(0.05)

fig.savefig(('prueba.png'), dpi=300)

如何获取整个表格,以及如何将条形与每一列对齐?

How can I get the whole table, and also how can I align the bars with each column?

推荐答案

在保存图形之前向您的代码添加以下两行:

add following two lines to you code before saving the figure:

ax.set_xlim(-0.5, 5.5)
p.subplots_adjust(bottom=0.2)

这篇关于matplotlib 表被裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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