[英]Chart barh matplotlib - overlap bars
I'm new user of matplotlib and I have a problem with chart barh: overlap bars.我是 matplotlib 的新用户,我有图表 barh 的问题:重叠条。 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.
我重新调整了它的大小,因为将来我将插入标题、图例和 x,y 描述。 I try some solution, but I have one solution!!
我尝试了一些解决方案,但我有一个解决方案!! This is my code:
这是我的代码:
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谢谢
The code is the same, but I change the data ( columns[]
e data[]
):代码相同,但我更改了数据(
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()
The problem is that I have variable data and I have to find a stable algorithm.问题是我有可变数据,我必须找到一个稳定的算法。
I think you are mis-understanding the meaning of height ( doc ).我认为您误解了高度( doc )的含义。 The units are in axis units, not pixels.
单位是轴单位,而不是像素。
heightColumn = height*1.0 / (rdata) / (len(columns))
heightColumn = heightColumn/dpi
to到
height_of_group = .9
heightColumn = height_of_group / (len(data))
#heightColumn = heightColumn/dpi
will get non-overlapping bars with just a bit of extra space between the groups.将得到不重叠的条形,组之间只有一点额外的空间。 You can adjust the space between the groups by making
height_of_group
larger or smaller so long as it is less than 1
.您可以通过使
height_of_group
更大或更小来调整组之间的空间,只要它小于1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.