【问题标题】:Chart barh matplotlib - overlap bars图表 barh matplotlib - 重叠条
【发布时间】:2012-09-26 09:03:45
【问题描述】:

我是 matplotlib 的新用户,我对图表 barh 有疑问:重叠条。 绘制图表时,条形图重叠,我没有找到原因。在我看来,问题在于重新调整图表的大小。我重新调整它的大小,因为将来我将插入标题、图例和 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[]):

import matplotlib.pyplot as plt
import matplotlib.font_manager as fm


from matplotlib.colors import LightSource
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 = [[1.5, 1.5], [1.5,1.5], [1.5,1.5]]
linewidth = 1
N = len(columns)


ind = np.arange(N)  
#Re-Size
height_of_group = .9
   

heightColumn = height_of_group / (len(columns))
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()

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

【问题讨论】:

  • 你解决了吗?

标签: python matplotlib


【解决方案1】:

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

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

height_of_group = .9
heightColumn = height_of_group / (len(data))
#heightColumn = heightColumn/dpi

将获得不重叠的条形,组之间只有一点额外的空间。您可以通过使height_of_group 大于或小于1 来调整组之间的间距。

【讨论】:

  • 非常感谢您的回复。一个问题:既然我是matplotlib的新用户,0.9是什么意思??
  • 另一个问题,这个操作对大量数据也有效吗?
  • height 的单位是轴单位。给定dat 中的条在这些单位中由1 分隔(鉴于您将它们与ind 绘制的方式)。因此,如果您想要组之间的间隙,则所有条形(在一个组中)的高度总和需要小于1height_of_group 设置总和,因此在这种情况下,Nth 组中的条形将以轴为单位从 N 变为 N+.9。这应该适用于任意数量的列和长度的数据。
  • 非常感谢!!!另一个问题:如果我有一个条形图,有什么区别(大约 0.9 值)??
  • 我不明白你的问题。
猜你喜欢
  • 2015-12-25
  • 2016-03-08
  • 1970-01-01
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 2020-08-18
相关资源
最近更新 更多