图表 barh matplotlib - 重叠条 [英] Chart barh matplotlib - overlap bars
问题描述
我是 matplotlib 的新用户,我在使用图表条时遇到问题:重叠条.绘制图形时,绘制的条形重叠,我还没有找到原因.我认为问题在于调整图表大小.我重新调整了它的大小,因为将来我将插入标题、图例和 x,y 描述.我尝试了一些解决方案,但我有一个解决方案!!这是我的代码:
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 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 []
):
The code is the same, but I change the 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). The units are in axis units, not pixels.
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
.
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
.
这篇关于图表 barh matplotlib - 重叠条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!