【发布时间】:2017-12-31 07:37:02
【问题描述】:
使用 matplotlib 2.0.2、python2.7、Win7,64bit 创建直方图时,在创建的 pdf 和 png 中都可见,我在 bin 之间出现垂直条纹。 我正在使用带有乳胶的 pgf 来创建一个 PDF,我将在 pdflatex 文档中由 includegraphics 使用它。创建的 PNG 只是快速检查。
在 Matplotlib 1.5.3 中并非如此。 如何消除这些分隔各个垃圾箱的白线?
尝试过的事情:
- 打开/关闭抗锯齿(在 hist 命令中,aa=True/False)
- 画一条线(ls="-"/ls="none" in hist 命令)
- 一种可行的方法是为 bin 指定宽度 (width=2.3),但这也不适用于所有缩放值的 PDF。
生成图像的代码
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
"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.
"font.size": 8,
"legend.fontsize": 7, # Make the legend/label fonts a little smaller
"xtick.labelsize": 7,
"ytick.labelsize": 7,
"pgf.preamble": [
r"\usepackage[utf8x]{inputenc}", # use utf8 fonts becasue your computer can handle it :)
r"\usepackage[T1]{fontenc}", # plots will be generated using this preamble
r"\usepackage{siunitx}",
r"\DeclareSIUnit[number-unit-product = {}] ",
r"\LSB{LSB}",
]
}
mpl.rcParams.update(pgf_with_latex)
import matplotlib.pyplot as pl
import numpy as np
fig=pl.figure(figsize=(3,2))
ax1 = fig.add_subplot(111)
dat=np.random.normal(-120-60,40,200000).astype(int)
bins=np.arange(int(np.amin(dat))-.5,127.5,2)
ax1.hist(dat, bins = bins, stacked = True)
ax1.set_title("\\emph{(a)} minimal example")
ax1.set_yscale("log", nonposy="clip")
ax1.set_ylim(0.8, 20000)
ax1.set_xlim(None, 130)
ax1.set_ylabel("frequency")
ax1.set_xlabel("data")
ax1.set_xticks([-300,-200, -127,0,127])
fig.tight_layout(h_pad=1,w_pad=0.2)
pl.savefig('test.png', bbox_inches='tight',dpi=600)
pl.savefig('test.pdf', bbox_inches='tight',dpi=600)
以上代码的输出:
【问题讨论】:
-
为了运行我需要删除
"pgf.preamble"的代码(缺少一些包)。这样做时,pdf 输出没有任何白色条纹。见screenshot。 (使用matplotlib 2.0.2, python2.7, Win8,64bit) -
我检查过:使用 Foxit Reader,我也看不到条纹,但使用 Adobe Reader DC (2017) 或 firefox PDF 插件我看到了它们。删除“pgf.preamble”不会改变行为。
标签: python python-2.7 matplotlib pgf