I'm new user of matplotlib and I have a problem with chart barh: overlap bars. When plot the graph, the bars draws overlapped and I haven't found the reason. In my opinion the problem is on re-size the graph. I re-size it, because in the future I will insert title, legend and x,y description. I try some solution, but I have one solution!! This is my code:
import matplotlib.pyplot as pltimport matplotlib.font_manager as fmfrom matplotlib.colors import LightSourcefrom matplotlib.artist import Artistimport numpy as npfrom decimal import *import datetime#Sizewidth = 360height = 240dpi = 80.0colors = ['#be1e2d','#666699','#92d5ea','#ee8310','#8d10ee','#5a3b16','#26a4ed','#f45a90','#e9e744']#Datacolumns = ['2005','2006']data = [[2.6,3.5],[2, 1.5]]linewidth = 1N = len(columns)ind = np.arange(N) #Re-Sizerdata = len(data) if columns is None else len(columns)heightColumn = height*1.0 / (rdata) / (len(columns))heightColumn = heightColumn/dpifig = plt.figure(1, figsize=(width/dpi,height/dpi),facecolor='w')ax = fig.add_axes([0.2, 0.3, 0.6, 0.5])#DrawtupleRects = ()idxColor = 0 valPositionCol = indfor dat in data:rects = plt.barh(valPositionCol, dat, heightColumn, color=colors[idxColor], alpha=0.8, linewidth=linewidth)valPositionCol=valPositionCol+heightColumnidxColor += 1if idxColor==9:idxColor = 0tupleRects += (rects,)plt.show()
THANKS
The code is the same, but I change the data (columns[]
e data[]
):
import matplotlib.pyplot as plt
import matplotlib.font_manager as fmfrom 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 = .9heightColumn = 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])
#DrawtupleRects = ()
idxColor = 0
valPositionCol = ind
for dat in data:rects = plt.barh(valPositionCol, dat, heightColumn, color=colors[idxColor], alpha=0.8, linewidth=linewidth)valPositionCol=valPositionCol+heightColumnidxColor += 1if idxColor==9:idxColor = 0tupleRects += (rects,)
plt.show()
The problem is that I have variable data and I have to find a stable algorithm.