tikzplotlib icon indicating copy to clipboard operation
tikzplotlib copied to clipboard

histogram with log scale has display issues for small values

Open maflcko opened this issue 8 years ago • 3 comments
trafficstars

It seems matplotlib can not properly render histograms, when log scale is active for the y-axis and the absolute frequency is smaller than ~2.7, ie. 1 and 2.

The Python code:

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib2tikz import save as tikz_save

plot_values = pd.DataFrame(
    {
        'test0': pd.Series([0, 0, 0, .1, .1, .2]),
        'test1': pd.Series([0, .1]),
    }
)
plot_values.plot.hist(stacked=False)  # Should not use stacked for log plot
plt.gcf().set_size_inches(4, 3)
plt.yscale('log')
plt.ylim(ymin=.1)

plt.savefig('/tmp/test_a.png')
tikz_save('/tmp/test_a.tikz')

The png looks fine:

test_a

However, from the pdf it seems histogram bars are plotted with their lower bound starting at ~2.7 (instead of -inf). This will especially cause issues when the frequency is 1 or 2. test_a pdf

maflcko avatar Jul 02 '17 09:07 maflcko

Can you cook up an example code without pandas? It's such a heavy dependency...

nschloe avatar Mar 21 '19 12:03 nschloe

I have a similar issue with my histogram while using a log scale on the y axis. The matplotlib version looks fine.

The .tex version has a similar problem as MarcoFalke described.

Matplotlib PNG Version: boundingbox_height

.tex Version after compiling in latex: image

derbach avatar Aug 16 '21 18:08 derbach

I have the same issue as @derbach. I solved it by manually replacing values in the tikz output file. The lower value for plotting a bar was set to 0, and I replaced it by 0.01. A bit dirty but it was enough in my case. e.g., \draw[draw=white,fill=color0,line width=0.32pt] (axis cs:1,0.01) rectangle (axis cs:2,13357);

allglc avatar Jan 25 '24 13:01 allglc