Skip to content

Commit 133154a

Browse files
authored
Merge pull request #344 from nschloe/scatter-markers
fix marker handling for paths
2 parents 6b34e2a + adef689 commit 133154a

10 files changed

+107
-82
lines changed

test/test_image_plot_upper_reference.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
x grid style={white!69.01960784313725!black},
1414
xmin=-0.5, xmax=511.5,
1515
xtick style={color=black},
16+
y dir=reverse,
1617
y grid style={white!69.01960784313725!black},
1718
ymin=-0.5, ymax=511.5,
1819
ytick style={color=black}

test/test_legend_line_scatter_reference.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
ymin=-0.239285714285714, ymax=4.23928571428571,
1515
ytick style={color=black}
1616
]
17-
\addplot [only marks, draw=color0, fill=color0, colormap/viridis]
17+
\addplot [only marks, marker=*, draw=color0, fill=color0, colormap/viridis]
1818
table{%
1919
x y
2020
0 0

test/test_marker_reference.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
ymin=-1.1, ymax=1.1,
1616
ytick style={color=black}
1717
]
18-
\addplot [only marks, draw=black, fill=black, colormap/viridis]
18+
\addplot [only marks, marker=+, draw=black, fill=black, colormap/viridis]
1919
table{%
2020
x y
2121
0 0

test/test_pandas_dataframe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import matplotlib.pyplot as plt
2-
32
import pandas as pd
3+
44
from helpers import assert_equality
55

66

test/test_scatter_reference.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
ymin=0.162919838390652, ymax=112.82609249885,
1717
ytick style={color=black}
1818
]
19-
\addplot [only marks, draw=color0, fill=color0, colormap/viridis]
19+
\addplot [only marks, marker=*, draw=color0, fill=color0, colormap/viridis]
2020
table{%
2121
x y
2222
0 10.4470377839679

test/test_subplots_with_colorbars_reference.tex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
x grid style={white!69.01960784313725!black},
1212
xmin=-0.5, xmax=2.5,
1313
xtick style={color=black},
14+
y dir=reverse,
1415
y grid style={white!69.01960784313725!black},
1516
ymin=-0.5, ymax=2.5,
1617
ytick style={color=black}
@@ -27,6 +28,7 @@
2728
x grid style={white!69.01960784313725!black},
2829
xmin=-0.5, xmax=2.5,
2930
xtick style={color=black},
31+
y dir=reverse,
3032
y grid style={white!69.01960784313725!black},
3133
ymin=-0.5, ymax=2.5,
3234
ytick style={color=black}
@@ -43,6 +45,7 @@
4345
x grid style={white!69.01960784313725!black},
4446
xmin=-0.5, xmax=2.5,
4547
xtick style={color=black},
48+
y dir=reverse,
4649
y grid style={white!69.01960784313725!black},
4750
ymin=-0.5, ymax=2.5,
4851
ytick style={color=black}

tikzplotlib/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
__email__ = "[email protected]"
33
__copyright__ = "Copyright (c) 2010-2019, {} <{}>".format(__author__, __email__)
44
__license__ = "License :: OSI Approved :: MIT License"
5-
__version__ = "0.8.3"
5+
__version__ = "0.8.4"
66
__status__ = "Development Status :: 5 - Production/Stable"

tikzplotlib/_markers.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# for matplotlib markers, see: http://matplotlib.org/api/markers_api.html
2+
_MP_MARKER2PGF_MARKER = {
3+
".": "*", # point
4+
"o": "o", # circle
5+
"+": "+", # plus
6+
"x": "x", # x
7+
"None": None,
8+
" ": None,
9+
"": None,
10+
}
11+
12+
# the following markers are only available with PGF's plotmarks library
13+
_MP_MARKER2PLOTMARKS = {
14+
"v": ("triangle", ["rotate=180"]), # triangle down
15+
"1": ("triangle", ["rotate=180"]),
16+
"^": ("triangle", []), # triangle up
17+
"2": ("triangle", []),
18+
"<": ("triangle", ["rotate=270"]), # triangle left
19+
"3": ("triangle", ["rotate=270"]),
20+
">": ("triangle", ["rotate=90"]), # triangle right
21+
"4": ("triangle", ["rotate=90"]),
22+
"s": ("square", []),
23+
"p": ("pentagon", []),
24+
"*": ("asterisk", []),
25+
"h": ("star", []), # hexagon 1
26+
"H": ("star", []), # hexagon 2
27+
"d": ("diamond", []), # diamond
28+
"D": ("diamond", []), # thin diamond
29+
"|": ("|", []), # vertical line
30+
"_": ("-", []), # horizontal line
31+
}
32+
33+
34+
def _mpl_marker2pgfp_marker(data, mpl_marker, marker_face_color):
35+
"""Translates a marker style of matplotlib to the corresponding style
36+
in PGFPlots.
37+
"""
38+
# try default list
39+
try:
40+
pgfplots_marker = _MP_MARKER2PGF_MARKER[mpl_marker]
41+
except KeyError:
42+
pass
43+
else:
44+
if (marker_face_color is not None) and pgfplots_marker == "o":
45+
pgfplots_marker = "*"
46+
data["tikz libs"].add("plotmarks")
47+
marker_options = []
48+
return data, pgfplots_marker, marker_options
49+
50+
# try plotmarks list
51+
try:
52+
data["tikz libs"].add("plotmarks")
53+
pgfplots_marker, marker_options = _MP_MARKER2PLOTMARKS[mpl_marker]
54+
except KeyError:
55+
# There's no equivalent for the pixel marker (,) in Pgfplots.
56+
pass
57+
else:
58+
if (
59+
marker_face_color is not None
60+
and (
61+
not isinstance(marker_face_color, str)
62+
or marker_face_color.lower() != "none"
63+
)
64+
and pgfplots_marker not in ["|", "-", "asterisk", "star"]
65+
):
66+
pgfplots_marker += "*"
67+
return data, pgfplots_marker, marker_options
68+
69+
return data, None, []

tikzplotlib/line2d.py

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from . import files
88
from . import path as mypath
99
from .util import get_legend_text, has_legend, transform_to_data_coordinates
10+
from ._markers import _mpl_marker2pgfp_marker
1011

1112

1213
def draw_line2d(data, obj):
@@ -123,77 +124,6 @@ def draw_linecollection(data, obj):
123124
return data, content
124125

125126

126-
# for matplotlib markers, see: http://matplotlib.org/api/markers_api.html
127-
_MP_MARKER2PGF_MARKER = {
128-
".": "*", # point
129-
"o": "o", # circle
130-
"+": "+", # plus
131-
"x": "x", # x
132-
"None": None,
133-
" ": None,
134-
"": None,
135-
}
136-
137-
# the following markers are only available with PGF's plotmarks library
138-
_MP_MARKER2PLOTMARKS = {
139-
"v": ("triangle", "rotate=180"), # triangle down
140-
"1": ("triangle", "rotate=180"),
141-
"^": ("triangle", None), # triangle up
142-
"2": ("triangle", None),
143-
"<": ("triangle", "rotate=270"), # triangle left
144-
"3": ("triangle", "rotate=270"),
145-
">": ("triangle", "rotate=90"), # triangle right
146-
"4": ("triangle", "rotate=90"),
147-
"s": ("square", None),
148-
"p": ("pentagon", None),
149-
"*": ("asterisk", None),
150-
"h": ("star", None), # hexagon 1
151-
"H": ("star", None), # hexagon 2
152-
"d": ("diamond", None), # diamond
153-
"D": ("diamond", None), # thin diamond
154-
"|": ("|", None), # vertical line
155-
"_": ("-", None), # horizontal line
156-
}
157-
158-
159-
def _mpl_marker2pgfp_marker(data, mpl_marker, marker_face_color):
160-
"""Translates a marker style of matplotlib to the corresponding style
161-
in PGFPlots.
162-
"""
163-
# try default list
164-
try:
165-
pgfplots_marker = _MP_MARKER2PGF_MARKER[mpl_marker]
166-
except KeyError:
167-
pass
168-
else:
169-
if (marker_face_color is not None) and pgfplots_marker == "o":
170-
pgfplots_marker = "*"
171-
data["tikz libs"].add("plotmarks")
172-
marker_options = None
173-
return (data, pgfplots_marker, marker_options)
174-
175-
# try plotmarks list
176-
try:
177-
data["tikz libs"].add("plotmarks")
178-
pgfplots_marker, marker_options = _MP_MARKER2PLOTMARKS[mpl_marker]
179-
except KeyError:
180-
# There's no equivalent for the pixel marker (,) in Pgfplots.
181-
pass
182-
else:
183-
if (
184-
marker_face_color is not None
185-
and (
186-
not isinstance(marker_face_color, str)
187-
or marker_face_color.lower() != "none"
188-
)
189-
and pgfplots_marker not in ["|", "-", "asterisk", "star"]
190-
):
191-
pgfplots_marker += "*"
192-
return (data, pgfplots_marker, marker_options)
193-
194-
return data, None, None
195-
196-
197127
def _marker(
198128
obj,
199129
data,
@@ -227,8 +157,7 @@ def _marker(
227157
)
228158

229159
mark_options = ["solid"]
230-
if extra_mark_options:
231-
mark_options.append(extra_mark_options)
160+
mark_options += extra_mark_options
232161
if marker_face_color is None or (
233162
isinstance(marker_face_color, str) and marker_face_color == "none"
234163
):

tikzplotlib/path.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import matplotlib as mpl
22
import numpy
3+
from matplotlib.markers import MarkerStyle
34

45
from . import color
56
from .axes import _mpl_cmap2pgf_cmap
67
from .util import get_legend_text, has_legend
8+
from ._markers import _mpl_marker2pgfp_marker
79

810

911
def draw_path(data, path, draw_options=None, simplify=None):
@@ -135,6 +137,7 @@ def draw_pathcollection(data, obj):
135137
ec = None
136138
fc = None
137139
ls = None
140+
marker0 = None
138141
else:
139142
# gather the draw options
140143
try:
@@ -152,17 +155,37 @@ def draw_pathcollection(data, obj):
152155
except (TypeError, IndexError):
153156
ls = None
154157

155-
# TODO marker info
156-
# <https://github.com/nschloe/tikzplotlib/issues/324>
157-
# <https://github.com/matplotlib/matplotlib/issues/15496>
158+
# "solution" from
159+
# <https://github.com/matplotlib/matplotlib/issues/4672#issuecomment-378702670>
160+
p = obj.get_paths()[0]
161+
ms = {style: MarkerStyle(style) for style in MarkerStyle.markers}
162+
paths = {
163+
style: marker.get_path().transformed(marker.get_transform())
164+
for style, marker in ms.items()
165+
}
166+
marker0 = None
167+
for marker, path in paths.items():
168+
if (
169+
numpy.all(path.codes == p.codes)
170+
and (path.vertices.shape == p.vertices.shape)
171+
and numpy.max(numpy.abs(path.vertices - p.vertices)) < 1.0e-10
172+
):
173+
marker0 = marker
174+
break
158175

159176
is_contour = len(dd) == 1
160177
if is_contour:
161178
draw_options = ["draw=none"]
162179

180+
if marker0 is not None:
181+
data, pgfplots_marker, marker_options = _mpl_marker2pgfp_marker(
182+
data, marker0, fc
183+
)
184+
draw_options += ["marker={}".format(pgfplots_marker)] + marker_options
185+
163186
# `only mark` plots don't need linewidth
164187
data, extra_draw_options = get_draw_options(data, obj, ec, fc, ls, None)
165-
draw_options.extend(extra_draw_options)
188+
draw_options += extra_draw_options
166189

167190
if obj.get_cmap():
168191
mycolormap, is_custom_cmap = _mpl_cmap2pgf_cmap(obj.get_cmap(), data)

0 commit comments

Comments
 (0)