matplotlib barh在条之间产生不规则的间距 [英] matplotlib barh produces wonky spacing between bars

查看:70
本文介绍了matplotlib barh在条之间产生不规则的间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在生成如下所示的条形图:

I've been generating bar charts that look like this:

请注意,由于某些原因,标签上的垂直间距不均匀;我不确定这是否与我如何分配刻度线或实际上放置文本的机制有关.相关代码:

Notice that the vertical spacing on the labels is uneven for some reason; I'm not sure if this has to do with how I have assigned the ticks or whatever mechanism is actually placing the text. Relevant code:

height_factor = 40.0
ind = np.linspace(0,len(sorted_totals)*height_factor,num=len(sorted_totals))
width = 0.25
fig = plt.figure(figsize=(15.5, 8.75),dpi=300)
p1 = plt.barh(ind,map(int,sorted_composite[:,0]),color='blue',align='center',height=height_factor)
p1 = plt.barh(ind,map(int,sorted_composite[:,2]),color=(0.75,0.1,0.1),align='center',height=height_factor)
plt.ylabel('# of Picks (blue) + # of Bans (red)')
plt.yticks(ind, sorted_totals[:,0])
plt.subplots_adjust(bottom=0.05, left=0.14,right=0.95,top=0.95)
plt.ylim([ind.min() - height_factor, ind.max() + height_factor])

我的数据存储在sorted_composite中,并且ind是我用来放置小节(ytick位置)的值.我正在使用linspace生成间距均匀的条形图,这仅是一种作品,我不确定确切的原因.

My data is stored in sorted_composite and ind are the values I'm using to place the bars (the ytick locations). I'm using linspace to produce evenly spaced bars and this only kind of works and I'm not sure exactly why.

推荐答案

就像user1127062建议的那样,可能是您的代码很好.

like user1127062 suggests, it might be that your code is just fine.

如果您不需要绘图具有交互性,请将其另存为 svg

If you don't need the plot to be interactive, save it as an svg

如果您运行:

data = numpy.random.randn(10000)
pylab.hist(data,300)
pylab.savefig(fileName+'.svg',format='svg')

您将在图形窗口中看到像素别名(在条形宽度中),但在svg文件中消失了.

you'll see the pixel aliasing (in the bar widths) in the figure window, but it's gone in the svg file.

如果 svg 与您正在执行的操作不兼容,cairo"后端似乎在保存 png 文件方面做得最好.它们看起来与svg的屏幕截图一样好.

The "cairo" backend seems to do the best job of saving png files, if svg's aren't compatible with what you're doing. They look as good as a screenshot of the svg.

可以通过运行切换后端.

You can switch the backend by running.

import matplotlib
# you have to change the backend before importing pylab
matplotlib.use('cairo') 
import pylab

raw "cairo" 不支持 show(),所以你不能在交互模式下使用它,或者直接从程序中显示绘图.

raw "cairo" doesn't support show(), so you can't use it in interactive mode, or to show a plot directly from a program.

GTKCairo"后端具有两全其美的优点,但在默认安装中未启用(至少在我使用 sudo apt-get install matplotlib 获得的那个中未启用)

The "GTKCairo" backend has the best of both worlds but isn't enabled in the default installation (at least not in the one I got with sudo apt-get install matplotlib)

如果你使用的是 Ubuntu,我认为你需要做的就是安装 gtk,然后重新编译 matplotlib:

If you're using Ubuntu I think all you need to do to get it working is to install gtk, and recompile matplotlib:

sudo apt-get install git-core python-gtk2-dev
git clone git://github.com/matplotlib/matplotlib.git
cd matplotlib
sudo python setup.py install

您可以通过以下方式检查哪个后端处于活动状态:

You can check which backend is active with:

matplotlib.get_backend()

你可以通过搜索你的 matplotlibrc 文件来自动加载你最喜欢的后端,我在:

You can automatically load your favorite backend by hunting down your matplotlibrc file, I found mine in:

/usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/matplotlibrc

这篇关于matplotlib barh在条之间产生不规则的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆