带双标题的Matplotlib表

带双标题的Matplotlib表,matplotlib,Matplotlib,Hi可以使matplotlib表具有这样的“双标题” (注意虚线) 您可以使用另一个没有数据作为标题的表来实现这一点。也就是说,创建空表,其列标签将作为表的标题。让我们考虑演示示例。首先,添加表header\u 0和header\u 1。第二步,更正标题和表的参数bbox,以正确定位所有表。由于表是重叠的,所以包含数据的表应该是最后一个 import numpy as np import matplotlib.pyplot as plt data = [[ 66386, 174296,

Hi可以使matplotlib表具有这样的“双标题” (注意虚线)


您可以使用另一个没有数据作为标题的表来实现这一点。也就是说,创建空表,其列标签将作为表的标题。让我们考虑演示示例。首先,添加表
header\u 0
header\u 1
。第二步,更正标题和表的参数
bbox
,以正确定位所有表。由于表是重叠的,所以包含数据的表应该是最后一个

import numpy as np
import matplotlib.pyplot as plt


data = [[  66386,  174296,   75131,  577908,   32015],
        [  58230,  381139,   78045,   99308,  160454],
        [  89135,   80552,  152558,  497981,  603535],
        [  78415,   81858,  150656,  193263,   69638],
        [ 139361,  331509,  343164,  781380,   52269]]

columns = ('Freeze', 'Wind', 'Flood', 'Quake', 'Hail')
rows = ['%d year' % x for x in (100, 50, 20, 10, 5)]

values = np.arange(0, 2500, 500)
value_increment = 1000

# Get some pastel shades for the colors
colors = plt.cm.BuPu(np.linspace(0, 0.5, len(rows)))
n_rows = len(data)

index = np.arange(len(columns)) + 0.3
bar_width = 0.4

# Initialize the vertical-offset for the stacked bar chart.
y_offset = np.array([0.0] * len(columns))

# Plot bars and create text labels for the table
cell_text = []
for row in range(n_rows):
    plt.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row])
    y_offset = y_offset + data[row]
    cell_text.append(['%1.1f' % (x/1000.0) for x in y_offset])
# Reverse colors and text labels to display the last value at the top.
colors = colors[::-1]
cell_text.reverse()

# Add headers and a table at the bottom of the axes
header_0 = plt.table(cellText=[['']*2],
                     colLabels=['Extra header 1', 'Extra header 2'],
                     loc='bottom',
                     bbox=[0, -0.1, 0.8, 0.1]
                     )

header_1 = plt.table(cellText=[['']],
                     colLabels=['Just Hail'],
                     loc='bottom',
                     bbox=[0.8, -0.1, 0.2, 0.1]
                     )

the_table = plt.table(cellText=cell_text,
                      rowLabels=rows,
                      rowColours=colors,
                      colLabels=columns,
                      loc='bottom',
                      bbox=[0, -0.35, 1.0, 0.3]
                      )

# Adjust layout to make room for the table:
plt.subplots_adjust(left=0.2, bottom=-0.2)

plt.ylabel("Loss in ${0}'s".format(value_increment))
plt.yticks(values * value_increment, ['%d' % val for val in values])
plt.xticks([])
plt.title('Loss by Disaster')

plt.show()

如果额外的标题是对称的,或者合并了等量的“正常”标题,则只需添加一个额外的标题表并更正数据表的
bbox
,如下所示(与删除列的示例相同):


您可以使用另一个没有数据作为标题的表来执行此操作。也就是说,创建空表,其列标签将作为表的标题。让我们考虑演示示例。首先,添加表
header\u 0
header\u 1
。第二步,更正标题和表的参数
bbox
,以正确定位所有表。由于表是重叠的,所以包含数据的表应该是最后一个

import numpy as np
import matplotlib.pyplot as plt


data = [[  66386,  174296,   75131,  577908,   32015],
        [  58230,  381139,   78045,   99308,  160454],
        [  89135,   80552,  152558,  497981,  603535],
        [  78415,   81858,  150656,  193263,   69638],
        [ 139361,  331509,  343164,  781380,   52269]]

columns = ('Freeze', 'Wind', 'Flood', 'Quake', 'Hail')
rows = ['%d year' % x for x in (100, 50, 20, 10, 5)]

values = np.arange(0, 2500, 500)
value_increment = 1000

# Get some pastel shades for the colors
colors = plt.cm.BuPu(np.linspace(0, 0.5, len(rows)))
n_rows = len(data)

index = np.arange(len(columns)) + 0.3
bar_width = 0.4

# Initialize the vertical-offset for the stacked bar chart.
y_offset = np.array([0.0] * len(columns))

# Plot bars and create text labels for the table
cell_text = []
for row in range(n_rows):
    plt.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row])
    y_offset = y_offset + data[row]
    cell_text.append(['%1.1f' % (x/1000.0) for x in y_offset])
# Reverse colors and text labels to display the last value at the top.
colors = colors[::-1]
cell_text.reverse()

# Add headers and a table at the bottom of the axes
header_0 = plt.table(cellText=[['']*2],
                     colLabels=['Extra header 1', 'Extra header 2'],
                     loc='bottom',
                     bbox=[0, -0.1, 0.8, 0.1]
                     )

header_1 = plt.table(cellText=[['']],
                     colLabels=['Just Hail'],
                     loc='bottom',
                     bbox=[0.8, -0.1, 0.2, 0.1]
                     )

the_table = plt.table(cellText=cell_text,
                      rowLabels=rows,
                      rowColours=colors,
                      colLabels=columns,
                      loc='bottom',
                      bbox=[0, -0.35, 1.0, 0.3]
                      )

# Adjust layout to make room for the table:
plt.subplots_adjust(left=0.2, bottom=-0.2)

plt.ylabel("Loss in ${0}'s".format(value_increment))
plt.yticks(values * value_increment, ['%d' % val for val in values])
plt.xticks([])
plt.title('Loss by Disaster')

plt.show()

如果额外的标题是对称的,或者合并了等量的“正常”标题,则只需添加一个额外的标题表并更正数据表的
bbox
,如下所示(与删除列的示例相同):

header = plt.table(cellText=[['']*2],
                      colLabels=['Extra header 1', 'Extra header 2'],
                      loc='bottom'
                      )

the_table = plt.table(cellText=cell_text,
                      rowLabels=rows,
                      rowColours=colors,
                      colLabels=columns,
                      loc='bottom',
                      bbox=[0, -0.35, 1.0, 0.3]
                      )