简体   繁体   English

matplotlib barh在条形之间产生不稳定的间距

[英]matplotlib barh produces wonky spacing between bars

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])

My data is stored in sorted_composite and ind are the values I'm using to place the bars (the ytick locations). 我的数据存储在sorted_composite中,而ind是我用来放置条形的值(ytick位置)。 I'm using linspace to produce evenly spaced bars and this only kind of works and I'm not sure exactly why. 我正在使用linspace来生成均匀间隔的条形图,这只是一种工作,我不确定为什么。

like user1127062 suggests, it might be that your code is just fine. 像user1127062建议的那样,你的代码可能就好了。

If you don't need the plot to be interactive, save it as an svg 如果您不需要绘图是交互式的,请将其另存为svg

If you run: 如果您运行:

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

you'll see the pixel aliasing (in the bar widths) in the figure window, but it's gone in the svg file. 你会在图窗口中看到像素别名(在条形宽度中),但它已经在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. 如果svg与你正在做的事情不兼容,那么“cairo”后端似乎可以最好地保存png文件。 They look as good as a screenshot of the svg. 它们看起来和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" doesn't support show() , so you can't use it in interactive mode, or to show a plot directly from a program. raw“cairo”不支持show() ,因此您无法在交互模式下使用它,或直接从程序中显示绘图。

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 ) “GTKCairo”后端具有两全其美但未在默认安装中启用(至少不是我使用sudo apt-get install matplotlib

If you're using Ubuntu I think all you need to do to get it working is to install gtk, and recompile matplotlib: 如果您正在使用Ubuntu,我认为您需要做的就是安装gtk,并重新编译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()

You can automatically load your favorite backend by hunting down your matplotlibrc file, I found mine in: 您可以通过搜索matplotlibrc文件自动加载您喜欢的后端,我发现我的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM