我是matplotlib的新用戶,我有一個圖表barh問題:重疊條。 當繪製圖表時,條形圖重疊,我沒有找到原因。在我看來,問題在於重新調整圖表的大小。我重新調整它的大小,因爲將來我會插入標題,圖例和x,y描述。我嘗試一些解決方案,但我有一個解決方案! 這是我的代碼:圖表barh matplotlib - 重疊條
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()
THANKS
的代碼是相同的,但我改變數據(列[]ë數據[]):
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()
的問題是我有可變數據,我必須找到一個穩定的算法。
你整理出來了嗎? – tacaswell