Skip to content

Commit 1264bca

Browse files
authored
Merge pull request #172 from nschloe/pylint
Pylint
2 parents fc922db + a9697cf commit 1264bca

27 files changed

+366
-301
lines changed

.pylintrc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[MESSAGES CONTROL]
2+
3+
disable=
4+
bad-continuation,
5+
duplicate-code,
6+
fixme,
7+
invalid-name,
8+
locally-disabled,
9+
missing-docstring,
10+
no-member,
11+
too-few-public-methods,
12+
too-many-branches,
13+
too-many-statements,
14+
too-many-arguments,
15+
too-many-locals

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ addons:
3131
- python-tk
3232

3333
before_install:
34-
- pip install pytest pytest-cov code_extract
34+
- pip install pytest pytest-cov code_extract pylint
3535
# Make sure to put the readme test at the very end; otherwise it's messing
3636
# around with the other tests.
3737
- code_extract README.md test/zzz_readme_test.py --filter python,test
@@ -51,6 +51,8 @@ before_script:
5151

5252
# run tests
5353
script:
54+
- pylint matplotlib2tikz/
55+
- pylint test/*py
5456
# cd into test directory to make sure we're using the pip-installed
5557
# matplotlib2tikz.
5658
- cd test && MPLBACKEND=Agg pytest --cov matplotlib2tikz

matplotlib2tikz/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
__copyright__ = 'Copyright (c) 2010-2017, %s <%s>' % (__author__, __email__)
66
__credits__ = []
77
__license__ = 'License :: OSI Approved :: MIT License'
8-
__version__ = '0.6.8'
8+
__version__ = '0.6.9'
99
__maintainer__ = u'Nico Schlömer'
1010
__status__ = 'Development Status :: 5 - Production/Stable'

matplotlib2tikz/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
'''Script to convert Matplotlib generated figures into TikZ/PGFPlots figures.
44
'''
5+
from __future__ import print_function
56

67
from matplotlib2tikz.__about__ import (
78
__author__,

matplotlib2tikz/axes.py

Lines changed: 123 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# -*- coding: utf-8 -*-
22
#
3-
from . import color
4-
53
import matplotlib as mpl
64
import numpy
75

6+
from . import color
7+
88

99
class Axes(object):
1010
def __init__(self, data, obj):
@@ -174,6 +174,7 @@ def __init__(self, data, obj):
174174
# Unfortunately, _tickdir doesn't seem to be quite accurate. See
175175
# <https://github.com/matplotlib/matplotlib/issues/5311>.
176176
# For now, just take the first tick direction of each of the axes.
177+
# pylint: disable=protected-access
177178
x_tick_dirs = [tick._tickdir for tick in obj.xaxis.get_major_ticks()]
178179
y_tick_dirs = [tick._tickdir for tick in obj.yaxis.get_major_ticks()]
179180
if x_tick_dirs or y_tick_dirs:
@@ -200,20 +201,18 @@ def __init__(self, data, obj):
200201

201202
# Set each rotation for every label
202203
x_tick_rotation_and_horizontal_alignment = \
203-
self.__get_label_rotation_and_horizontal_alignment(obj, data, 'x')
204+
_get_label_rotation_and_horizontal_alignment(obj, data, 'x')
204205
if x_tick_rotation_and_horizontal_alignment:
205206
self.axis_options.append(x_tick_rotation_and_horizontal_alignment)
206207

207208
y_tick_rotation_and_horizontal_alignment = \
208-
self.__get_label_rotation_and_horizontal_alignment(obj, data, 'y')
209+
_get_label_rotation_and_horizontal_alignment(obj, data, 'y')
209210
if y_tick_rotation_and_horizontal_alignment:
210211
self.axis_options.append(y_tick_rotation_and_horizontal_alignment)
211212

212213
# Set tick position
213-
x_tick_position_string, x_tick_position = \
214-
self.__get_tick_position(obj, data, 'x')
215-
y_tick_position_string, y_tick_position = \
216-
self.__get_tick_position(obj, data, 'y')
214+
x_tick_position_string, x_tick_position = _get_tick_position(obj, 'x')
215+
y_tick_position_string, y_tick_position = _get_tick_position(obj, 'y')
217216

218217
if x_tick_position == y_tick_position and x_tick_position is not None:
219218
self.axis_options.append("tick pos=%s" % x_tick_position)
@@ -225,6 +224,7 @@ def __init__(self, data, obj):
225224
# <http://sourceforge.net/p/matplotlib/mailman/message/25169234/>
226225
# Coordinate of the lines are entirely meaningless, but styles
227226
# (colors,...) are respected.
227+
# pylint: disable=protected-access
228228
if obj.xaxis._gridOnMajor:
229229
self.axis_options.append('xmajorgrids')
230230
elif obj.xaxis._gridOnMinor:
@@ -237,6 +237,7 @@ def __init__(self, data, obj):
237237
if col != 'black':
238238
self.axis_options.append('x grid style={%s}' % col)
239239

240+
# pylint: disable=protected-access
240241
if obj.yaxis._gridOnMajor:
241242
self.axis_options.append('ymajorgrids')
242243
elif obj.yaxis._gridOnMinor:
@@ -386,128 +387,138 @@ def __init__(self, data, obj):
386387

387388
return
388389

389-
def __get_label_rotation_and_horizontal_alignment(self, obj, data, axes):
390-
tick_label_text_width = None
391-
tick_label_text_width_identifier = "%s tick label text width" % axes
392-
if tick_label_text_width_identifier in data['extra axis options']:
393-
tick_label_text_width = data['extra axis options [base]'][
394-
tick_label_text_width_identifier
395-
]
396-
del data['extra axis options'][tick_label_text_width_identifier]
390+
def get_begin_code(self):
391+
content = self.content
392+
if self.axis_options:
393+
content.append('[\n' + ',\n'.join(self.axis_options) + '\n]\n')
394+
return content
395+
396+
def get_end_code(self, data):
397+
if not self.is_subplot:
398+
return '\\end{axis}\n\n'
399+
elif self.is_subplot and self.nsubplots == self.subplot_index:
400+
data['is_in_groupplot_env'] = False
401+
return '\\end{groupplot}\n\n'
402+
403+
return ''
397404

398-
label_style = ""
399405

400-
major_tick_labels = obj.xaxis.get_majorticklabels() if axes == 'x' \
401-
else obj.yaxis.get_majorticklabels()
406+
def _get_label_rotation_and_horizontal_alignment(
407+
obj, data, axes_obj
408+
):
409+
tick_label_text_width = None
410+
tick_label_text_width_identifier = \
411+
'%s tick label text width' % axes_obj
412+
if tick_label_text_width_identifier in data['extra axis options']:
413+
tick_label_text_width = data['extra axis options [base]'][
414+
tick_label_text_width_identifier
415+
]
416+
del data['extra axis options'][tick_label_text_width_identifier]
402417

403-
if len(major_tick_labels) == 0:
404-
return None
418+
label_style = ""
405419

406-
tick_labels_rotation = \
407-
[label.get_rotation() for label in major_tick_labels]
408-
tick_labels_rotation_same_value = len(set(tick_labels_rotation)) == 1
420+
major_tick_labels = \
421+
obj.xaxis.get_majorticklabels() if axes_obj == 'x' \
422+
else obj.yaxis.get_majorticklabels()
409423

410-
tick_labels_horizontal_alignment = \
411-
[label.get_horizontalalignment() for label in major_tick_labels]
412-
tick_labels_horizontal_alignment_same_value = \
413-
len(set(tick_labels_horizontal_alignment)) == 1
424+
if not major_tick_labels:
425+
return None
414426

415-
if tick_labels_rotation_same_value and \
416-
tick_labels_horizontal_alignment_same_value:
417-
values = []
427+
tick_labels_rotation = \
428+
[label.get_rotation() for label in major_tick_labels]
429+
tick_labels_rotation_same_value = len(set(tick_labels_rotation)) == 1
418430

419-
if any(tick_labels_rotation) != 0:
420-
values.append('rotate=%d' % tick_labels_rotation[0])
431+
tick_labels_horizontal_alignment = \
432+
[label.get_horizontalalignment() for label in major_tick_labels]
433+
tick_labels_horizontal_alignment_same_value = \
434+
len(set(tick_labels_horizontal_alignment)) == 1
421435

422-
if tick_label_text_width:
423-
values.append('align=%s' % tick_labels_horizontal_alignment[0])
424-
values.append('text width=%s' % tick_label_text_width)
425-
else:
426-
print('Horizontal alignment will be ignored as no \'%s tick '
427-
'label text width\' has been passed in the \'extra\' '
428-
'parameter' % axes)
436+
if tick_labels_rotation_same_value and \
437+
tick_labels_horizontal_alignment_same_value:
438+
values = []
439+
440+
if any(tick_labels_rotation) != 0:
441+
values.append('rotate=%d' % tick_labels_rotation[0])
429442

430-
if len(values) > 0:
431-
label_style = \
432-
'%sticklabel style = {%s}' % (axes, ','.join(values))
443+
if tick_label_text_width:
444+
values.append('align=%s' % tick_labels_horizontal_alignment[0])
445+
values.append('text width=%s' % tick_label_text_width)
433446
else:
434-
values = []
447+
print('Horizontal alignment will be ignored as no \'%s tick '
448+
'label text width\' has been passed in the \'extra\' '
449+
'parameter' % axes_obj)
435450

436-
if tick_labels_rotation_same_value:
437-
values.append('rotate=%d' % tick_labels_rotation[0])
438-
else:
439-
values.append('rotate={%s,0}[\\ticknum]'
440-
% ','.join(str(x) for x in tick_labels_rotation))
441-
442-
if tick_label_text_width:
443-
if tick_labels_horizontal_alignment_same_value:
444-
values.append('align=%s' %
445-
tick_labels_horizontal_alignment[0])
446-
values.append('text width=%s' % tick_label_text_width)
447-
else:
448-
for idx, x in enumerate(tick_labels_horizontal_alignment):
449-
label_style += '%s_tick_label_ha_%d/.initial = %s' \
450-
% (axes, idx, x)
451-
452-
values.append(
453-
'align=\pgfkeysvalueof{/pgfplots/'
454-
'%s_tick_label_ha_\\ticknum}' % axes)
455-
values.append('text width=%s' % tick_label_text_width)
451+
if values:
452+
label_style = \
453+
'%sticklabel style = {%s}' % (axes_obj, ','.join(values))
454+
else:
455+
values = []
456+
457+
if tick_labels_rotation_same_value:
458+
values.append('rotate=%d' % tick_labels_rotation[0])
459+
else:
460+
values.append('rotate={%s,0}[\\ticknum]'
461+
% ','.join(str(x) for x in tick_labels_rotation))
462+
463+
if tick_label_text_width:
464+
if tick_labels_horizontal_alignment_same_value:
465+
values.append('align=%s' %
466+
tick_labels_horizontal_alignment[0])
467+
values.append('text width=%s' % tick_label_text_width)
456468
else:
457-
print('Horizontal alignment will be ignored as no \'%s tick '
458-
'label text width\' has been passed in the \'extra\' '
459-
'parameter' % axes)
469+
for idx, x in enumerate(tick_labels_horizontal_alignment):
470+
label_style += '%s_tick_label_ha_%d/.initial = %s' \
471+
% (axes_obj, idx, x)
460472

461-
label_style = 'every %s tick label/.style = {\n' \
462-
'%s\n' \
463-
'}' % (axes, ',\n'.join(values))
473+
values.append(
474+
'align=\\pgfkeysvalueof{/pgfplots/'
475+
'%s_tick_label_ha_\\ticknum}' % axes_obj
476+
)
477+
values.append('text width=%s' % tick_label_text_width)
478+
else:
479+
print(
480+
'Horizontal alignment will be ignored as no \'%s tick '
481+
'label text width\' has been passed in the \'extra\' '
482+
'parameter' % axes_obj
483+
)
464484

465-
return label_style
485+
label_style = 'every %s tick label/.style = {\n' \
486+
'%s\n' \
487+
'}' % (axes_obj, ',\n'.join(values))
466488

467-
def __get_tick_position(self, obj, data, axes):
468-
major_ticks = obj.xaxis.majorTicks if axes == 'x' else \
469-
obj.yaxis.majorTicks
489+
return label_style
470490

471-
major_ticks_bottom = [tick.tick1On for tick in major_ticks]
472-
major_ticks_top = [tick.tick2On for tick in major_ticks]
473491

474-
major_ticks_bottom_show_all = False
475-
if len(set(major_ticks_bottom)) == 1 and major_ticks_bottom[0] is True:
476-
major_ticks_bottom_show_all = True
492+
def _get_tick_position(obj, axes_obj):
493+
major_ticks = obj.xaxis.majorTicks if axes_obj == 'x' else \
494+
obj.yaxis.majorTicks
477495

478-
major_ticks_top_show_all = False
479-
if len(set(major_ticks_top)) == 1 and major_ticks_top[0] is True:
480-
major_ticks_top_show_all = True
496+
major_ticks_bottom = [tick.tick1On for tick in major_ticks]
497+
major_ticks_top = [tick.tick2On for tick in major_ticks]
481498

482-
major_ticks_position = None
483-
if not major_ticks_bottom_show_all and not major_ticks_top_show_all:
484-
position_string = "%smajorticks=false" % axes
485-
elif major_ticks_bottom_show_all and major_ticks_top_show_all:
486-
major_ticks_position = 'both'
487-
elif major_ticks_bottom_show_all:
488-
major_ticks_position = 'left'
489-
elif major_ticks_top_show_all:
490-
major_ticks_position = 'right'
499+
major_ticks_bottom_show_all = False
500+
if len(set(major_ticks_bottom)) == 1 and major_ticks_bottom[0] is True:
501+
major_ticks_bottom_show_all = True
491502

492-
if major_ticks_position:
493-
position_string = "%stick pos=%s" % (axes, major_ticks_position)
503+
major_ticks_top_show_all = False
504+
if len(set(major_ticks_top)) == 1 and major_ticks_top[0] is True:
505+
major_ticks_top_show_all = True
494506

495-
return position_string, major_ticks_position
507+
major_ticks_position = None
508+
if not major_ticks_bottom_show_all and not major_ticks_top_show_all:
509+
position_string = "%smajorticks=false" % axes_obj
510+
elif major_ticks_bottom_show_all and major_ticks_top_show_all:
511+
major_ticks_position = 'both'
512+
elif major_ticks_bottom_show_all:
513+
major_ticks_position = 'left'
514+
elif major_ticks_top_show_all:
515+
major_ticks_position = 'right'
496516

497-
def get_begin_code(self):
498-
content = self.content
499-
if self.axis_options:
500-
content.append('[\n' + ',\n'.join(self.axis_options) + '\n]\n')
501-
return content
517+
if major_ticks_position:
518+
position_string = \
519+
"%stick pos=%s" % (axes_obj, major_ticks_position)
502520

503-
def get_end_code(self, data):
504-
if not self.is_subplot:
505-
return '\\end{axis}\n\n'
506-
elif self.is_subplot and self.nsubplots == self.subplot_index:
507-
data['is_in_groupplot_env'] = False
508-
return '\\end{groupplot}\n\n'
509-
else:
510-
return ''
521+
return position_string, major_ticks_position
511522

512523

513524
def _get_ticks(data, xy, ticks, ticklabels):
@@ -613,6 +624,7 @@ def _handle_linear_segmented_color_map(cmap):
613624
# Label the 3 elements in each row in the cdict entry for a given color as
614625
# (x, y0, y1). Then for values of x between x[i] and x[i+1] the color
615626
# value is interpolated between y1[i] and y0[i+1].
627+
# pylint: disable=protected-access
616628
segdata = cmap._segmentdata
617629
red = segdata['red']
618630
green = segdata['green']
@@ -721,16 +733,16 @@ def _handle_listed_color_map(cmap):
721733
if cmap.N is None or cmap.N == len(cmap.colors):
722734
colors = [
723735
'rgb(%d%s)=(%.15g,%.15g,%.15g)'
724-
% (k, unit, color[0], color[1], color[2])
725-
for (k, color) in enumerate(cmap.colors)
736+
% (k, unit, rgb[0], rgb[1], rgb[2])
737+
for (k, rgb) in enumerate(cmap.colors)
726738
]
727739
else:
728740
reps = int(float(cmap.N) / len(cmap.colors) - 0.5) + 1
729741
repeated_cols = reps * cmap.colors
730742
colors = [
731743
'rgb(%d%s)=(%.15g,%.15g,%.15g)'
732-
% (k, unit, color[0], color[1], color[2])
733-
for (k, color) in enumerate(repeated_cols[:cmap.N])
744+
% (k, unit, rgb[0], rgb[1], rgb[2])
745+
for (k, rgb) in enumerate(repeated_cols[:cmap.N])
734746
]
735747

736748
colormap_string = '{mymap}{[1%s]\n %s\n}' % \

matplotlib2tikz/color.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def mpl_color2xcolor(data, matplotlib_color):
1717
'red': numpy.array([1, 0, 0]),
1818
'green': numpy.array([0, 1, 0]),
1919
'blue': numpy.array([0, 0, 1]),
20-
'brown': numpy.array([0.75, 0.5, 0.25]),
20+
'brown': numpy.array([0.75, 0.5, 0.25]),
2121
'lime': numpy.array([0.75, 1, 0]),
2222
'orange': numpy.array([1, 0.5, 0]),
2323
'pink': numpy.array([1, 0.75, 0.75]),
@@ -60,7 +60,7 @@ def mpl_color2xcolor(data, matplotlib_color):
6060

6161
# The cases 0.0 (my_col == black) and 1.0 (my_col == rgb) are
6262
# already accounted for by checking in available_colors above.
63-
if all(my_col[:3] == alpha * rgb) and 0.0 < alpha and alpha < 1.0:
63+
if all(my_col[:3] == alpha * rgb) and 0.0 < alpha < 1.0:
6464
xcol = name + ('!%r!black' % (alpha * 100))
6565
return data, xcol, my_col
6666

0 commit comments

Comments
 (0)