减少Matplotlib直方图中条形之间的空白

时间:2019-08-27 22:43:47

标签: python pandas matplotlib histogram histogram2d

我生成了以下直方图: enter image description here

我希望压缩直方图,即减小条形之间的空白(例如,在0.8-0.85和0.85-0.9之间)。

在这方面,增加条形宽度的解决方案对我来说并不方便。

据我所知,我发现的所有已经问过的问题都说明了如何减少外部空白(即在0.95-1之后和0.75-0.8之前)。

我用来生成直方图的代码是:

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt

dataset = pd.read_csv('compare.csv', usecols=['Y', 'X'])

dict = {'0.75-0.8': [0, 0], '0.8-0.85': [0, 0], '0.85-0.9': [0, 0], '0.9-0.95': [0, 0], '0.95-1': [0, 0]}

mylist = list(dict.values())
#various code that fills 'mylist'    

df = pd.DataFrame(data=mylist,
                 index=['0.75-0.8', '0.8-0.85', '0.85-0.9', '0.9-0.95', '0.95-1'],
                 columns=pd.Index(['Y', 'X'],
                 )).round(2)

ax = pd.DataFrame(df).plot.bar(color=("dodgerblue", "pink"), legend=True, figsize=(8, 6))
for container, hatch in zip(ax.containers, ("", "//")):
    for patch in container.patches:
        patch.set_hatch(hatch)
plt.show()

1 个答案:

答案 0 :(得分:1)

更改此行
ax = pd.DataFrame(df).plot.bar(color=("dodgerblue", "pink"), legend=True, figsize=(8, 6))

与此行

ax = df.plot(kind='bar', width=0.9, color=("dodgerblue", "pink"), legend=True, figsize=(8, 6))

并根据自己的喜好调整值的宽度

enter image description here