Skip to content

Commit

Permalink
Add support for transparent contours (#9)
Browse files Browse the repository at this point in the history
Add support for transparent contours in plot_contours and plot (with the alpha kwarg)
  • Loading branch information
Stefan-Heimersheim authored Feb 5, 2021
1 parent 9d79a3f commit ba08dfe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion fgivenx/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def plot(x, y, z, ax=None, **kwargs):
Color scheme to plot with. Recommend plotting in reverse
(Default: :class:`matplotlib.pyplot.cm.Reds_r`)
alpha: float, optional
Transparency of filled contours. Given as alpha blending
value between 0 (transparent) and 1 (opague).
smooth: float, optional
Percentage by which to smooth the contours.
(Default: no smoothing)
Expand Down Expand Up @@ -72,6 +76,8 @@ def plot(x, y, z, ax=None, **kwargs):

rasterize_contours = kwargs.pop('rasterize_contours', False)

alpha = kwargs.pop('alpha', 1)

lines = kwargs.pop('lines', True)

if kwargs:
Expand All @@ -86,7 +92,8 @@ def plot(x, y, z, ax=None, **kwargs):
z = scipy.ndimage.gaussian_filter(z, sigma=sigma, order=0)

# Plot the filled contours onto the axis ax
cbar = ax.contourf(x, y, z, cmap=colors, levels=contour_color_levels)
cbar = ax.contourf(x, y, z, cmap=colors, levels=contour_color_levels,
alpha=alpha)

# Rasterize contours (the rest of the figure stays in vector format)
if rasterize_contours:
Expand Down
8 changes: 8 additions & 0 deletions fgivenx/test/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ def test_plot():
assert type(cbar) is matplotlib.contour.QuadContourSet


def test_plot_transparent():
x, y, z = gen_plot_data()

fig, ax = plt.subplots()
cbar = plot(x, y, z, ax, alpha=0.7)
assert type(cbar) is matplotlib.contour.QuadContourSet


def test_plot_wrong_argument():
x, y, z = gen_plot_data()
with pytest.raises(TypeError):
Expand Down

0 comments on commit ba08dfe

Please sign in to comment.