获取对当前子图的引用

时间:2018-11-30 06:35:20

标签: python matplotlib

如何获取对当前子图的引用,以便可以使用.add_patch(plt.Rectangle)在其周围绘制边框?是从头开始解决问题的方法吗?

这是我的代码:

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)

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)
    ax.grid(True, which='major', axis='y')
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    if not is_last:
        for tick in ax.xaxis.get_major_ticks():
            tick.set_visible(False)

0 个答案:

没有答案