[英]Matplotlib double bar chart: Bars not properly drawn
I am trying to plot two different data columns of a frame ("n" and "m") for each data set as two differently colored bars next to each other. 我正在尝试将每个数据集的帧的两个不同数据列(“ n”和“ m”)绘制为彼此相邻的两个不同颜色的条。
def graphPlot(data, size=None):
pos = arange(len(data))+.5 # the bar centers on the y axis
figure(figsize=size)
xscale("log")
barh(pos, data["n"], align='center', height=0.25, color="darkgrey")
barh(pos - 0.25, data["m"], align='center', height=0.25, color="lightblue")
yticks(pos, data["graph"])
xlabel("")
grid(True)
But it turns out like this: 但是结果是这样的:
Why are the bars not properly drawn? 为什么没有正确绘制条形图? Are my position values wrong?
我的位置值有误吗?
It's your xscale("log")
call causing the problems. 是导致问题的
xscale("log")
调用。 You need the plots to be scaled so instead pass log=True
to the barh
calls: 您需要对图进行缩放,以便将
log=True
传递给barh
调用:
def graphPlot(data, size=None):
pos = arange(len(data))+.5 # the bar centers on the y axis
figure(figsize=size)
#xscale("log")
barh(pos, data["n"], align='center', height=0.25, color="darkgrey", log=True)
barh(pos - 0.25, data["m"], align='center', height=0.25, color="lightblue", log=True)
yticks(pos, data["graph"])
xlabel("")
grid(True)
An alternative is to use a pandas dataframe to hold your data and then simply call df[['ser1', 'ser2']].plot(kind='barh', logx=True)
. 另一种方法是使用熊猫数据框保存数据,然后简单地调用
df[['ser1', 'ser2']].plot(kind='barh', logx=True)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.