Python 图表栏matplotlib-重叠栏

Python 图表栏matplotlib-重叠栏,python,matplotlib,Python,Matplotlib,我是matplotlib的新用户,我对图表栏有一个问题:重叠栏。 绘制图形时,条形图重叠,我还没有找到原因。在我看来,问题在于重新调整图表的大小。我重新调整了它的大小,因为将来我会插入标题、图例和x、y描述。我尝试了一些解决方案,但我有一个解决方案!! 这是我的代码: import matplotlib.pyplot as plt import matplotlib.font_manager as fm from matplotlib.colors import Ligh

我是matplotlib的新用户,我对图表栏有一个问题:重叠栏。 绘制图形时,条形图重叠,我还没有找到原因。在我看来,问题在于重新调整图表的大小。我重新调整了它的大小,因为将来我会插入标题、图例和x、y描述。我尝试了一些解决方案,但我有一个解决方案!! 这是我的代码:

    import matplotlib.pyplot as plt
    import matplotlib.font_manager as fm
    from matplotlib.colors import LightSource
    from matplotlib.artist import Artist
    import numpy as np
    from decimal import *
    import datetime
    #Size
    width = 360
    height = 240
    dpi = 80.0
    colors = ['#be1e2d',
            '#666699',
            '#92d5ea',
            '#ee8310',
            '#8d10ee',
            '#5a3b16',
            '#26a4ed',
            '#f45a90',
            '#e9e744']
    #Data
    columns = ['2005','2006']
    data = [[2.6,3.5],[2, 1.5]]
    linewidth = 1
    N = len(columns)
    
    ind = np.arange(N)  
    #Re-Size
    rdata = len(data) if columns is None else len(columns)
    heightColumn = height*1.0 / (rdata) / (len(columns))
    heightColumn = heightColumn/dpi
    fig = plt.figure(1, figsize=(width/dpi,height/dpi),facecolor='w')
    ax = fig.add_axes([0.2, 0.3, 0.6, 0.5])
    #Draw
    tupleRects = ()
    idxColor = 0 
    valPositionCol = ind
    for dat in data:
        rects = plt.barh(valPositionCol, dat, heightColumn, color=colors[idxColor], alpha=0.8,  
                         linewidth=linewidth)
        valPositionCol=valPositionCol+heightColumn
        idxColor += 1
        if idxColor==9:
            idxColor = 0
        tupleRects += (rects,)
    plt.show()
谢谢


代码相同,但我更改了数据(
columns[]
e
data[]
):


问题是我有可变的数据,我必须找到一个稳定的算法。

我认为你误解了高度的含义()。单位是轴单位,而不是像素

heightColumn = height*1.0 / (rdata) / (len(columns))
heightColumn = heightColumn/dpi


将得到不重叠的条,组之间只需要一点额外的空间。只要
1

非常感谢您的回复,您可以通过将组的
height\u变大或变小来调整组之间的间距。一个问题:由于我是matplotlib的新用户,它的值0.9是什么意思?另一个问题,这个操作对很多数据也有效吗?高度的单位是轴单位。给定的
dat
中的条形图以这些单位用
1
分隔(给定您根据
ind
绘制它们的方式)。因此,如果希望组之间有间隙,则(组中)所有钢筋的高度之和必须小于
1
height\u of_group
设置总和,因此在这种情况下,
N
th group中的条形将以轴为单位从N变为N+.9。这应该适用于任何列数和数据长度。非常感谢!!!另一个问题:如果我有一个条形图,区别是什么(大约0.9个值)??我不理解你的问题。你把这个问题解决了吗?
heightColumn = height*1.0 / (rdata) / (len(columns))
heightColumn = heightColumn/dpi
height_of_group = .9
heightColumn = height_of_group / (len(data))
#heightColumn = heightColumn/dpi