Bar Matplotlib 2.0 in a bar chart

I get vertical stripes between the bins when creating a histogram with matplotlib 2.0.2, python2.7, Win7,64bit, visible both in pdf and png. I use pgf with latex to create a PDF file that I will use by including the graphics in the pdflatex document. The generated PNG is just a quick check.

This was not so in Matplotlib 1.5.3. How to get rid of these white lines separating individual cells?

Verified things:

  • Turn on / off smoothing on (aa = True / False in the hist command)
  • line drawing (ls = "-" / ls = "none" in the hist command)
  • One thing that seems to give the width for the bins (width = 2.3), but this also does not work for PDF in all scaling values.

Image Creation Code

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) 

The output of the above code:
output of the above code

+5
source share
1 answer

1. Do not use pgf backend

As @unutbu noted in his (unfortunately, now deleted) answer, using the pgf backend will not actually lead to the expected schedule.

Delete row

 mpl.use('pgf') 

will give

enter image description here

2. Step function

If for some reason the use of the pgf backend cannot be avoided, a workaround may be to use the step function to plot the histogram. Extract ax1.hist(...) from code and replace it with

 hist, ex = np.histogram(dat, bins = bins) ax1.fill_between(bins[:-1], hist, lw=0.0, step="post") 

gives

enter image description here

+2
source

Source: https://habr.com/ru/post/1270194/


All Articles