matplotlib条形图中条形之间的空间太大

时间:2017-03-29 13:19:45

标签: python matplotlib charts

我正在尝试使用matplotlib创建一个条形图。

x轴数据是年份列表:[1950,1960,1970,1980,1990,1995-2015]

y轴数据是一个列表,其数量与年份相同。

这是我的代码:

import csv
import matplotlib.pyplot as plt

path = "bevoelkerung_entwicklung.csv"



with open(path, 'r') as datei:
    reader = csv.reader(datei, delimiter=';')
    jahr = next(reader)
    population = next(reader)

population_list = []

for p in population:
    population_list.append(str(p).replace("'",""))

population_list = list(map(int, population_list))
jahr = list(map(int, jahr))

datei.close()

plt.bar(jahr,population_list, color='c')

plt.xlabel('Year')
plt.ylabel('Population in 1000')
plt.title('Population growth')
plt.legend()
plt.show()

结果如下: Too much space between bars

正如你所看到的,1950 - 1960年间的差距是巨大的。我怎样才能做到这一点,以便在1950-1995之间没有间隙。我知道它有10年的间隔,但它看起来并不好。

任何帮助都会被贬低。

1 个答案:

答案 0 :(得分:0)

您需要将总体数据绘制为增加整数的函数。这使得条具有相等的间距。然后,您可以将标签调整为每个图表所代表的年份。

import matplotlib.pyplot as plt
import numpy as np

jahre = np.append(np.arange(1950,2000,10), np.arange(1995,2017))
bevoelkerung = np.cumsum(np.ones_like(jahre))
x = np.arange(len(jahre))

plt.bar(x, bevoelkerung)
plt.xticks(x, jahre, rotation=90)
plt.show()

enter image description here