Skip to content

Commit

Permalink
Replacing multicolor with matplotlib-only example
Browse files Browse the repository at this point in the history
  • Loading branch information
tdegeus committed Nov 26, 2021
1 parent 59d7bfe commit 136d098
Show file tree
Hide file tree
Showing 4 changed files with 971 additions and 52 deletions.
52 changes: 0 additions & 52 deletions GooseMPL/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
import matplotlib.pyplot as plt
import numpy as np
import yaml
from matplotlib.collections import LineCollection
from matplotlib.colors import BoundaryNorm
from matplotlib.colors import ListedColormap
from numpy.typing import ArrayLike
from scipy.optimize import curve_fit

Expand Down Expand Up @@ -204,55 +201,6 @@ def latex_float(number, fmt="{0:.2g}"):
return float_str


def plotmulticolor(x: ArrayLike, y: ArrayLike, cmap, axis: plt.Axes = None, dashes=100, **kwargs):
"""
Plot with a line in which multiple colors.
.. tip::
Set the x- and y-limits before calling this function to get a proper calculation of
the size of the dashes.
:param x: x-coordinates.
:param y: y-coordinates.
:param cmap: Colormap.
:param axis: Apply ticks/labels to an axis.
:param dashes: Number of dashes to use on a diagonal line.
"""

ax = axis if axis else plt.gca()
x = np.array(x)
y = np.array(y)

if isinstance(cmap, list):
cmap = ListedColormap(cmap)
if isinstance(cmap, np.ndarray):
cmap = ListedColormap(cmap)

norm = BoundaryNorm(np.arange(cmap.N + 1) - 0.5, cmap.N)

xp = []
yp = []
cp = []

for i in range(len(x) - 1):
xi = [x[i], x[i + 1]]
yi = [y[i], y[i + 1]]
d = np.sqrt(
np.diff(ax.transLimits.transform(xi)) ** 2 + np.diff(ax.transLimits.transform(yi)) ** 2
)
n = int(d / np.sqrt(2) * dashes)
xp += np.linspace(xi[0], xi[1], n).tolist()
yp += np.linspace(yi[0], yi[1], n).tolist()
cp += np.tile(np.arange(cmap.N), int(np.ceil(n / cmap.N)))[: n - 1].tolist()

points = np.array([xp, yp]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
lc = LineCollection(segments, cmap=cmap, norm=norm)
lc.set_array(np.array(cp))
return ax.add_collection(lc)


def log_ticks(
lim: tuple(int, int) = None,
keep: list = None,
Expand Down
15 changes: 15 additions & 0 deletions docs/examples/pyplot/multicolor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import matplotlib.pyplot as plt
import numpy as np

plt.style.use(["goose", "goose-latex"])

x = np.linspace(0, 2 * np.pi, 1000)
y = np.sin(x)

fig, ax = plt.subplots()

ax.plot(x, y, linestyle=(0, (10, 20)), color="r")
ax.plot(x, y, linestyle=(10, (10, 20)), color="g")
ax.plot(x, y, linestyle=(20, (10, 20)), color="b")

plt.savefig("multicolor.svg")

0 comments on commit 136d098

Please sign in to comment.