Skip to content

Commit a5c20a2

Browse files
authored
Merge pull request #519 from nschloe/update-mpl-tests
update mpl for tests
2 parents b564b88 + e5d94db commit a5c20a2

10 files changed

+63
-101
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
runs-on: ubuntu-latest
2424
strategy:
2525
matrix:
26-
python-version: ["3.7", "3.8", "3.9", "3.10-dev"]
26+
python-version: ["3.7", "3.8", "3.9", "3.10"]
2727
steps:
2828
- uses: actions/setup-python@v2
2929
with:

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ The output of tikzplotlib is in [PGFPlots](https://github.com/pgf-tikz/pgfplots/
3030
library that sits on top of [PGF/TikZ](https://en.wikipedia.org/wiki/PGF/TikZ) and
3131
describes graphs in terms of axes, data etc. Consequently, the output of tikzplotlib
3232

33-
- retains more information,
34-
- can be more easily understood, and
35-
- is more easily editable
33+
- retains more information,
34+
- can be more easily understood, and
35+
- is more easily editable
3636

3737
than [raw TikZ output](https://matplotlib.org/users/whats_new.html#pgf-tikz-backend).
3838
For example, the matplotlib figure

setup.cfg

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

src/tikzplotlib/_axes.py

+28-15
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ def _get_tick_position(obj, axes_obj):
541541
if len(set(major_ticks_top)) == 1 and major_ticks_top[0] is True:
542542
major_ticks_top_show_all = True
543543

544+
position_string = None
544545
major_ticks_position = None
545546
if not major_ticks_bottom_show_all and not major_ticks_top_show_all:
546547
position_string = f"{axes_obj}majorticks=false"
@@ -562,27 +563,39 @@ def _get_ticks(data, xy, ticks, ticklabels):
562563
necessary axis options for the given configuration.
563564
"""
564565
axis_options = []
565-
pgfplots_ticks = []
566-
pgfplots_ticklabels = []
566+
567+
# Check if the label is necessary. If one of the labels is, then all of them must
568+
# appear in the TikZ plot.
567569
is_label_required = False
568570
for tick, ticklabel in zip(ticks, ticklabels):
569571
# store the label anyway
570572
label = ticklabel.get_text()
571-
if "," in label:
572-
label = "{" + label + "}"
573-
if ticklabel.get_visible():
574-
label = _common_texification(label)
575-
pgfplots_ticklabels.append(label)
576-
else:
573+
574+
if not ticklabel.get_visible():
577575
is_label_required = True
578-
# Check if the label is necessary. If one of the labels is, then all of them
579-
# must appear in the TikZ plot.
580-
if label:
581-
try:
582-
label_float = float(label.replace("\N{MINUS SIGN}", "-"))
583-
is_label_required = is_label_required or (label and label_float != tick)
584-
except ValueError:
576+
break
577+
578+
if not label:
579+
continue
580+
581+
try:
582+
label_float = float(label.replace("\N{MINUS SIGN}", "-"))
583+
except ValueError:
584+
is_label_required = True
585+
break
586+
else:
587+
if abs(label_float - tick) > 1.0e-10 + 1.0e-10 * abs(tick):
585588
is_label_required = True
589+
break
590+
591+
pgfplots_ticks = []
592+
pgfplots_ticklabels = []
593+
for tick, ticklabel in zip(ticks, ticklabels):
594+
label = ticklabel.get_text()
595+
if "," in label:
596+
label = "{" + label + "}"
597+
pgfplots_ticklabels.append(_common_texification(label))
598+
586599
# note: ticks may be present even if labels are not, keep them for grid lines
587600
for tick in ticks:
588601
pgfplots_ticks.append(tick)

src/tikzplotlib/_path.py

+17-34
Original file line numberDiff line numberDiff line change
@@ -407,44 +407,27 @@ def get_draw_options(data, obj, ec, fc, ls, lw, hatch=None):
407407

408408

409409
def mpl_linewidth2pgfp_linewidth(data, line_width):
410-
if data["strict"]:
411-
# Takes the matplotlib linewidths, and just translate them into PGFPlots.
412-
try:
413-
return {
414-
0.1: "ultra thin",
415-
0.2: "very thin",
416-
0.4: "thin",
417-
0.6: "semithick",
418-
0.8: "thick",
419-
1.2: "very thick",
420-
1.6: "ultra thick",
421-
}[line_width]
422-
except KeyError:
423-
# explicit line width
424-
ff = data["float format"]
425-
return f"line width={line_width:{ff}}pt"
426-
427-
# The following is an alternative approach to line widths.
428-
# The default line width in matplotlib is 1.0pt, in PGFPlots 0.4pt
429-
# ('thin').
430-
# Match the two defaults, and scale for the rest.
431-
scaled_line_width = line_width / 1.0 # scale by default line width
410+
# PGFplots gives line widths in pt, matplotlib in axes space. Translate.
411+
# Scale such that the default mpl line width (1.5) is mapped to the PGFplots
412+
# line with semithick, 0.6. From a visual comparison, semithick or even thick
413+
# matches best with the default mpl style.
414+
# Keep the line with in units of decipoint to make sure we stay in integers.
415+
line_width_decipoint = line_width * 4 # 4 = 10 * 0.6 / 1.5
432416
try:
433-
out = {
434-
0.25: "ultra thin",
435-
0.5: "very thin",
436-
1.0: None, # default, 'thin'
437-
1.5: "semithick",
438-
2: "thick",
439-
3: "very thick",
440-
4: "ultra thick",
441-
}[scaled_line_width]
417+
# https://github.com/pgf-tikz/pgf/blob/e9c22dc9fe48f975b7fdb32181f03090b3747499/tex/generic/pgf/frontendlayer/tikz/tikz.code.tex#L1574
418+
return {
419+
1: "ultra thin",
420+
2: "very thin",
421+
4: None, # "thin",
422+
6: "semithick",
423+
8: "thick",
424+
12: "very thick",
425+
16: "ultra thick",
426+
}[line_width_decipoint]
442427
except KeyError:
443428
# explicit line width
444429
ff = data["float format"]
445-
out = f"line width={0.4 * line_width:{ff}}pt"
446-
447-
return out
430+
return f"line width={line_width_decipoint / 10:{ff}}pt"
448431

449432

450433
def mpl_linestyle2pgfplots_linestyle(data, line_style, line=None):

tests/test_barchart_errorbars_reference.tex

+6-6
Original file line numberDiff line numberDiff line change
@@ -55,37 +55,37 @@
5555
(axis cs:2.25,0.9)
5656
--(axis cs:2.25,1.1);
5757

58-
\addplot [semithick, black, mark=-, mark size=8, mark options={solid}, only marks]
58+
\addplot [line width=2pt, black, mark=-, mark size=8, mark options={solid}, only marks]
5959
table {%
6060
-0.25 0.9
6161
0.75 1.8
6262
1.75 2.5
6363
};
64-
\addplot [semithick, black, mark=-, mark size=8, mark options={solid}, only marks]
64+
\addplot [line width=2pt, black, mark=-, mark size=8, mark options={solid}, only marks]
6565
table {%
6666
-0.25 1.1
6767
0.75 2.2
6868
1.75 3.5
6969
};
70-
\addplot [semithick, black, mark=-, mark size=8, mark options={solid}, only marks]
70+
\addplot [line width=2pt, black, mark=-, mark size=8, mark options={solid}, only marks]
7171
table {%
7272
0 2.6
7373
1 1.8
7474
2 3.5
7575
};
76-
\addplot [semithick, black, mark=-, mark size=8, mark options={solid}, only marks]
76+
\addplot [line width=2pt, black, mark=-, mark size=8, mark options={solid}, only marks]
7777
table {%
7878
0 3.4
7979
1 2.2
8080
2 4.5
8181
};
82-
\addplot [semithick, black, mark=-, mark size=8, mark options={solid}, only marks]
82+
\addplot [line width=2pt, black, mark=-, mark size=8, mark options={solid}, only marks]
8383
table {%
8484
0.25 4.9
8585
1.25 2.8
8686
2.25 0.9
8787
};
88-
\addplot [semithick, black, mark=-, mark size=8, mark options={solid}, only marks]
88+
\addplot [line width=2pt, black, mark=-, mark size=8, mark options={solid}, only marks]
8989
table {%
9090
0.25 5.1
9191
1.25 3.2

tests/test_cleanfigure.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def test_surface3D(self):
174174

175175
with plt.rc_context(rc=RC_PARAMS):
176176
fig = plt.figure()
177-
ax = fig.gca(projection="3d")
177+
ax = plt.axes(projection="3d")
178178

179179
# Plot the surface.
180180
surf = ax.plot_surface(
@@ -217,7 +217,7 @@ def test_trisurface3D(self):
217217

218218
with plt.rc_context(rc=RC_PARAMS):
219219
fig = plt.figure()
220-
ax = fig.gca(projection="3d")
220+
ax = plt.axes(projection="3d")
221221

222222
ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True)
223223
with pytest.warns(Warning):
@@ -244,7 +244,7 @@ def test_polygon3D(self):
244244

245245
with plt.rc_context(rc=RC_PARAMS):
246246
fig = plt.figure()
247-
ax = fig.gca(projection="3d")
247+
ax = plt.axes(projection="3d")
248248

249249
def cc(arg):
250250
"""
@@ -302,7 +302,7 @@ def test_bar3D(self):
302302
def test_quiver3D(self):
303303
with plt.rc_context(rc=RC_PARAMS):
304304
fig = plt.figure()
305-
ax = fig.gca(projection="3d")
305+
ax = plt.axes(projection="3d")
306306

307307
# Make the grid
308308
x, y, z = np.meshgrid(
@@ -328,8 +328,7 @@ def test_quiver3D(self):
328328

329329
def test_2D_in_3D(self):
330330
with plt.rc_context(rc=RC_PARAMS):
331-
fig = plt.figure()
332-
ax = fig.gca(projection="3d")
331+
ax = plt.axes(projection="3d")
333332

334333
# Plot a sin curve using the x and y axes.
335334
x = np.linspace(0, 1, 100)

tests/test_colorbars_reference.tex

+1-32
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,9 @@
1919
--(axis cs:9.9414062,10)
2020
--(axis cs:-4.9414062,10)
2121
--(axis cs:-5,10)
22-
--cycle;
23-
\addplot graphics [includegraphics cmd=\pgfimage,xmin=-5, xmax=10, ymin=-5, ymax=10] {tmp-000.png};
24-
\path [draw=black, line width=0.32pt]
25-
(axis cs:-5,-5)
26-
--(axis cs:-4.9414062,-5)
27-
--(axis cs:9.9414062,-5)
28-
--(axis cs:10,-5)
29-
--(axis cs:10,10)
30-
--(axis cs:9.9414062,10)
31-
--(axis cs:-4.9414062,10)
3222
--(axis cs:-5,10)
3323
--cycle;
24+
\addplot graphics [includegraphics cmd=\pgfimage,xmin=-5, xmax=10, ymin=-5, ymax=10] {tmp-000.png};
3425

3526
\nextgroupplot[
3627
tick align=outside,
@@ -39,8 +30,6 @@
3930
xlabel={Discrete intervals, some other units},
4031
xmin=0.65, xmax=8.35,
4132
xtick style={color=black},
42-
xtick={1,2,4,7,8},
43-
xticklabels={1,2,4,7,8},
4433
ymin=1, ymax=8
4534
]
4635
\path [draw=white, fill=white, line width=0.004pt]
@@ -54,16 +43,6 @@
5443
--(axis cs:0.65,4.5)
5544
--cycle;
5645
\addplot graphics [includegraphics cmd=\pgfimage,xmin=0.65, xmax=8.35, ymin=1, ymax=8] {tmp-001.png};
57-
\path [draw=black, line width=0.32pt]
58-
(axis cs:0.65,4.5)
59-
--(axis cs:1,1)
60-
--(axis cs:8,1)
61-
--(axis cs:8.35,4.5)
62-
--(axis cs:8.35,4.5)
63-
--(axis cs:8,8)
64-
--(axis cs:1,8)
65-
--(axis cs:0.65,4.5)
66-
--cycle;
6746

6847
\nextgroupplot[
6948
tick align=outside,
@@ -85,16 +64,6 @@
8564
--(axis cs:-1.5,0)
8665
--cycle;
8766
\addplot graphics [includegraphics cmd=\pgfimage,xmin=-1.5, xmax=1.5, ymin=-1, ymax=1] {tmp-002.png};
88-
\path [draw=black, line width=0.32pt]
89-
(axis cs:-1.5,0)
90-
--(axis cs:-1,-1)
91-
--(axis cs:1,-1)
92-
--(axis cs:1.5,0)
93-
--(axis cs:1.5,0)
94-
--(axis cs:1,1)
95-
--(axis cs:-1,1)
96-
--(axis cs:-1.5,0)
97-
--cycle;
9867
\end{groupplot}
9968

10069
\end{tikzpicture}

tests/test_fillstyle_reference.tex

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
x grid style={white!69.019608!black},
99
xmin=-0.05, xmax=1.05,
1010
xtick style={color=black},
11-
xtick={-0.2,0,0.2,0.4,0.6,0.8,1,1.2},
12-
xticklabels={−0.2,0.0,0.2,0.4,0.6,0.8,1.0,1.2},
1311
y grid style={white!69.019608!black},
1412
ymin=-0.45, ymax=9.45,
1513
ytick style={color=black}

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ deps =
88
pytest
99
pytest-cov
1010
pytest-codeblocks
11-
matplotlib == 3.3.4
11+
matplotlib == 3.4.3
1212
pytest-randomly
1313
commands =
1414
pytest {posargs} --codeblocks

0 commit comments

Comments
 (0)