Skip to content

Commit bf62dda

Browse files
authored
Merge pull request #436 from nschloe/lgtm
lgtm
2 parents b6adba9 + c25ca37 commit bf62dda

File tree

17 files changed

+118
-135
lines changed

17 files changed

+118
-135
lines changed

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ tag:
88
@if [ "$(shell git rev-parse --abbrev-ref HEAD)" != "master" ]; then exit 1; fi
99
curl -H "Authorization: token `cat $(HOME)/.github-access-token`" -d '{"tag_name": "v$(VERSION)"}' https://api.github.com/repos/nschloe/tikzplotlib/releases
1010

11-
upload:
11+
upload: clean
1212
@if [ "$(shell git rev-parse --abbrev-ref HEAD)" != "master" ]; then exit 1; fi
13-
rm -f dist/*
1413
# https://stackoverflow.com/a/58756491/353337
1514
python3 -m pep517.build --source --binary .
1615
twine upload dist/*

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
<p align="center">The artist formerly known as <em>matplotlib2tikz.</em></p>
44
</p>
55

6-
[![gh-actions](https://img.shields.io/github/workflow/status/nschloe/tikzplotlib/ci?style=flat-square)](https://github.com/nschloe/tikzplotlib/actions?query=workflow%3Aci)
7-
[![codecov](https://img.shields.io/codecov/c/github/nschloe/tikzplotlib.svg?style=flat-square)](https://codecov.io/gh/nschloe/tikzplotlib)
8-
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg?style=flat-square)](https://github.com/psf/black)
9-
[![Documentation Status](https://readthedocs.org/projects/tikzplotlib/badge/?version=latest&style=flat-square)](https://readthedocs.org/projects/tikzplotlib/?badge=latest)
10-
[![awesome](https://img.shields.io/badge/awesome-yes-brightgreen.svg?style=flat-square)](https://github.com/nschloe/tikzplotlib)
11-
[![PyPI pyversions](https://img.shields.io/pypi/pyversions/tikzplotlib.svg?style=flat-square)](https://pypi.org/pypi/tikzplotlib/)
126
[![PyPi Version](https://img.shields.io/pypi/v/tikzplotlib.svg?style=flat-square)](https://pypi.org/project/tikzplotlib)
7+
[![PyPI pyversions](https://img.shields.io/pypi/pyversions/tikzplotlib.svg?style=flat-square)](https://pypi.org/pypi/tikzplotlib/)
138
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1173089.svg?style=flat-square)](https://doi.org/10.5281/zenodo.1173089)
149
[![GitHub stars](https://img.shields.io/github/stars/nschloe/tikzplotlib.svg?style=flat-square&logo=github&label=Stars&logoColor=white)](https://github.com/nschloe/tikzplotlib)
1510
[![PyPi downloads](https://img.shields.io/pypi/dm/tikzplotlib.svg?style=flat-square)](https://pypistats.org/packages/tikzplotlib)
1611

12+
[![Documentation Status](https://readthedocs.org/projects/tikzplotlib/badge/?version=latest&style=flat-square)](https://readthedocs.org/projects/tikzplotlib/?badge=latest)
13+
[![awesome](https://img.shields.io/badge/awesome-yes-brightgreen.svg?style=flat-square)](https://github.com/nschloe/tikzplotlib)
14+
15+
[![gh-actions](https://img.shields.io/github/workflow/status/nschloe/tikzplotlib/ci?style=flat-square)](https://github.com/nschloe/tikzplotlib/actions?query=workflow%3Aci)
16+
[![codecov](https://img.shields.io/codecov/c/github/nschloe/tikzplotlib.svg?style=flat-square)](https://codecov.io/gh/nschloe/tikzplotlib)
17+
[![LGTM](https://img.shields.io/lgtm/grade/python/github/nschloe/tikzplotlib.svg?style=flat-square)](https://lgtm.com/projects/g/nschloe/tikzplotlib)
18+
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg?style=flat-square)](https://github.com/psf/black)
19+
1720
This is tikzplotlib, a Python tool for converting matplotlib figures into
1821
[PGFPlots](https://www.ctan.org/pkg/pgfplots) ([PGF/TikZ](https://www.ctan.org/pkg/pgf))
1922
figures like
@@ -23,7 +26,7 @@ figures like
2326
for native inclusion into LaTeX or ConTeXt documents.
2427

2528
The output of tikzplotlib is in
26-
[PGFPlots](http://pgfplots.sourceforge.net/pgfplots.pdf), a TeX library that sits on
29+
[PGFPlots](https://github.com/pgf-tikz/pgfplots/), a TeX library that sits on
2730
top of [PGF/TikZ](https://en.wikipedia.org/wiki/PGF/TikZ) and describes graphs in terms
2831
of axes, data etc. Consequently, the output of tikzplotlib
2932

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = tikzplotlib
3-
version = 0.9.3
3+
version = 0.9.4
44
author = Nico Schlömer
55
author_email = [email protected]
66
description = Convert matplotlib figures into TikZ/PGFPlots

test/helpers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010

1111
def print_tree(obj, indent=""):
12-
"""Recursively prints the tree structure of the matplotlib object.
13-
"""
12+
"""Recursively prints the tree structure of the matplotlib object."""
1413
if isinstance(obj, matplotlib.text.Text):
1514
print(indent, type(obj).__name__, f'("{obj.get_text()}")')
1615
else:

test/test_hatch.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
def plot():
55
"""
6-
Hatch demo code from
7-
https://matplotlib.org/3.1.1/gallery/shapes_and_collections/hatch_demo.html
6+
Hatch demo code from
7+
https://matplotlib.org/3.1.1/gallery/shapes_and_collections/hatch_demo.html
88
9-
Slightly modified to test more aspects of the hatch implementation
9+
Slightly modified to test more aspects of the hatch implementation
1010
"""
1111
import matplotlib.pyplot as plt
1212
from matplotlib.patches import Ellipse, Polygon

tikzplotlib/_axes.py

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ def _common_texification(string):
1414

1515
class Axes:
1616
def __init__(self, data, obj): # noqa: C901
17-
"""Returns the PGFPlots code for an axis environment.
18-
"""
17+
"""Returns the PGFPlots code for an axis environment."""
1918
self.content = []
2019

2120
# Are we dealing with an axis that hosts a colorbar? Skip then, those are
@@ -270,14 +269,14 @@ def _ticks(self, data, obj):
270269
self.axis_options.append("tick align=center")
271270

272271
# Set each rotation for every label
273-
x_tick_rotation_and_horizontal_alignment = self._get_label_rotation_and_horizontal_alignment(
274-
obj, data, "x"
272+
x_tick_rotation_and_horizontal_alignment = (
273+
self._get_label_rotation_and_horizontal_alignment(obj, data, "x")
275274
)
276275
if x_tick_rotation_and_horizontal_alignment:
277276
self.axis_options.append(x_tick_rotation_and_horizontal_alignment)
278277

279-
y_tick_rotation_and_horizontal_alignment = self._get_label_rotation_and_horizontal_alignment(
280-
obj, data, "y"
278+
y_tick_rotation_and_horizontal_alignment = (
279+
self._get_label_rotation_and_horizontal_alignment(obj, data, "y")
281280
)
282281
if y_tick_rotation_and_horizontal_alignment:
283282
self.axis_options.append(y_tick_rotation_and_horizontal_alignment)
@@ -431,11 +430,6 @@ def _subplot(self, obj, data):
431430
data["pgfplots libs"].add("groupplots")
432431

433432
def _get_label_rotation_and_horizontal_alignment(self, obj, data, x_or_y):
434-
tick_label_text_width = None
435-
tick_label_text_width_identifier = f"{x_or_y} tick label text width"
436-
if tick_label_text_width_identifier in self.axis_options:
437-
self.axis_options.remove(tick_label_text_width_identifier)
438-
439433
label_style = ""
440434

441435
major_tick_labels = (
@@ -447,6 +441,10 @@ def _get_label_rotation_and_horizontal_alignment(self, obj, data, x_or_y):
447441
if not major_tick_labels:
448442
return None
449443

444+
tick_label_text_width_identifier = f"{x_or_y} tick label text width"
445+
if tick_label_text_width_identifier in self.axis_options:
446+
self.axis_options.remove(tick_label_text_width_identifier)
447+
450448
tick_labels_rotation = [label.get_rotation() for label in major_tick_labels]
451449
tick_labels_rotation_same_value = len(set(tick_labels_rotation)) == 1
452450

@@ -468,9 +466,10 @@ def _get_label_rotation_and_horizontal_alignment(self, obj, data, x_or_y):
468466

469467
# Horizontal alignment will be ignored if no 'x/y tick label text width' has
470468
# been passed in the 'extra' parameter
471-
if tick_label_text_width:
472-
values.append(f"align={tick_labels_horizontal_alignment[0]}")
473-
values.append(f"text width={tick_label_text_width}")
469+
# tick_label_text_width = None
470+
# if tick_label_text_width:
471+
# values.append(f"align={tick_labels_horizontal_alignment[0]}")
472+
# values.append(f"text width={tick_label_text_width}")
474473

475474
if values:
476475
label_style = "{}ticklabel style = {{{}}}".format(
@@ -490,18 +489,18 @@ def _get_label_rotation_and_horizontal_alignment(self, obj, data, x_or_y):
490489

491490
# Ignore horizontal alignment if no '{x,y} tick label text width' has been
492491
# passed in the 'extra' parameter
493-
if tick_label_text_width:
494-
if tick_labels_horizontal_alignment_same_value:
495-
values.append(f"align={tick_labels_horizontal_alignment[0]}")
496-
values.append(f"text width={tick_label_text_width}")
497-
else:
498-
for idx, x in enumerate(tick_labels_horizontal_alignment):
499-
label_style += f"{x_or_y}_tick_label_ha_{idx}/.initial = {x}"
500-
501-
values.append(
502-
f"align=\\pgfkeysvalueof{{/pgfplots/{x_or_y}_tick_label_ha_\\ticknum}}"
503-
)
504-
values.append(f"text width={tick_label_text_width}")
492+
# if tick_label_text_width:
493+
# if tick_labels_horizontal_alignment_same_value:
494+
# values.append(f"align={tick_labels_horizontal_alignment[0]}")
495+
# values.append(f"text width={tick_label_text_width}")
496+
# else:
497+
# for idx, x in enumerate(tick_labels_horizontal_alignment):
498+
# label_style += f"{x_or_y}_tick_label_ha_{idx}/.initial = {x}"
499+
500+
# values.append(
501+
# f"align=\\pgfkeysvalueof{{/pgfplots/{x_or_y}_tick_label_ha_\\ticknum}}"
502+
# )
503+
# values.append(f"text width={tick_label_text_width}")
505504

506505
label_style = (
507506
"every {} tick label/.style = {{\n"
@@ -600,8 +599,7 @@ def _get_ticks(data, xy, ticks, ticklabels):
600599

601600

602601
def _is_colorbar_heuristic(obj):
603-
"""Find out if the object is in fact a color bar.
604-
"""
602+
"""Find out if the object is in fact a color bar."""
605603
# TODO come up with something more accurate here
606604
# Might help:
607605
# TODO Are the colorbars exactly the l.collections.PolyCollection's?
@@ -664,7 +662,6 @@ def _handle_linear_segmented_color_map(cmap, data):
664662
k_red = 0
665663
k_green = 0
666664
k_blue = 0
667-
x = 0.0
668665
colors = []
669666
X = []
670667
while True:
@@ -782,8 +779,7 @@ def _handle_listed_color_map(cmap, data):
782779

783780

784781
def _scale_to_int(X, max_val):
785-
"""Scales the array X such that it contains only integers.
786-
"""
782+
"""Scales the array X such that it contains only integers."""
787783
# if max_val is None:
788784
# X = X / _gcd_array(X)
789785
X = X / max(1 / max_val, _gcd_array(X))
@@ -817,8 +813,7 @@ def _gcd(a, b):
817813

818814

819815
def _linear_interpolation(x, X, Y):
820-
"""Given two data points [X,Y], linearly interpolate those at x.
821-
"""
816+
"""Given two data points [X,Y], linearly interpolate those at x."""
822817
return (Y[1] * (x - X[0]) + Y[0] * (X[1] - x)) / (X[1] - X[0])
823818

824819

tikzplotlib/_color.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44

55
def mpl_color2xcolor(data, matplotlib_color):
6-
"""Translates a matplotlib color specification into a proper LaTeX xcolor.
7-
"""
6+
"""Translates a matplotlib color specification into a proper LaTeX xcolor."""
87
# Convert it to RGBA.
98
my_col = numpy.array(mpl.colors.ColorConverter().to_rgba(matplotlib_color))
109

tikzplotlib/_hatches.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,27 @@
3535

3636
def add_custom_pattern(mpl_hatch, pattern_name, pattern_definition=None):
3737
"""
38-
The patterns of tikzpgf are quite simple, and cannot be customized but for the
39-
color. A solution is to expose a function like this one to allow the user to
40-
populate _MP_HATCH2PGF_PATTERN with custom (hatch, pattern) pairs. mpl does no
41-
validation of the hatch string, it just ignores it if it does not recognize it,
42-
so it is possible to have <any> string be a mpl_hatch.
43-
44-
If the pattern definition is passed, it could be added at the start of the code
45-
in a similar fashion to
46-
> data["custom colors"] = {}
47-
in get_tikz_code(). tikzplotlib pattern definitions would mend the bad
48-
correspondence between the mpl hatches and tikz patterns, with custom patterns
49-
for the mpl hatches 'o' and 'O'
50-
51-
Want some opinions on this before I implement it..
52-
53-
From https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.patches.Patch.html:
54-
> Letters can be combined, in which case all the specified hatchings are done.
55-
> If same letter repeats, it increases the density of hatching of that pattern.
56-
To achieve something like this, custom patterns must be created
57-
https://tex.stackexchange.com/questions/29359/pgfplots-how-to-fill-the-area-
58-
under-a-curve-with-oblique-lines-hatching-as-a/29367#29367
38+
The patterns of tikzpgf are quite simple, and cannot be customized but for the
39+
color. A solution is to expose a function like this one to allow the user to
40+
populate _MP_HATCH2PGF_PATTERN with custom (hatch, pattern) pairs. mpl does no
41+
validation of the hatch string, it just ignores it if it does not recognize it, so
42+
it is possible to have <any> string be a mpl_hatch.
43+
44+
If the pattern definition is passed, it could be added at the start of the code in a
45+
similar fashion to
46+
> data["custom colors"] = {}
47+
in get_tikz_code(). tikzplotlib pattern definitions would mend the bad
48+
correspondence between the mpl hatches and tikz patterns, with custom patterns for
49+
the mpl hatches 'o' and 'O'.
50+
51+
Want some opinions on this before I implement it..
52+
53+
From https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.patches.Patch.html:
54+
> Letters can be combined, in which case all the specified hatchings are done.
55+
> If same letter repeats, it increases the density of hatching of that pattern.
56+
To achieve something like this, custom patterns must be created
57+
https://tex.stackexchange.com/questions/29359/pgfplots-how-to-fill-the-area-
58+
under-a-curve-with-oblique-lines-hatching-as-a/29367#29367
5959
"""
6060

6161

@@ -80,14 +80,14 @@ def __validate_hatch(hatch):
8080

8181
def _mpl_hatch2pgfp_pattern(data, hatch, color_name, color_rgba):
8282
r"""
83-
Translates a hatch from matplotlib to the corresponding patten in PGFPlots.
84-
85-
Input:
86-
hatch - str, like {'/', '\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}
87-
color_name - str, xcolor or custom color name
88-
color_rgba - np.array, the rgba value of the color
89-
Output:
90-
draw_options - list, empty or with a post action string
83+
Translates a hatch from matplotlib to the corresponding patten in PGFPlots.
84+
85+
Input:
86+
hatch - str, like {'/', '\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}
87+
color_name - str, xcolor or custom color name
88+
color_rgba - np.array, the rgba value of the color
89+
Output:
90+
draw_options - list, empty or with a post action string
9191
"""
9292
hatch = __validate_hatch(hatch)
9393
try:

tikzplotlib/_image.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77

88
def draw_image(data, obj):
9-
"""Returns the PGFPlots code for an image environment.
10-
"""
9+
"""Returns the PGFPlots code for an image environment."""
1110
content = []
1211

1312
filename, rel_filepath = _files.new_filename(data, "img", ".png")

tikzplotlib/_legend.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77

88
def draw_legend(data, obj):
9-
"""Adds legend code.
10-
"""
9+
"""Adds legend code."""
1110
texts = []
1211
children_alignment = []
1312
for text in obj.texts:

tikzplotlib/_line2d.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212

1313
def draw_line2d(data, obj):
14-
"""Returns the PGFPlots code for an Line2D environment.
15-
"""
14+
"""Returns the PGFPlots code for an Line2D environment."""
1615
content = []
1716
addplot_options = []
1817

@@ -101,8 +100,7 @@ def draw_line2d(data, obj):
101100

102101

103102
def draw_linecollection(data, obj):
104-
"""Returns Pgfplots code for a number of patch objects.
105-
"""
103+
"""Returns Pgfplots code for a number of patch objects."""
106104
content = []
107105

108106
edgecolors = obj.get_edgecolors()

tikzplotlib/_patch.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66

77
def draw_patch(data, obj):
8-
"""Return the PGFPlots code for patches.
9-
"""
8+
"""Return the PGFPlots code for patches."""
109
if isinstance(obj, mpl.patches.FancyArrowPatch):
1110
data, draw_options = mypath.get_draw_options(
1211
data,
@@ -70,8 +69,7 @@ def zip_modulo(*seqs):
7069

7170

7271
def draw_patchcollection(data, obj):
73-
"""Returns PGFPlots code for a number of patch objects.
74-
"""
72+
"""Returns PGFPlots code for a number of patch objects."""
7573
content = []
7674

7775
# recompute the face colors
@@ -116,8 +114,7 @@ def _draw_polygon(data, obj, draw_options):
116114

117115

118116
def _draw_rectangle(data, obj, draw_options):
119-
"""Return the PGFPlots code for rectangles.
120-
"""
117+
"""Return the PGFPlots code for rectangles."""
121118
# Objects with labels are plot objects (from bar charts, etc). Even those without
122119
# labels explicitly set have a label of "_nolegend_". Everything else should be
123120
# skipped because they likely correspong to axis/legend objects which are handled by
@@ -156,8 +153,7 @@ def _draw_rectangle(data, obj, draw_options):
156153

157154

158155
def _draw_ellipse(data, obj, draw_options):
159-
"""Return the PGFPlots code for ellipses.
160-
"""
156+
"""Return the PGFPlots code for ellipses."""
161157
if isinstance(obj, mpl.patches.Circle):
162158
# circle specialization
163159
return _draw_circle(data, obj, draw_options)
@@ -180,8 +176,7 @@ def _draw_ellipse(data, obj, draw_options):
180176

181177

182178
def _draw_circle(data, obj, draw_options):
183-
"""Return the PGFPlots code for circles.
184-
"""
179+
"""Return the PGFPlots code for circles."""
185180
x, y = obj.center
186181
ff = data["float format"]
187182
do = ",".join(draw_options)

0 commit comments

Comments
 (0)