Skip to content

Commit

Permalink
Merge pull request #344 from nschloe/scatter-markers
Browse files Browse the repository at this point in the history
fix marker handling for paths
  • Loading branch information
nschloe committed Oct 23, 2019
2 parents 6b34e2a + adef689 commit 133154a
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 82 deletions.
1 change: 1 addition & 0 deletions test/test_image_plot_upper_reference.tex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
x grid style={white!69.01960784313725!black},
xmin=-0.5, xmax=511.5,
xtick style={color=black},
y dir=reverse,
y grid style={white!69.01960784313725!black},
ymin=-0.5, ymax=511.5,
ytick style={color=black}
Expand Down
2 changes: 1 addition & 1 deletion test/test_legend_line_scatter_reference.tex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
ymin=-0.239285714285714, ymax=4.23928571428571,
ytick style={color=black}
]
\addplot [only marks, draw=color0, fill=color0, colormap/viridis]
\addplot [only marks, marker=*, draw=color0, fill=color0, colormap/viridis]
table{%
x y
0 0
Expand Down
2 changes: 1 addition & 1 deletion test/test_marker_reference.tex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
ymin=-1.1, ymax=1.1,
ytick style={color=black}
]
\addplot [only marks, draw=black, fill=black, colormap/viridis]
\addplot [only marks, marker=+, draw=black, fill=black, colormap/viridis]
table{%
x y
0 0
Expand Down
2 changes: 1 addition & 1 deletion test/test_pandas_dataframe.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import matplotlib.pyplot as plt

import pandas as pd

from helpers import assert_equality


Expand Down
2 changes: 1 addition & 1 deletion test/test_scatter_reference.tex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
ymin=0.162919838390652, ymax=112.82609249885,
ytick style={color=black}
]
\addplot [only marks, draw=color0, fill=color0, colormap/viridis]
\addplot [only marks, marker=*, draw=color0, fill=color0, colormap/viridis]
table{%
x y
0 10.4470377839679
Expand Down
3 changes: 3 additions & 0 deletions test/test_subplots_with_colorbars_reference.tex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
x grid style={white!69.01960784313725!black},
xmin=-0.5, xmax=2.5,
xtick style={color=black},
y dir=reverse,
y grid style={white!69.01960784313725!black},
ymin=-0.5, ymax=2.5,
ytick style={color=black}
Expand All @@ -27,6 +28,7 @@
x grid style={white!69.01960784313725!black},
xmin=-0.5, xmax=2.5,
xtick style={color=black},
y dir=reverse,
y grid style={white!69.01960784313725!black},
ymin=-0.5, ymax=2.5,
ytick style={color=black}
Expand All @@ -43,6 +45,7 @@
x grid style={white!69.01960784313725!black},
xmin=-0.5, xmax=2.5,
xtick style={color=black},
y dir=reverse,
y grid style={white!69.01960784313725!black},
ymin=-0.5, ymax=2.5,
ytick style={color=black}
Expand Down
2 changes: 1 addition & 1 deletion tikzplotlib/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
__email__ = "nico.schloemer@gmail.com"
__copyright__ = "Copyright (c) 2010-2019, {} <{}>".format(__author__, __email__)
__license__ = "License :: OSI Approved :: MIT License"
__version__ = "0.8.3"
__version__ = "0.8.4"
__status__ = "Development Status :: 5 - Production/Stable"
69 changes: 69 additions & 0 deletions tikzplotlib/_markers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# for matplotlib markers, see: http://matplotlib.org/api/markers_api.html
_MP_MARKER2PGF_MARKER = {
".": "*", # point
"o": "o", # circle
"+": "+", # plus
"x": "x", # x
"None": None,
" ": None,
"": None,
}

# the following markers are only available with PGF's plotmarks library
_MP_MARKER2PLOTMARKS = {
"v": ("triangle", ["rotate=180"]), # triangle down
"1": ("triangle", ["rotate=180"]),
"^": ("triangle", []), # triangle up
"2": ("triangle", []),
"<": ("triangle", ["rotate=270"]), # triangle left
"3": ("triangle", ["rotate=270"]),
">": ("triangle", ["rotate=90"]), # triangle right
"4": ("triangle", ["rotate=90"]),
"s": ("square", []),
"p": ("pentagon", []),
"*": ("asterisk", []),
"h": ("star", []), # hexagon 1
"H": ("star", []), # hexagon 2
"d": ("diamond", []), # diamond
"D": ("diamond", []), # thin diamond
"|": ("|", []), # vertical line
"_": ("-", []), # horizontal line
}


def _mpl_marker2pgfp_marker(data, mpl_marker, marker_face_color):
"""Translates a marker style of matplotlib to the corresponding style
in PGFPlots.
"""
# try default list
try:
pgfplots_marker = _MP_MARKER2PGF_MARKER[mpl_marker]
except KeyError:
pass
else:
if (marker_face_color is not None) and pgfplots_marker == "o":
pgfplots_marker = "*"
data["tikz libs"].add("plotmarks")
marker_options = []
return data, pgfplots_marker, marker_options

# try plotmarks list
try:
data["tikz libs"].add("plotmarks")
pgfplots_marker, marker_options = _MP_MARKER2PLOTMARKS[mpl_marker]
except KeyError:
# There's no equivalent for the pixel marker (,) in Pgfplots.
pass
else:
if (
marker_face_color is not None
and (
not isinstance(marker_face_color, str)
or marker_face_color.lower() != "none"
)
and pgfplots_marker not in ["|", "-", "asterisk", "star"]
):
pgfplots_marker += "*"
return data, pgfplots_marker, marker_options

return data, None, []
75 changes: 2 additions & 73 deletions tikzplotlib/line2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from . import files
from . import path as mypath
from .util import get_legend_text, has_legend, transform_to_data_coordinates
from ._markers import _mpl_marker2pgfp_marker


def draw_line2d(data, obj):
Expand Down Expand Up @@ -123,77 +124,6 @@ def draw_linecollection(data, obj):
return data, content


# for matplotlib markers, see: http://matplotlib.org/api/markers_api.html
_MP_MARKER2PGF_MARKER = {
".": "*", # point
"o": "o", # circle
"+": "+", # plus
"x": "x", # x
"None": None,
" ": None,
"": None,
}

# the following markers are only available with PGF's plotmarks library
_MP_MARKER2PLOTMARKS = {
"v": ("triangle", "rotate=180"), # triangle down
"1": ("triangle", "rotate=180"),
"^": ("triangle", None), # triangle up
"2": ("triangle", None),
"<": ("triangle", "rotate=270"), # triangle left
"3": ("triangle", "rotate=270"),
">": ("triangle", "rotate=90"), # triangle right
"4": ("triangle", "rotate=90"),
"s": ("square", None),
"p": ("pentagon", None),
"*": ("asterisk", None),
"h": ("star", None), # hexagon 1
"H": ("star", None), # hexagon 2
"d": ("diamond", None), # diamond
"D": ("diamond", None), # thin diamond
"|": ("|", None), # vertical line
"_": ("-", None), # horizontal line
}


def _mpl_marker2pgfp_marker(data, mpl_marker, marker_face_color):
"""Translates a marker style of matplotlib to the corresponding style
in PGFPlots.
"""
# try default list
try:
pgfplots_marker = _MP_MARKER2PGF_MARKER[mpl_marker]
except KeyError:
pass
else:
if (marker_face_color is not None) and pgfplots_marker == "o":
pgfplots_marker = "*"
data["tikz libs"].add("plotmarks")
marker_options = None
return (data, pgfplots_marker, marker_options)

# try plotmarks list
try:
data["tikz libs"].add("plotmarks")
pgfplots_marker, marker_options = _MP_MARKER2PLOTMARKS[mpl_marker]
except KeyError:
# There's no equivalent for the pixel marker (,) in Pgfplots.
pass
else:
if (
marker_face_color is not None
and (
not isinstance(marker_face_color, str)
or marker_face_color.lower() != "none"
)
and pgfplots_marker not in ["|", "-", "asterisk", "star"]
):
pgfplots_marker += "*"
return (data, pgfplots_marker, marker_options)

return data, None, None


def _marker(
obj,
data,
Expand Down Expand Up @@ -227,8 +157,7 @@ def _marker(
)

mark_options = ["solid"]
if extra_mark_options:
mark_options.append(extra_mark_options)
mark_options += extra_mark_options
if marker_face_color is None or (
isinstance(marker_face_color, str) and marker_face_color == "none"
):
Expand Down
31 changes: 27 additions & 4 deletions tikzplotlib/path.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import matplotlib as mpl
import numpy
from matplotlib.markers import MarkerStyle

from . import color
from .axes import _mpl_cmap2pgf_cmap
from .util import get_legend_text, has_legend
from ._markers import _mpl_marker2pgfp_marker


def draw_path(data, path, draw_options=None, simplify=None):
Expand Down Expand Up @@ -135,6 +137,7 @@ def draw_pathcollection(data, obj):
ec = None
fc = None
ls = None
marker0 = None
else:
# gather the draw options
try:
Expand All @@ -152,17 +155,37 @@ def draw_pathcollection(data, obj):
except (TypeError, IndexError):
ls = None

# TODO marker info
# <https://github.com/nschloe/tikzplotlib/issues/324>
# <https://github.com/matplotlib/matplotlib/issues/15496>
# "solution" from
# <https://github.com/matplotlib/matplotlib/issues/4672#issuecomment-378702670>
p = obj.get_paths()[0]
ms = {style: MarkerStyle(style) for style in MarkerStyle.markers}
paths = {
style: marker.get_path().transformed(marker.get_transform())
for style, marker in ms.items()
}
marker0 = None
for marker, path in paths.items():
if (
numpy.all(path.codes == p.codes)
and (path.vertices.shape == p.vertices.shape)
and numpy.max(numpy.abs(path.vertices - p.vertices)) < 1.0e-10
):
marker0 = marker
break

is_contour = len(dd) == 1
if is_contour:
draw_options = ["draw=none"]

if marker0 is not None:
data, pgfplots_marker, marker_options = _mpl_marker2pgfp_marker(
data, marker0, fc
)
draw_options += ["marker={}".format(pgfplots_marker)] + marker_options

# `only mark` plots don't need linewidth
data, extra_draw_options = get_draw_options(data, obj, ec, fc, ls, None)
draw_options.extend(extra_draw_options)
draw_options += extra_draw_options

if obj.get_cmap():
mycolormap, is_custom_cmap = _mpl_cmap2pgf_cmap(obj.get_cmap(), data)
Expand Down

0 comments on commit 133154a

Please sign in to comment.