簡體   English   中英

圖表 barh matplotlib - 重疊條

[英]Chart barh matplotlib - overlap bars

我是 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()

問題是我有可變數據,我必須找到一個穩定的算法。

我認為您誤解了高度( 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM