2
2
#
3
3
import matplotlib as mpl
4
4
import numpy
5
+ from matplotlib .backends import backend_pgf as mpl_backend_pgf
5
6
6
7
from . import color
7
8
@@ -41,9 +42,11 @@ def __init__(self, data, obj):
41
42
# get axes titles
42
43
xlabel = obj .get_xlabel ()
43
44
if xlabel :
45
+ xlabel = mpl_backend_pgf .common_texification (xlabel )
44
46
self .axis_options .append ("xlabel={{{}}}" .format (xlabel ))
45
47
ylabel = obj .get_ylabel ()
46
48
if ylabel :
49
+ ylabel = mpl_backend_pgf .common_texification (ylabel )
47
50
self .axis_options .append ("ylabel={{{}}}" .format (ylabel ))
48
51
49
52
# Axes limits.
@@ -57,8 +60,14 @@ def __init__(self, data, obj):
57
60
# axes scaling
58
61
if obj .get_xscale () == "log" :
59
62
self .axis_options .append ("xmode=log" )
63
+ self .axis_options .append (
64
+ "log basis x={{{}}}" .format (_try_f2i (obj .xaxis ._scale .base ))
65
+ )
60
66
if obj .get_yscale () == "log" :
61
67
self .axis_options .append ("ymode=log" )
68
+ self .axis_options .append (
69
+ "log basis y={{{}}}" .format (_try_f2i (obj .yaxis ._scale .base ))
70
+ )
62
71
63
72
if not obj .get_axisbelow ():
64
73
self .axis_options .append ("axis on top" )
@@ -539,6 +548,7 @@ def _get_ticks(data, xy, ticks, ticklabels):
539
548
# store the label anyway
540
549
label = ticklabel .get_text ()
541
550
if ticklabel .get_visible ():
551
+ label = mpl_backend_pgf .common_texification (label )
542
552
pgfplots_ticklabels .append (label )
543
553
else :
544
554
is_label_required = True
@@ -687,8 +697,11 @@ def _handle_linear_segmented_color_map(cmap):
687
697
# of 1pt, e.g., does most often not work.
688
698
unit = "pt"
689
699
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 )
692
705
693
706
color_changes = []
694
707
for (k , x ) in enumerate (X ):
@@ -743,11 +756,15 @@ def _handle_listed_color_map(cmap):
743
756
return (colormap_string , is_custom_colormap )
744
757
745
758
746
- def _scale_to_int (X ):
759
+ def _scale_to_int (X , max_val = None ):
747
760
"""
748
761
Scales the array X such that it contains only integers.
749
762
"""
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 ))
751
768
return [int (entry ) for entry in X ]
752
769
753
770
@@ -799,3 +816,11 @@ def _find_associated_colorbar(obj):
799
816
# reference to axis containing colorbar)
800
817
return cbar
801
818
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
0 commit comments