Skip to content

Commit cc298cf

Browse files
author
Thomas Wagner
committed
2 parents de24151 + c893417 commit cc298cf

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

matplotlib2tikz/axes.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
import matplotlib as mpl
44
import numpy
5+
from matplotlib.backends import backend_pgf as mpl_backend_pgf
56

67
from . import color
78

@@ -41,9 +42,11 @@ def __init__(self, data, obj):
4142
# get axes titles
4243
xlabel = obj.get_xlabel()
4344
if xlabel:
45+
xlabel = mpl_backend_pgf.common_texification(xlabel)
4446
self.axis_options.append("xlabel={{{}}}".format(xlabel))
4547
ylabel = obj.get_ylabel()
4648
if ylabel:
49+
ylabel = mpl_backend_pgf.common_texification(ylabel)
4750
self.axis_options.append("ylabel={{{}}}".format(ylabel))
4851

4952
# Axes limits.
@@ -57,8 +60,14 @@ def __init__(self, data, obj):
5760
# axes scaling
5861
if obj.get_xscale() == "log":
5962
self.axis_options.append("xmode=log")
63+
self.axis_options.append(
64+
"log basis x={{{}}}".format(_try_f2i(obj.xaxis._scale.base))
65+
)
6066
if obj.get_yscale() == "log":
6167
self.axis_options.append("ymode=log")
68+
self.axis_options.append(
69+
"log basis y={{{}}}".format(_try_f2i(obj.yaxis._scale.base))
70+
)
6271

6372
if not obj.get_axisbelow():
6473
self.axis_options.append("axis on top")
@@ -539,6 +548,7 @@ def _get_ticks(data, xy, ticks, ticklabels):
539548
# store the label anyway
540549
label = ticklabel.get_text()
541550
if ticklabel.get_visible():
551+
label = mpl_backend_pgf.common_texification(label)
542552
pgfplots_ticklabels.append(label)
543553
else:
544554
is_label_required = True
@@ -687,8 +697,11 @@ def _handle_linear_segmented_color_map(cmap):
687697
# of 1pt, e.g., does most often not work.
688698
unit = "pt"
689699

690-
# Scale to integer
691-
X = _scale_to_int(numpy.array(X))
700+
# Scale to integer (too high integers will firstly be slow and secondly may
701+
# produce dimension errors or memory errors in latex)
702+
# 0-1000 is the internal granularity of PGFplots.
703+
# 16300 was the maximum value for pgfplots<=1.13
704+
X = _scale_to_int(numpy.array(X), 1000)
692705

693706
color_changes = []
694707
for (k, x) in enumerate(X):
@@ -743,11 +756,15 @@ def _handle_listed_color_map(cmap):
743756
return (colormap_string, is_custom_colormap)
744757

745758

746-
def _scale_to_int(X):
759+
def _scale_to_int(X, max_val=None):
747760
"""
748761
Scales the array X such that it contains only integers.
749762
"""
750-
X = X / _gcd_array(X)
763+
764+
if max_val is None:
765+
X = X / _gcd_array(X)
766+
else:
767+
X = X / max(1 / max_val, _gcd_array(X))
751768
return [int(entry) for entry in X]
752769

753770

@@ -799,3 +816,11 @@ def _find_associated_colorbar(obj):
799816
# reference to axis containing colorbar)
800817
return cbar
801818
return None
819+
820+
821+
def _try_f2i(x):
822+
"""Convert losslessly float to int if possible.
823+
Used for log base: if not used, base for log scale can be "10.0" (and then
824+
printed as such by pgfplots).
825+
"""
826+
return int(x) if int(x) == x else x

test/test_logplot_base.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
import helpers
4+
5+
6+
def plot():
7+
from matplotlib import pyplot as plt
8+
9+
a = [pow(10, i) for i in range(10)]
10+
fig = plt.figure()
11+
ax = fig.add_subplot(1, 1, 1)
12+
ax.semilogy(a, color="blue", lw=0.25, basey=2)
13+
14+
plt.grid(b=True, which="major", color="g", linestyle="-", linewidth=0.25)
15+
plt.grid(b=True, which="minor", color="r", linestyle="--", linewidth=0.5)
16+
return fig
17+
18+
19+
def test():
20+
phash = helpers.Phash(plot())
21+
assert phash.phash == "e9e15ee95eaa9480", phash.get_details()
22+
return

0 commit comments

Comments
 (0)