当子图的高度不同时,保持子图栏高度不变

时间:2018-11-29 22:45:33

标签: matplotlib

是否有可能使子图比其他子图高,以便为X轴刻度标签腾出空间,但内部条形图的高度与较短子图中的条形高度相同?当我将高度参数添加到df.plot()时,我得到“ TypeError:()为关键字参数'height'获得了多个值”。这是我的代码:

from collections import OrderedDict

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd


data = OrderedDict()
data['Test Break'] = [0.1, 0.5, np.nan]
data['No Break']   = [0.9, 0.5, np.nan]
data['Not Tested'] = [0.0, 0.0, 1.0000]
index = ['Very very long name ' + str(x+1) for x in range(len(data))]
df = pd.DataFrame(data=data, index=index)

num_plots = 2
rows = num_plots + 1
cols = 1
layout = (rows, cols)

red, green, grey = '#FF0000', '#00FF00', '#888888'
light_grey = '#AAAAAA'

fig = plt.figure()
fig.set_size_inches(6, 3)

for z in range(num_plots):
    is_last = z == num_plots - 1
    rowspan = 2 if is_last else 1
    ax = plt.subplot2grid(layout, (z, 0), rowspan=rowspan)
    df.plot(ax=ax, kind='bar', stacked=True, yticks=[0,1], legend=False, color=[red, green, grey])
    plt.subplots_adjust(left=0.05, right=0.95, bottom=0.02, top=0.98, hspace=0.5)
    ax.grid(True, which='major', axis='y')
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['bottom'].set_edgecolor(light_grey)
    ax.spines['left'].set_edgecolor(light_grey)
    if not is_last:
        for tick in ax.xaxis.get_major_ticks():
             tick.set_visible(False)

0 个答案:

没有答案