-1

我正在使用代码示例bar构建一个带有表格的图表。

当我有多达 10 列时,一切看起来都很好,但超过 10 列时,条形图向左移动,并且与表格列的位置不匹配。这只发生在奇数列/条(例如 11 和 13,但不是 12 和 14 看起来不错)。看起来第 11 列和第 13 列的 x 轴为第 12 列和第 14 列保留了空间。我附上了我对上述示例所做的代码更改,以便重现问题。

我已经为此苦苦挣扎了太久,在网上没有找到任何东西,所以我将不胜感激。

示例中的修改内容:

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

colLabels = ('Freeze', 'Wind', 'Flood', 'Quake', 'Hail', 'Freeze2', 'Wind2', 'Flood2', 'Quake2', 'Hail2', '11th', "12th", "13th")
4

2 回答 2

0

确保您指定xlim

xlim(0,len(colLabels))

使用对齐表绘图

如果您不这样做,那么 matplotlib 将自动缩放 x 轴,在这种情况下这不是您想要的,因为绘图下方的表格与实际条形的位置相关联。

于 2013-05-21T19:42:49.227 回答
0

以前的答案已经过时,并没有真正起作用。在您的示例中,您需要做两件事。

  1. 更改index = np.arange(len(columns)) + 0.3index = np.arange(len(columns))

  2. 添加plt.xlim([-0.5, len(colLabels)-0.5])

import numpy as np
import matplotlib.pyplot as plt

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

colLabels = ('Freeze', 'Wind', 'Flood', 'Quake', 'Hail', 'Freeze2', 'Wind2', 'Flood2', 'Quake2', 'Hail2', '11th', "12th", "13th")
columns = colLabels
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
index = np.arange(len(columns))
print(index)
bar_width = 0.4

# Initialize the vertical-offset for the stacked bar chart.
y_offset = np.zeros(len(columns))

fig, ax = plt.subplots(figsize=(15, 10))
# 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()
plt.xlim([-0.5, len(colLabels)-0.5])
# Add a table at the bottom of the axes
the_table = ax.table(cellText=cell_text,
                      rowLabels=rows,
                      rowColours=colors,
                      colLabels=columns,
                      loc='bottom')

plt.subplots_adjust(left=0.2, bottom=0.2)
plt.ylabel("Loss in ${0}'s".format(value_increment), fontsize=15)
plt.yticks(values * value_increment, ['%d' % val for val in values], fontsize=13)
plt.xticks([])
plt.title('Loss by Disaster', fontsize=15)
plt.show()

在此处输入图像描述

于 2022-01-26T09:06:10.590 回答