首页 > 解决方案 > 在条形图之间创建空间

问题描述

我正在研究以前在此论坛上发布的示例之一。我想在每个图之间放置空格,因为索引有重叠,我几乎看不懂它。这是代码:

import numpy as np
import matplotlib.pyplot as plt

# Data
states = ["AK", "TX", "CA", "MT", "NM", "AZ", "NV", "CO", "OR", "WY", "MI",
          "MN", "UT", "ID", "KS", "NE", "SD", "WA", "ND", "OK", 
         "AK1", "1TX", "C1A", "1MT", "N1M", "A1Z", "N1V", "C1O", "O1R", "W1Y", "1MI",
          "MN1", "UT1", "ID1", "KS1", "NE1", "S1D", "WA1", "ND1", "O1K", ]
staff = np.array([20, 30, 40, 10, 15, 35, 18, 25, 22, 7, 12, 22, 3, 4, 5, 8,
                  14, 28, 24, 32, 20, 30, 40, 10, 15, 35, 18, 25, 22, 7, 12, 22, 3, 4, 5, 8,
                  14, 28, 24, 32])
sales = staff * (20 + 10 * np.random.random(staff.size))

# Sort by number of sales staff
idx = staff.argsort()
states, staff, sales = [np.take(x, idx) for x in [states, staff, sales]]

y = np.arange(sales.size)

fig, axes = plt.subplots(ncols=2, sharey=True)
axes[0].barh(y, staff, align='center', color='gray', zorder=10)
axes[0].set(title='Number of sales staff')
axes[1].barh(y, sales, align='center', color='gray', zorder=10)
axes[1].set(title='Sales (x $1000)')

axes[0].invert_xaxis()
axes[0].set(yticks=y, yticklabels=states)
axes[0].yaxis.tick_right()

for ax in axes.flat:
    ax.margins(0.03)
    ax.grid(True)

fig.tight_layout()
fig.subplots_adjust(wspace=0.19)
plt.show() 

并输出:

在此处输入图像描述

谁能帮我解决这个问题?

标签: pythonmatplotlibbar-chart

解决方案


推荐阅读