Skip to content

Commit 4fb6df4

Browse files
authored
Merge pull request #460 from nschloe/np
some updates
2 parents a88d4e6 + 2ad15da commit 4fb6df4

16 files changed

+111
-116
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ jobs:
3535
with:
3636
python-version: ${{ matrix.python-version }}
3737
- uses: actions/checkout@v2
38-
- name: Install system dependencies
39-
run: sudo apt-get install -y texlive-latex-base texlive-latex-extra context python3-tk
38+
# - name: Install system dependencies
39+
# run: sudo apt-get install -y texlive-latex-base texlive-latex-extra context python3-tk
4040
- name: Test with tox
4141
run: |
4242
pip install tox

test/helpers.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import pathlib
23
import subprocess
34
import tempfile
45

@@ -30,7 +31,7 @@ def _unidiff_output(expected, actual):
3031

3132

3233
def assert_equality(
33-
plot, filename, assert_compilation=True, flavor="latex", **extra_get_tikz_code_args
34+
plot, filename, assert_compilation=False, flavor="latex", **extra_get_tikz_code_args
3435
):
3536
plot()
3637
code = tikzplotlib.get_tikz_code(
@@ -41,8 +42,8 @@ def assert_equality(
4142
)
4243
plt.close()
4344

44-
this_dir = os.path.dirname(os.path.abspath(__file__))
45-
with open(os.path.join(this_dir, filename), encoding="utf-8") as f:
45+
this_dir = pathlib.Path(__file__).resolve().parent
46+
with open(this_dir / filename, encoding="utf-8") as f:
4647
reference = f.read()
4748
assert reference == code, filename + "\n" + _unidiff_output(reference, code)
4849

test/refresh_reference_files.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import argparse
22
import importlib.util
3-
import os
3+
import pathlib
44

55
import matplotlib.pyplot as plt
66

@@ -11,16 +11,14 @@ def _main():
1111
parser = argparse.ArgumentParser(description="Refresh all reference TeX files.")
1212
parser.parse_args()
1313

14-
this_dir = os.path.dirname(os.path.abspath(__file__))
14+
this_dir = pathlib.Path(__file__).resolve().parent
1515

1616
test_files = [
1717
f
18-
for f in os.listdir(this_dir)
19-
if os.path.isfile(os.path.join(this_dir, f))
20-
and f[:5] == "test_"
21-
and f[-3:] == ".py"
18+
for f in this_dir.iterdir()
19+
if (this_dir / f).is_file() and f.name[:5] == "test_" and f.name[-3:] == ".py"
2220
]
23-
test_modules = [f[:-3] for f in test_files]
21+
test_modules = [f.name[:-3] for f in test_files]
2422

2523
# remove some edge cases
2624
test_modules.remove("test_rotated_labels")
@@ -36,7 +34,7 @@ def _main():
3634
plt.close()
3735

3836
tex_filename = mod + "_reference.tex"
39-
with open(os.path.join(this_dir, tex_filename), "w", encoding="utf8") as f:
37+
with open(this_dir / tex_filename, "w", encoding="utf8") as f:
4038
f.write(code)
4139

4240

test/test_image_plot_lower.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pathlib
2+
13
import matplotlib.pyplot as plt
24
from helpers import assert_equality
35

@@ -6,13 +8,11 @@
68

79

810
def plot():
9-
import os
10-
1111
import matplotlib.image as mpimg
1212
from matplotlib import rcParams
1313

14-
this_dir = os.path.dirname(os.path.realpath(__file__))
15-
img = mpimg.imread(os.path.join(this_dir, "lena.png"))
14+
this_dir = pathlib.Path(__file__).resolve().parent
15+
img = mpimg.imread(this_dir / "lena.png")
1616

1717
dpi = rcParams["figure.dpi"]
1818
figsize = img.shape[0] / dpi, img.shape[1] / dpi

test/test_image_plot_upper.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
import pathlib
2+
13
import matplotlib.pyplot as plt
24

35
# the picture 'lena.png' with origin='lower' is flipped upside-down.
46
# So it has to be upside-down in the pdf-file as well.
57

68

79
def plot():
8-
import os
9-
1010
import matplotlib.image as mpimg
1111
from matplotlib import rcParams
1212

13-
this_dir = os.path.dirname(os.path.realpath(__file__))
14-
img = mpimg.imread(os.path.join(this_dir, "lena.png"))
13+
this_dir = pathlib.Path(__file__).resolve().parent
14+
img = mpimg.imread(this_dir / "lena.png")
1515

1616
dpi = rcParams["figure.dpi"]
1717
figsize = img.shape[0] / dpi, img.shape[1] / dpi

test/test_text_overlay.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import matplotlib.pyplot as plt
2-
import numpy
2+
import numpy as np
33
from helpers import assert_equality
44

55

66
def plot():
77
fig = plt.figure()
88

9-
xxx = numpy.linspace(0, 5)
9+
xxx = np.linspace(0, 5)
1010
yyy = xxx ** 2
1111
plt.text(
1212
1,

tikzplotlib/_axes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import matplotlib as mpl
2-
import numpy
2+
import numpy as np
33
from matplotlib.backends.backend_pgf import (
44
common_texification as mpl_common_texification,
55
)
@@ -731,7 +731,7 @@ def _handle_linear_segmented_color_map(cmap, data):
731731
# dimension errors or memory errors in latex)
732732
# 0-1000 is the internal granularity of PGFplots.
733733
# 16300 was the maximum value for pgfplots<=1.13
734-
X = _scale_to_int(numpy.array(X), 1000)
734+
X = _scale_to_int(np.array(X), 1000)
735735

736736
color_changes = []
737737
ff = data["float format"]

tikzplotlib/_color.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import matplotlib as mpl
2-
import numpy
2+
import numpy as np
33

44

55
def mpl_color2xcolor(data, matplotlib_color):
66
"""Translates a matplotlib color specification into a proper LaTeX xcolor."""
77
# Convert it to RGBA.
8-
my_col = numpy.array(mpl.colors.ColorConverter().to_rgba(matplotlib_color))
8+
my_col = np.array(mpl.colors.ColorConverter().to_rgba(matplotlib_color))
99

1010
# If the alpha channel is exactly 0, then the color is really 'none'
1111
# regardless of the RGB channels.
@@ -18,22 +18,22 @@ def mpl_color2xcolor(data, matplotlib_color):
1818
# List white first such that for gray values, the combination
1919
# white!<x>!black is preferred over, e.g., gray!<y>!black. Note that
2020
# the order of the dictionary is respected from Python 3.6 on.
21-
"white": numpy.array([1, 1, 1]),
22-
"lightgray": numpy.array([0.75, 0.75, 0.75]),
23-
"gray": numpy.array([0.5, 0.5, 0.5]),
24-
"darkgray": numpy.array([0.25, 0.25, 0.25]),
25-
"black": numpy.array([0, 0, 0]),
21+
"white": np.array([1, 1, 1]),
22+
"lightgray": np.array([0.75, 0.75, 0.75]),
23+
"gray": np.array([0.5, 0.5, 0.5]),
24+
"darkgray": np.array([0.25, 0.25, 0.25]),
25+
"black": np.array([0, 0, 0]),
2626
#
27-
"red": numpy.array([1, 0, 0]),
28-
"green": numpy.array([0, 1, 0]),
29-
"blue": numpy.array([0, 0, 1]),
30-
"brown": numpy.array([0.75, 0.5, 0.25]),
31-
"lime": numpy.array([0.75, 1, 0]),
32-
"orange": numpy.array([1, 0.5, 0]),
33-
"pink": numpy.array([1, 0.75, 0.75]),
34-
"purple": numpy.array([0.75, 0, 0.25]),
35-
"teal": numpy.array([0, 0.5, 0.5]),
36-
"violet": numpy.array([0.5, 0, 0.5]),
27+
"red": np.array([1, 0, 0]),
28+
"green": np.array([0, 1, 0]),
29+
"blue": np.array([0, 0, 1]),
30+
"brown": np.array([0.75, 0.5, 0.25]),
31+
"lime": np.array([0.75, 1, 0]),
32+
"orange": np.array([1, 0.5, 0]),
33+
"pink": np.array([1, 0.75, 0.75]),
34+
"purple": np.array([0.75, 0, 0.25]),
35+
"teal": np.array([0, 0.5, 0.5]),
36+
"violet": np.array([0.5, 0, 0.5]),
3737
# The colors cyan, magenta, yellow, and olive are also
3838
# predefined by xcolor, but their RGB approximation of the
3939
# native CMYK values is not very good. Don't use them here.

tikzplotlib/_files.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import os
2-
import posixpath
1+
import pathlib
32

43

5-
def _gen_filename(data, nb_key, ext):
4+
def _gen_filepath(data, nb_key, ext):
65
name = data["base name"] + f"-{data[nb_key]:03d}{ext}"
7-
return os.path.join(data["output dir"], name), name
6+
return pathlib.Path(data["output dir"]) / name
87

98

10-
def new_filename(data, file_kind, ext):
11-
"""Returns an available filename.
9+
def new_filepath(data, file_kind, ext):
10+
"""Returns an available filepath.
1211
1312
:param file_kind: Name under which numbering is recorded, such as 'img' or
1413
'table'.
@@ -17,7 +16,7 @@ def new_filename(data, file_kind, ext):
1716
:param ext: Filename extension.
1817
:type ext: str
1918
20-
:returns: (filename, rel_filepath) where filename is a path in the
19+
:returns: (filepath, rel_filepath) where filepath is a path in the
2120
filesystem and rel_filepath is the path to be used in the tex
2221
code.
2322
"""
@@ -26,20 +25,19 @@ def new_filename(data, file_kind, ext):
2625
if nb_key not in data.keys():
2726
data[nb_key] = -1
2827

28+
data[nb_key] = data[nb_key] + 1
29+
filepath = _gen_filepath(data, nb_key, ext)
2930
if not data["override externals"]:
3031
# Make sure not to overwrite anything.
31-
file_exists = True
32+
file_exists = filepath.is_file()
3233
while file_exists:
3334
data[nb_key] = data[nb_key] + 1
34-
filename, name = _gen_filename(data, nb_key, ext)
35-
file_exists = os.path.isfile(filename)
36-
else:
37-
data[nb_key] = data[nb_key] + 1
38-
filename, name = _gen_filename(data, nb_key, ext)
35+
filepath = _gen_filepath(data, nb_key, ext)
36+
file_exists = filepath.is_file()
3937

4038
if data["rel data path"]:
41-
rel_filepath = posixpath.join(data["rel data path"], name)
39+
rel_filepath = pathlib.Path(data["rel data path"]) / filepath
4240
else:
43-
rel_filepath = name
41+
rel_filepath = filepath.name
4442

45-
return filename, rel_filepath
43+
return filepath, rel_filepath

tikzplotlib/_image.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import matplotlib as mpl
2-
import numpy
1+
import matplotlib.pyplot as plt
2+
import numpy as np
33
import PIL
44

55
from . import _files
@@ -9,16 +9,16 @@ def draw_image(data, obj):
99
"""Returns the PGFPlots code for an image environment."""
1010
content = []
1111

12-
filename, rel_filepath = _files.new_filename(data, "img", ".png")
12+
filepath, rel_filepath = _files.new_filepath(data, "img", ".png")
1313

1414
# store the image as in a file
1515
img_array = obj.get_array()
1616

1717
dims = img_array.shape
1818
if len(dims) == 2: # the values are given as one real number: look at cmap
1919
clims = obj.get_clim()
20-
mpl.pyplot.imsave(
21-
fname=filename,
20+
plt.imsave(
21+
fname=filepath,
2222
arr=img_array,
2323
cmap=obj.get_cmap(),
2424
vmin=clims[0],
@@ -30,17 +30,17 @@ def draw_image(data, obj):
3030
assert len(dims) == 3 and dims[2] in [3, 4]
3131
# convert to PIL image
3232
if obj.origin == "lower":
33-
img_array = numpy.flipud(img_array)
33+
img_array = np.flipud(img_array)
3434

3535
# Convert mpl image to PIL
36-
if img_array.dtype != numpy.uint8:
37-
img_array = numpy.uint8(img_array * 255)
36+
if img_array.dtype != np.uint8:
37+
img_array = np.uint8(img_array * 255)
3838
image = PIL.Image.fromarray(img_array)
3939

4040
# If the input image is PIL:
4141
# image = PIL.Image.fromarray(img_array)
4242

43-
image.save(filename, origin=obj.origin)
43+
image.save(filepath, origin=obj.origin)
4444

4545
# write the corresponding information to the TikZ file
4646
extent = obj.get_extent()

0 commit comments

Comments
 (0)