Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib条形图间距无法正常工作_Python_Dataframe_Matplotlib - Fatal编程技术网

Python matplotlib条形图间距无法正常工作

Python matplotlib条形图间距无法正常工作,python,dataframe,matplotlib,Python,Dataframe,Matplotlib,我使用matplotlib和pandas DataFrame绘制条形图,如下所示: pdata = pd.DataFrame([[11.14285714, 6.33333333, 2.52380952], [10.47619048, 6.61904762, 2.9047619 ], [10.80952381, 6.19047619, 3. ], [11.0952381 , 6.66666667, 2.23809524],

我使用matplotlib和pandas DataFrame绘制条形图,如下所示:

pdata = pd.DataFrame([[11.14285714,  6.33333333,  2.52380952],
       [10.47619048,  6.61904762,  2.9047619 ],
       [10.80952381,  6.19047619,  3.        ],
       [11.0952381 ,  6.66666667,  2.23809524],
       [10.14285714,  4.9047619 ,  4.95238095],
       [ 9.61904762,  5.71428571,  4.66666667],
       [10.0952381 ,  5.14285714,  4.76190476],
       [ 9.47619048,  5.14285714,  5.38095238],
       [10.66666667,  4.38095238,  4.95238095],
       [ 9.66666667,  5.04761905,  5.28571429],
       [10.33333333,  4.95238095,  4.71428571],
       [10.85714286,  4.9047619 ,  4.23809524],
       [ 9.71428571,  4.9047619 ,  5.38095238],
       [10.71428571,  4.52380952,  4.76190476],
       [ 9.57142857,  3.71428571,  6.71428571],
       [11.61904762,  5.47619048,  2.9047619 ],
       [12.23809524,  5.23809524,  2.52380952],
       [11.28571429,  7.28571429,  1.42857143],
       [10.52380952,  6.52380952,  2.95238095],
       [10.80952381,  6.38095238,  2.80952381],
       [10.95238095,  7.71428571,  1.33333333],
       [11.0952381 ,  7.42857143,  1.47619048],
       [10.0952381 ,  8.71428571,  1.19047619],
       [10.42857143,  8.42857143,  1.14285714],
       [10.57142857,  7.95238095,  1.47619048],
       [10.14285714,  8.66666667,  1.19047619],
       [ 9.38095238,  9.38095238,  1.23809524],
       [ 8.9047619 ,  9.80952381,  1.28571429],
       [10.66666667,  8.04761905,  1.28571429],
       [ 9.19047619,  9.19047619,  1.61904762]])
pdata.index = np.arange(30)+1

fig,axes = plt.subplots(1,2,figsize=(6,3),sharey=True)
ax = axes[0]
pdata[pdata.index<=15].plot(
    ax=ax,kind='bar',stacked=True,width=.95, color=colors,alpha=.7, rot=0)

ax = axes[1]
pdata[pdata.index>15].plot(
    ax=ax,kind='bar',stacked=True, color=colors,width=.95,alpha=.7, rot=0)
ax.get_legend().remove()
ax.set_ylim([0,20])
fig.tight_layout()
但是,生成的图形如下所示,各条之间的间距不同:


有没有办法纠正间距?此外,是否可以进一步缩短两个绘图之间的距离?

您看到的是两种效果的组合,宽度属性设置为0.95,dpi太低,无法解析条之间的所有空白。如果您需要一些空白,但在每个条之间,您可以简单地增加图形的dpi,如注释所示,如中所示

将数字的dpi设置为300,并生成类似

其中,条间空白由width=0.95创建,如果希望条间完全没有空格,只需设置width=1.0即可生成

请注意,在这两种情况下,我都使用plt.subplot\u adjustwspace=0.05而不是fig.tight\u布局,但这是一个首选问题。此外,请注意,如果使用宽度=1.0,则无需调整dpi属性,因为显示将正确:

完整示例
为什么不使用width=1来获得无间距?要减小间距,可以使用plt.subplot\u adjustwspace=0.05并删除fig.tight\u布局。您可以控制wspace以获得所需的间距。关于条间距,该问题与dpi有关,因为当我将dpi增加到500时,fig大小增加,并且条之间的间距变得一致和相等。在dpiYou上@ImportanceOfBeingErnest是一个很好的答案。您还应该检查昨天提出的问题
fig,axes = plt.subplots(1,2,figsize=(6,3),sharey=True, dpi=300)
import pandas as pd
import matplotlib.pyplot as plt
pdata = pd.DataFrame([[11.14285714,  6.33333333,  2.52380952],
       [10.47619048,  6.61904762,  2.9047619 ],
       [10.80952381,  6.19047619,  3.        ],
       [11.0952381 ,  6.66666667,  2.23809524],
       [10.14285714,  4.9047619 ,  4.95238095],
       [ 9.61904762,  5.71428571,  4.66666667],
       [10.0952381 ,  5.14285714,  4.76190476],
       [ 9.47619048,  5.14285714,  5.38095238],
       [10.66666667,  4.38095238,  4.95238095],
       [ 9.66666667,  5.04761905,  5.28571429],
       [10.33333333,  4.95238095,  4.71428571],
       [10.85714286,  4.9047619 ,  4.23809524],
       [ 9.71428571,  4.9047619 ,  5.38095238],
       [10.71428571,  4.52380952,  4.76190476],
       [ 9.57142857,  3.71428571,  6.71428571],
       [11.61904762,  5.47619048,  2.9047619 ],
       [12.23809524,  5.23809524,  2.52380952],
       [11.28571429,  7.28571429,  1.42857143],
       [10.52380952,  6.52380952,  2.95238095],
       [10.80952381,  6.38095238,  2.80952381],
       [10.95238095,  7.71428571,  1.33333333],
       [11.0952381 ,  7.42857143,  1.47619048],
       [10.0952381 ,  8.71428571,  1.19047619],
       [10.42857143,  8.42857143,  1.14285714],
       [10.57142857,  7.95238095,  1.47619048],
       [10.14285714,  8.66666667,  1.19047619],
       [ 9.38095238,  9.38095238,  1.23809524],
       [ 8.9047619 ,  9.80952381,  1.28571429],
       [10.66666667,  8.04761905,  1.28571429],
       [ 9.19047619,  9.19047619,  1.61904762]])
pdata.index = np.arange(30)+1


# With spacing between bars
fig,axes = plt.subplots(1,2,figsize=(6,3),sharey=True, dpi=300)
plt.subplots_adjust(wspace=0.05)
ax = axes[0]
colors = list()
pdata[pdata.index<=15].plot(
    ax=ax,kind='bar',stacked=True,width=0.95 ,alpha=.7, rot=0)

ax = axes[1]
pdata[pdata.index>15].plot(
    ax=ax,kind='bar',stacked=True, width=0.95,alpha=.7, rot=0)
ax.get_legend().remove()
ax.set_ylim([0,20])
plt.show()

# Without any spacing between bars
fig,axes = plt.subplots(1,2,figsize=(6,3),sharey=True, dpi=300)
plt.subplots_adjust(wspace=0.05)
ax = axes[0]
colors = list()
pdata[pdata.index<=15].plot(
    ax=ax,kind='bar',stacked=True,width=1 ,alpha=.7, rot=0)

ax = axes[1]
pdata[pdata.index>15].plot(
    ax=ax,kind='bar',stacked=True, width=1,alpha=.7, rot=0)
ax.get_legend().remove()
ax.set_ylim([0,20])
plt.show()