Elb*_*t85 6 python matplotlib pgf python-2.7
当使用matplotlib 2.0.2,python2.7,Win7,64bit创建直方图时,我在bin之间得到垂直条纹,在pdf和png中都可见.我使用latex来创建PDF,我将在pdflatex文档中使用includegraphics.创建的PNG只是一个快速检查.
在Matplotlib 1.5.3中并非如此.如何摆脱分隔各个箱子的这些白线?
事情尝试:
用于生成图像的代码
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)
Run Code Online (Sandbox Code Playgroud)
输出上面的代码:

正如 @unutbu 在他的(不幸的是现在被删除)答案中指出的那样,不使用 pgf 后端实际上会产生预期的情节。
拆除线路
mpl.use('pgf')
Run Code Online (Sandbox Code Playgroud)
会给
如果由于某种原因无法避免使用 pgf 后端,解决方法可能是使用阶跃函数来绘制直方图。ax1.hist(...)从代码中删除并将其替换为
hist, ex = np.histogram(dat, bins = bins)
ax1.fill_between(bins[:-1], hist, lw=0.0, step="post")
Run Code Online (Sandbox Code Playgroud)
给出
| 归档时间: |
|
| 查看次数: |
366 次 |
| 最近记录: |