我使用下面的代码使用matplotlib构建条形图。当我的第一列或最后一列数据为0时,我的第一列被楔入Y轴。在条形图和Y轴之间添加填充
这是一个例子。请注意,第一列是在x = 0点上。
如果我在这列数据,我得到的Y轴和这里看到的第一列之间的巨大空白。注意附加栏,现在在X = 0。如果我在最后一栏中有数据,则重复此效果。
我的代码如下:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import MultipleLocator
binVals = [0,5531608,6475325,1311915,223000,609638,291151,449434,1398731,2516755,3035532,2976924,2695079,1822865,1347155,304911,3562,157,5,0,0,0,0,0,0,0,0]
binTot = sum(binVals)
binNorm = []
for v in range(len(binVals)):
binNorm.append(float(binVals[v])/binTot)
fig = plt.figure(figsize=(6,4))
ax1 = fig.add_subplot(1,1,1)
ax1.bar(range(len(binNorm)),binNorm,align='center', label='Values')
plt.legend(loc=1)
plt.title("Demo Histogram")
plt.xlabel("Value")
plt.xticks(range(len(binLabels)),binLabels,rotation='vertical')
plt.grid(b=True, which='major', color='grey', linestyle='--', alpha=0.35)
ax1.xaxis.grid(False)
plt.ylabel("% of Count")
plt.subplots_adjust(bottom=0.15)
plt.tight_layout()
plt.show()
如何设置Y轴和我的第一个/最后一棒之间保持恒定的距离呢?
此外,我意识到它被标记为“演示直方图”,那是因为在纠正问题here时我错过了它。