Python matplotlib条形图的最后一个条形图已填充一半

Python matplotlib条形图的最后一个条形图已填充一半,python,matplotlib,latex,pgf,Python,Matplotlib,Latex,Pgf,我正在使用matplotlib为我的论文创建一个图表。我正在使用以下代码: import numpy as np import numpy as np import matplotlib as mpl mpl.use('pgf') fig_width_pt = 390 # Get this from LaTeX using \showthe\columnwidth inches_per_pt = 1.0/72.27 # Convert pt to inch gol

我正在使用matplotlib为我的论文创建一个图表。我正在使用以下代码:

import numpy as np
import numpy as np
import matplotlib as mpl
mpl.use('pgf')

fig_width_pt = 390  # Get this from LaTeX using \showthe\columnwidth
inches_per_pt = 1.0/72.27               # Convert pt to inch
golden_mean = (np.sqrt(5)-1.0)/2.0         # Aesthetic ratio
fig_width = fig_width_pt*inches_per_pt  # width in inches
fig_height = fig_width*golden_mean      # height in inches
fig_size =  [fig_width,fig_height]

pgf_with_latex = {                      # setup matplotlib to use latex for output
    "pgf.texsystem": "pdflatex",        # change this if using xetex or lautex
    "text.usetex": True,                # use LaTeX to write all text
    "font.family": "serif",
    "font.serif": [],                   # blank entries should cause plots to inherit fonts from the document
    "font.sans-serif": [],
    "font.monospace": [],
    "axes.labelsize": 10,               # LaTeX default is 10pt font.
    "text.fontsize": 10,
    "legend.fontsize": 8,               # Make the legend/label fonts a little smaller
    "xtick.labelsize": 8,
    "ytick.labelsize": 8,
    "figure.figsize": fig_size,
    'axes.linewidth': .5,
    'lines.linewidth': .5,
    'patch.linewidth': .5,
    "pgf.preamble": [
        r"\usepackage[utf8x]{inputenc}",    # use utf8 fonts becasue your computer can handle it :)
        ]
    }
mpl.rcParams.update(pgf_with_latex)

import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import colorsys

def savefig(filename):
    plt.savefig('{}.pgf'.format(filename))
    plt.savefig('{}.pdf'.format(filename))

# setup
title = 'Clustering after salting out'
ylabel = '% of total colloids'
xlabel = 'Cluster size'
xticklabels = ('1','2','3','4','5','6','7','5+')
legend = ['10min', '20min','30min']

# read data from files
# skipped this part for legibility

# calculations with data, skipped for legibility

# plot it in a bar plot
N = len(ys[0])

ind = np.arange(0, N+1, 1.2)  # the x locations for the groups
width = 0.35       # the width of the bars

# generate colours
hsv_colours = [(x*1.0/N, 0.8, 0.8) for x in range(N)]
rgb_colours = map(lambda x: colorsys.hsv_to_rgb(*x), hsv_colours)

fig, ax = plt.subplots()
rects = [ax.bar([x+i*width for x in ind], y, width, color=rgb_colours[i], yerr=errors_percentage[i]) for i,y in enumerate(ys)]

# add some info
ax.set_ylabel(ylabel)
ax.set_xlabel(xlabel)
ax.set_title(title)
ax.set_xticks(ind+width)
ax.set_xticklabels(xticklabels)
ax.axis([0,7,0,60])
ax.legend(rects, legend)

savefig('tpm_cluster_statistics')
生成的输出如下所示:

如您所见,条形图的最后一个条形未完全填充。我需要一些其他的设置来让它工作吗? 目标是创建一个PGF文件以包含在LaTex文档中。PDF文件仅供预览。部分填充的栏位于PDF和PGF文件中。 非常感谢您的帮助

编辑: 作为对tcaswell的回复:这是您可以在计算机上尝试的最低限度的工作示例:

import numpy as np
import numpy as np
import matplotlib as mpl
mpl.use('pgf')

pgf_with_latex = {                      # setup matplotlib to use latex for output
    "pgf.texsystem": "pdflatex",        # change this if using xetex or lautex
    "text.usetex": True,                # use LaTeX to write all text
    }
mpl.rcParams.update(pgf_with_latex)

import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import colorsys

def savefig(filename):
    plt.savefig('{}.pgf'.format(filename))
    plt.savefig('{}.pdf'.format(filename))

# setup
title = 'Title'
ylabel = 'x'
xlabel = 'y'
xticklabels = ('1','2','3','4','5','6','7','8')
legend = ['1', '2','3']

#data
ys = [[51.63593099345628, 28.911362284354553, 12.135633551457465, 4.521118381915526, 1.189767995240928, 0.7138607971445567, 0.41641879833432477, 0.4759071980963712], [46.66359871145882, 21.445006902899216, 14.496088357109988, 7.363092498849516, 4.1417395306028535, 3.313391624482283, 0.0, 2.577082374597331], [52.642595499738356, 22.39665096807954, 12.087912087912088, 7.744636316064887, 2.3547880690737837, 1.5698587127158554, 0.3663003663003663, 0.837257980115123]]

# plot it in a bar plot
N = len(ys[0])

ind = np.arange(0, N+1, 1.2)  # the x locations for the groups
width = 0.35       # the width of the bars

fig, ax = plt.subplots()
rects = [ax.bar([x+i*width for x in ind], y, width) for i,y in enumerate(ys)]

# add some info
ax.set_ylabel(ylabel)
ax.set_xlabel(xlabel)
ax.set_title(title)
ax.set_xticks(ind+width)
ax.set_xticklabels(xticklabels)
ax.axis([0,7,0,60])
ax.legend(rects, legend)

savefig('tpm_cluster_statistics')
结果如下所示:

但当我删除这些行时:

mpl.use('pgf')

pgf_with_latex = {                      # setup matplotlib to use latex for output
    "pgf.texsystem": "pdflatex",        # change this if using xetex or lautex
    "text.usetex": True,                # use LaTeX to write all text
    }
mpl.rcParams.update(pgf_with_latex)

只需使用
plt.show()
显示输出,结果看起来确实正确。

我不知道真正的原因,但切换到没有
mpl的PDF输出。使用('pgf')
解决了这个问题。现在,我将坚持使用PDF,这也很好。谢谢你的帮助

看看如果添加一些虚拟数据来绘制一个以上的条形图会发生什么,除了零高度和无误差条形图。那么你的集群大小6 30分钟的酒吧有人吗?谢谢你的回答。我使用了以下数据:
data=[[old_-data0,0.],[old_-data1,0.],[old_-data2,0.]]和errors=[[old_-errors0,0.],[old_-errors1,0.],[old_-errors2,0.]
,但我看不出有什么不同……什么版本的mpl?您能否将此问题减少到复制此问题所需的最小代码量(合成数据、去掉标签+花哨的格式),这闻起来像是pgf后端中的一个错误。@tcaswell:我已经用一个最小的示例和导致问题的行数更新了此问题。我在Windows 7计算机上安装了matplotlib 1.3.1。