new ip
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
# Check that the test directories exist.
|
||||
if not (Path(__file__).parent / 'baseline_images').exists():
|
||||
raise IOError(
|
||||
'The baseline image directory does not exist. '
|
||||
'This is most likely because the test data is not installed. '
|
||||
'You may need to install matplotlib from source to get the '
|
||||
'test data.')
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
|
||||
from matplotlib.testing.conftest import ( # noqa
|
||||
mpl_test_settings, pytest_configure, pytest_unconfigure, pd, xr)
|
||||
@@ -0,0 +1,137 @@
|
||||
from io import BytesIO
|
||||
import pytest
|
||||
import logging
|
||||
|
||||
from matplotlib import _afm
|
||||
from matplotlib import font_manager as fm
|
||||
|
||||
|
||||
# See note in afm.py re: use of comma as decimal separator in the
|
||||
# UnderlineThickness field and re: use of non-ASCII characters in the Notice
|
||||
# field.
|
||||
AFM_TEST_DATA = b"""StartFontMetrics 2.0
|
||||
Comment Comments are ignored.
|
||||
Comment Creation Date:Mon Nov 13 12:34:11 GMT 2017
|
||||
FontName MyFont-Bold
|
||||
EncodingScheme FontSpecific
|
||||
FullName My Font Bold
|
||||
FamilyName Test Fonts
|
||||
Weight Bold
|
||||
ItalicAngle 0.0
|
||||
IsFixedPitch false
|
||||
UnderlinePosition -100
|
||||
UnderlineThickness 56,789
|
||||
Version 001.000
|
||||
Notice Copyright \xa9 2017 No one.
|
||||
FontBBox 0 -321 1234 369
|
||||
StartCharMetrics 3
|
||||
C 0 ; WX 250 ; N space ; B 0 0 0 0 ;
|
||||
C 42 ; WX 1141 ; N foo ; B 40 60 800 360 ;
|
||||
C 99 ; WX 583 ; N bar ; B 40 -10 543 210 ;
|
||||
EndCharMetrics
|
||||
EndFontMetrics
|
||||
"""
|
||||
|
||||
|
||||
def test_nonascii_str():
|
||||
# This tests that we also decode bytes as utf-8 properly.
|
||||
# Else, font files with non ascii characters fail to load.
|
||||
inp_str = "привет"
|
||||
byte_str = inp_str.encode("utf8")
|
||||
|
||||
ret = _afm._to_str(byte_str)
|
||||
assert ret == inp_str
|
||||
|
||||
|
||||
def test_parse_header():
|
||||
fh = BytesIO(AFM_TEST_DATA)
|
||||
header = _afm._parse_header(fh)
|
||||
assert header == {
|
||||
b'StartFontMetrics': 2.0,
|
||||
b'FontName': 'MyFont-Bold',
|
||||
b'EncodingScheme': 'FontSpecific',
|
||||
b'FullName': 'My Font Bold',
|
||||
b'FamilyName': 'Test Fonts',
|
||||
b'Weight': 'Bold',
|
||||
b'ItalicAngle': 0.0,
|
||||
b'IsFixedPitch': False,
|
||||
b'UnderlinePosition': -100,
|
||||
b'UnderlineThickness': 56.789,
|
||||
b'Version': '001.000',
|
||||
b'Notice': b'Copyright \xa9 2017 No one.',
|
||||
b'FontBBox': [0, -321, 1234, 369],
|
||||
b'StartCharMetrics': 3,
|
||||
}
|
||||
|
||||
|
||||
def test_parse_char_metrics():
|
||||
fh = BytesIO(AFM_TEST_DATA)
|
||||
_afm._parse_header(fh) # position
|
||||
metrics = _afm._parse_char_metrics(fh)
|
||||
assert metrics == (
|
||||
{0: (250.0, 'space', [0, 0, 0, 0]),
|
||||
42: (1141.0, 'foo', [40, 60, 800, 360]),
|
||||
99: (583.0, 'bar', [40, -10, 543, 210]),
|
||||
},
|
||||
{'space': (250.0, 'space', [0, 0, 0, 0]),
|
||||
'foo': (1141.0, 'foo', [40, 60, 800, 360]),
|
||||
'bar': (583.0, 'bar', [40, -10, 543, 210]),
|
||||
})
|
||||
|
||||
|
||||
def test_get_familyname_guessed():
|
||||
fh = BytesIO(AFM_TEST_DATA)
|
||||
font = _afm.AFM(fh)
|
||||
del font._header[b'FamilyName'] # remove FamilyName, so we have to guess
|
||||
assert font.get_familyname() == 'My Font'
|
||||
|
||||
|
||||
def test_font_manager_weight_normalization():
|
||||
font = _afm.AFM(BytesIO(
|
||||
AFM_TEST_DATA.replace(b"Weight Bold\n", b"Weight Custom\n")))
|
||||
assert fm.afmFontProperty("", font).weight == "normal"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"afm_data",
|
||||
[
|
||||
b"""nope
|
||||
really nope""",
|
||||
b"""StartFontMetrics 2.0
|
||||
Comment Comments are ignored.
|
||||
Comment Creation Date:Mon Nov 13 12:34:11 GMT 2017
|
||||
FontName MyFont-Bold
|
||||
EncodingScheme FontSpecific""",
|
||||
],
|
||||
)
|
||||
def test_bad_afm(afm_data):
|
||||
fh = BytesIO(afm_data)
|
||||
with pytest.raises(RuntimeError):
|
||||
_afm._parse_header(fh)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"afm_data",
|
||||
[
|
||||
b"""StartFontMetrics 2.0
|
||||
Comment Comments are ignored.
|
||||
Comment Creation Date:Mon Nov 13 12:34:11 GMT 2017
|
||||
Aardvark bob
|
||||
FontName MyFont-Bold
|
||||
EncodingScheme FontSpecific
|
||||
StartCharMetrics 3""",
|
||||
b"""StartFontMetrics 2.0
|
||||
Comment Comments are ignored.
|
||||
Comment Creation Date:Mon Nov 13 12:34:11 GMT 2017
|
||||
ItalicAngle zero degrees
|
||||
FontName MyFont-Bold
|
||||
EncodingScheme FontSpecific
|
||||
StartCharMetrics 3""",
|
||||
],
|
||||
)
|
||||
def test_malformed_header(afm_data, caplog):
|
||||
fh = BytesIO(afm_data)
|
||||
with caplog.at_level(logging.ERROR):
|
||||
_afm._parse_header(fh)
|
||||
|
||||
assert len(caplog.records) == 1
|
||||
@@ -0,0 +1,338 @@
|
||||
import io
|
||||
|
||||
import numpy as np
|
||||
from numpy.testing import assert_array_almost_equal
|
||||
from PIL import Image, TiffTags
|
||||
import pytest
|
||||
|
||||
|
||||
from matplotlib import (
|
||||
collections, patheffects, pyplot as plt, transforms as mtransforms,
|
||||
rcParams, rc_context)
|
||||
from matplotlib.backends.backend_agg import RendererAgg
|
||||
from matplotlib.figure import Figure
|
||||
from matplotlib.image import imread
|
||||
from matplotlib.path import Path
|
||||
from matplotlib.testing.decorators import image_comparison
|
||||
from matplotlib.transforms import IdentityTransform
|
||||
|
||||
|
||||
def test_repeated_save_with_alpha():
|
||||
# We want an image which has a background color of bluish green, with an
|
||||
# alpha of 0.25.
|
||||
|
||||
fig = Figure([1, 0.4])
|
||||
fig.set_facecolor((0, 1, 0.4))
|
||||
fig.patch.set_alpha(0.25)
|
||||
|
||||
# The target color is fig.patch.get_facecolor()
|
||||
|
||||
buf = io.BytesIO()
|
||||
|
||||
fig.savefig(buf,
|
||||
facecolor=fig.get_facecolor(),
|
||||
edgecolor='none')
|
||||
|
||||
# Save the figure again to check that the
|
||||
# colors don't bleed from the previous renderer.
|
||||
buf.seek(0)
|
||||
fig.savefig(buf,
|
||||
facecolor=fig.get_facecolor(),
|
||||
edgecolor='none')
|
||||
|
||||
# Check the first pixel has the desired color & alpha
|
||||
# (approx: 0, 1.0, 0.4, 0.25)
|
||||
buf.seek(0)
|
||||
assert_array_almost_equal(tuple(imread(buf)[0, 0]),
|
||||
(0.0, 1.0, 0.4, 0.250),
|
||||
decimal=3)
|
||||
|
||||
|
||||
def test_large_single_path_collection():
|
||||
buff = io.BytesIO()
|
||||
|
||||
# Generates a too-large single path in a path collection that
|
||||
# would cause a segfault if the draw_markers optimization is
|
||||
# applied.
|
||||
f, ax = plt.subplots()
|
||||
collection = collections.PathCollection(
|
||||
[Path([[-10, 5], [10, 5], [10, -5], [-10, -5], [-10, 5]])])
|
||||
ax.add_artist(collection)
|
||||
ax.set_xlim(10**-3, 1)
|
||||
plt.savefig(buff)
|
||||
|
||||
|
||||
def test_marker_with_nan():
|
||||
# This creates a marker with nans in it, which was segfaulting the
|
||||
# Agg backend (see #3722)
|
||||
fig, ax = plt.subplots(1)
|
||||
steps = 1000
|
||||
data = np.arange(steps)
|
||||
ax.semilogx(data)
|
||||
ax.fill_between(data, data*0.8, data*1.2)
|
||||
buf = io.BytesIO()
|
||||
fig.savefig(buf, format='png')
|
||||
|
||||
|
||||
def test_long_path():
|
||||
buff = io.BytesIO()
|
||||
fig = Figure()
|
||||
ax = fig.subplots()
|
||||
points = np.ones(100_000)
|
||||
points[::2] *= -1
|
||||
ax.plot(points)
|
||||
fig.savefig(buff, format='png')
|
||||
|
||||
|
||||
@image_comparison(['agg_filter.png'], remove_text=True)
|
||||
def test_agg_filter():
|
||||
def smooth1d(x, window_len):
|
||||
# copied from https://scipy-cookbook.readthedocs.io/
|
||||
s = np.r_[
|
||||
2*x[0] - x[window_len:1:-1], x, 2*x[-1] - x[-1:-window_len:-1]]
|
||||
w = np.hanning(window_len)
|
||||
y = np.convolve(w/w.sum(), s, mode='same')
|
||||
return y[window_len-1:-window_len+1]
|
||||
|
||||
def smooth2d(A, sigma=3):
|
||||
window_len = max(int(sigma), 3) * 2 + 1
|
||||
A = np.apply_along_axis(smooth1d, 0, A, window_len)
|
||||
A = np.apply_along_axis(smooth1d, 1, A, window_len)
|
||||
return A
|
||||
|
||||
class BaseFilter:
|
||||
|
||||
def get_pad(self, dpi):
|
||||
return 0
|
||||
|
||||
def process_image(self, padded_src, dpi):
|
||||
raise NotImplementedError("Should be overridden by subclasses")
|
||||
|
||||
def __call__(self, im, dpi):
|
||||
pad = self.get_pad(dpi)
|
||||
padded_src = np.pad(im, [(pad, pad), (pad, pad), (0, 0)],
|
||||
"constant")
|
||||
tgt_image = self.process_image(padded_src, dpi)
|
||||
return tgt_image, -pad, -pad
|
||||
|
||||
class OffsetFilter(BaseFilter):
|
||||
|
||||
def __init__(self, offsets=(0, 0)):
|
||||
self.offsets = offsets
|
||||
|
||||
def get_pad(self, dpi):
|
||||
return int(max(self.offsets) / 72 * dpi)
|
||||
|
||||
def process_image(self, padded_src, dpi):
|
||||
ox, oy = self.offsets
|
||||
a1 = np.roll(padded_src, int(ox / 72 * dpi), axis=1)
|
||||
a2 = np.roll(a1, -int(oy / 72 * dpi), axis=0)
|
||||
return a2
|
||||
|
||||
class GaussianFilter(BaseFilter):
|
||||
"""Simple Gaussian filter."""
|
||||
|
||||
def __init__(self, sigma, alpha=0.5, color=(0, 0, 0)):
|
||||
self.sigma = sigma
|
||||
self.alpha = alpha
|
||||
self.color = color
|
||||
|
||||
def get_pad(self, dpi):
|
||||
return int(self.sigma*3 / 72 * dpi)
|
||||
|
||||
def process_image(self, padded_src, dpi):
|
||||
tgt_image = np.empty_like(padded_src)
|
||||
tgt_image[:, :, :3] = self.color
|
||||
tgt_image[:, :, 3] = smooth2d(padded_src[:, :, 3] * self.alpha,
|
||||
self.sigma / 72 * dpi)
|
||||
return tgt_image
|
||||
|
||||
class DropShadowFilter(BaseFilter):
|
||||
|
||||
def __init__(self, sigma, alpha=0.3, color=(0, 0, 0), offsets=(0, 0)):
|
||||
self.gauss_filter = GaussianFilter(sigma, alpha, color)
|
||||
self.offset_filter = OffsetFilter(offsets)
|
||||
|
||||
def get_pad(self, dpi):
|
||||
return max(self.gauss_filter.get_pad(dpi),
|
||||
self.offset_filter.get_pad(dpi))
|
||||
|
||||
def process_image(self, padded_src, dpi):
|
||||
t1 = self.gauss_filter.process_image(padded_src, dpi)
|
||||
t2 = self.offset_filter.process_image(t1, dpi)
|
||||
return t2
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
|
||||
# draw lines
|
||||
line1, = ax.plot([0.1, 0.5, 0.9], [0.1, 0.9, 0.5], "bo-",
|
||||
mec="b", mfc="w", lw=5, mew=3, ms=10, label="Line 1")
|
||||
line2, = ax.plot([0.1, 0.5, 0.9], [0.5, 0.2, 0.7], "ro-",
|
||||
mec="r", mfc="w", lw=5, mew=3, ms=10, label="Line 1")
|
||||
|
||||
gauss = DropShadowFilter(4)
|
||||
|
||||
for line in [line1, line2]:
|
||||
|
||||
# draw shadows with same lines with slight offset.
|
||||
xx = line.get_xdata()
|
||||
yy = line.get_ydata()
|
||||
shadow, = ax.plot(xx, yy)
|
||||
shadow.update_from(line)
|
||||
|
||||
# offset transform
|
||||
transform = mtransforms.offset_copy(line.get_transform(), ax.figure,
|
||||
x=4.0, y=-6.0, units='points')
|
||||
shadow.set_transform(transform)
|
||||
|
||||
# adjust zorder of the shadow lines so that it is drawn below the
|
||||
# original lines
|
||||
shadow.set_zorder(line.get_zorder() - 0.5)
|
||||
shadow.set_agg_filter(gauss)
|
||||
shadow.set_rasterized(True) # to support mixed-mode renderers
|
||||
|
||||
ax.set_xlim(0., 1.)
|
||||
ax.set_ylim(0., 1.)
|
||||
|
||||
ax.xaxis.set_visible(False)
|
||||
ax.yaxis.set_visible(False)
|
||||
|
||||
|
||||
def test_too_large_image():
|
||||
fig = plt.figure(figsize=(300, 1000))
|
||||
buff = io.BytesIO()
|
||||
with pytest.raises(ValueError):
|
||||
fig.savefig(buff)
|
||||
|
||||
|
||||
def test_chunksize():
|
||||
x = range(200)
|
||||
|
||||
# Test without chunksize
|
||||
fig, ax = plt.subplots()
|
||||
ax.plot(x, np.sin(x))
|
||||
fig.canvas.draw()
|
||||
|
||||
# Test with chunksize
|
||||
fig, ax = plt.subplots()
|
||||
rcParams['agg.path.chunksize'] = 105
|
||||
ax.plot(x, np.sin(x))
|
||||
fig.canvas.draw()
|
||||
|
||||
|
||||
@pytest.mark.backend('Agg')
|
||||
def test_jpeg_dpi():
|
||||
# Check that dpi is set correctly in jpg files.
|
||||
plt.plot([0, 1, 2], [0, 1, 0])
|
||||
buf = io.BytesIO()
|
||||
plt.savefig(buf, format="jpg", dpi=200)
|
||||
im = Image.open(buf)
|
||||
assert im.info['dpi'] == (200, 200)
|
||||
|
||||
|
||||
def test_pil_kwargs_png():
|
||||
from PIL.PngImagePlugin import PngInfo
|
||||
buf = io.BytesIO()
|
||||
pnginfo = PngInfo()
|
||||
pnginfo.add_text("Software", "test")
|
||||
plt.figure().savefig(buf, format="png", pil_kwargs={"pnginfo": pnginfo})
|
||||
im = Image.open(buf)
|
||||
assert im.info["Software"] == "test"
|
||||
|
||||
|
||||
def test_pil_kwargs_tiff():
|
||||
buf = io.BytesIO()
|
||||
pil_kwargs = {"description": "test image"}
|
||||
plt.figure().savefig(buf, format="tiff", pil_kwargs=pil_kwargs)
|
||||
im = Image.open(buf)
|
||||
tags = {TiffTags.TAGS_V2[k].name: v for k, v in im.tag_v2.items()}
|
||||
assert tags["ImageDescription"] == "test image"
|
||||
|
||||
|
||||
def test_pil_kwargs_webp():
|
||||
plt.plot([0, 1, 2], [0, 1, 0])
|
||||
buf_small = io.BytesIO()
|
||||
pil_kwargs_low = {"quality": 1}
|
||||
plt.savefig(buf_small, format="webp", pil_kwargs=pil_kwargs_low)
|
||||
assert len(pil_kwargs_low) == 1
|
||||
buf_large = io.BytesIO()
|
||||
pil_kwargs_high = {"quality": 100}
|
||||
plt.savefig(buf_large, format="webp", pil_kwargs=pil_kwargs_high)
|
||||
assert len(pil_kwargs_high) == 1
|
||||
assert buf_large.getbuffer().nbytes > buf_small.getbuffer().nbytes
|
||||
|
||||
|
||||
def test_webp_alpha():
|
||||
plt.plot([0, 1, 2], [0, 1, 0])
|
||||
buf = io.BytesIO()
|
||||
plt.savefig(buf, format="webp", transparent=True)
|
||||
im = Image.open(buf)
|
||||
assert im.mode == "RGBA"
|
||||
|
||||
|
||||
def test_draw_path_collection_error_handling():
|
||||
fig, ax = plt.subplots()
|
||||
ax.scatter([1], [1]).set_paths(Path([(0, 1), (2, 3)]))
|
||||
with pytest.raises(TypeError):
|
||||
fig.canvas.draw()
|
||||
|
||||
|
||||
def test_chunksize_fails():
|
||||
# NOTE: This test covers multiple independent test scenarios in a single
|
||||
# function, because each scenario uses ~2GB of memory and we don't
|
||||
# want parallel test executors to accidentally run multiple of these
|
||||
# at the same time.
|
||||
|
||||
N = 100_000
|
||||
dpi = 500
|
||||
w = 5*dpi
|
||||
h = 6*dpi
|
||||
|
||||
# make a Path that spans the whole w-h rectangle
|
||||
x = np.linspace(0, w, N)
|
||||
y = np.ones(N) * h
|
||||
y[::2] = 0
|
||||
path = Path(np.vstack((x, y)).T)
|
||||
# effectively disable path simplification (but leaving it "on")
|
||||
path.simplify_threshold = 0
|
||||
|
||||
# setup the minimal GraphicsContext to draw a Path
|
||||
ra = RendererAgg(w, h, dpi)
|
||||
gc = ra.new_gc()
|
||||
gc.set_linewidth(1)
|
||||
gc.set_foreground('r')
|
||||
|
||||
gc.set_hatch('/')
|
||||
with pytest.raises(OverflowError, match='can not split hatched path'):
|
||||
ra.draw_path(gc, path, IdentityTransform())
|
||||
gc.set_hatch(None)
|
||||
|
||||
with pytest.raises(OverflowError, match='can not split filled path'):
|
||||
ra.draw_path(gc, path, IdentityTransform(), (1, 0, 0))
|
||||
|
||||
# Set to zero to disable, currently defaults to 0, but let's be sure.
|
||||
with rc_context({'agg.path.chunksize': 0}):
|
||||
with pytest.raises(OverflowError, match='Please set'):
|
||||
ra.draw_path(gc, path, IdentityTransform())
|
||||
|
||||
# Set big enough that we do not try to chunk.
|
||||
with rc_context({'agg.path.chunksize': 1_000_000}):
|
||||
with pytest.raises(OverflowError, match='Please reduce'):
|
||||
ra.draw_path(gc, path, IdentityTransform())
|
||||
|
||||
# Small enough we will try to chunk, but big enough we will fail to render.
|
||||
with rc_context({'agg.path.chunksize': 90_000}):
|
||||
with pytest.raises(OverflowError, match='Please reduce'):
|
||||
ra.draw_path(gc, path, IdentityTransform())
|
||||
|
||||
path.should_simplify = False
|
||||
with pytest.raises(OverflowError, match="should_simplify is False"):
|
||||
ra.draw_path(gc, path, IdentityTransform())
|
||||
|
||||
|
||||
def test_non_tuple_rgbaface():
|
||||
# This passes rgbaFace as a ndarray to draw_path.
|
||||
fig = plt.figure()
|
||||
fig.add_subplot(projection="3d").scatter(
|
||||
[0, 1, 2], [0, 1, 2], path_effects=[patheffects.Stroke(linewidth=4)])
|
||||
fig.canvas.draw()
|
||||
@@ -0,0 +1,33 @@
|
||||
import numpy as np
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.testing.decorators import image_comparison
|
||||
|
||||
|
||||
@image_comparison(baseline_images=['agg_filter_alpha'],
|
||||
extensions=['png', 'pdf'])
|
||||
def test_agg_filter_alpha():
|
||||
# Remove this line when this test image is regenerated.
|
||||
plt.rcParams['pcolormesh.snap'] = False
|
||||
|
||||
ax = plt.axes()
|
||||
x, y = np.mgrid[0:7, 0:8]
|
||||
data = x**2 - y**2
|
||||
mesh = ax.pcolormesh(data, cmap='Reds', zorder=5)
|
||||
|
||||
def manual_alpha(im, dpi):
|
||||
im[:, :, 3] *= 0.6
|
||||
print('CALLED')
|
||||
return im, 0, 0
|
||||
|
||||
# Note: Doing alpha like this is not the same as setting alpha on
|
||||
# the mesh itself. Currently meshes are drawn as independent patches,
|
||||
# and we see fine borders around the blocks of color. See the SO
|
||||
# question for an example: https://stackoverflow.com/q/20678817/
|
||||
mesh.set_agg_filter(manual_alpha)
|
||||
|
||||
# Currently we must enable rasterization for this to have an effect in
|
||||
# the PDF backend.
|
||||
mesh.set_rasterized(True)
|
||||
|
||||
ax.plot([0, 4, 7], [1, 3, 8])
|
||||
@@ -0,0 +1,518 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import platform
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import weakref
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
import matplotlib as mpl
|
||||
from matplotlib import pyplot as plt
|
||||
from matplotlib import animation
|
||||
from matplotlib.testing.decorators import check_figures_equal
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def anim(request):
|
||||
"""Create a simple animation (with options)."""
|
||||
fig, ax = plt.subplots()
|
||||
line, = ax.plot([], [])
|
||||
|
||||
ax.set_xlim(0, 10)
|
||||
ax.set_ylim(-1, 1)
|
||||
|
||||
def init():
|
||||
line.set_data([], [])
|
||||
return line,
|
||||
|
||||
def animate(i):
|
||||
x = np.linspace(0, 10, 100)
|
||||
y = np.sin(x + i)
|
||||
line.set_data(x, y)
|
||||
return line,
|
||||
|
||||
# "klass" can be passed to determine the class returned by the fixture
|
||||
kwargs = dict(getattr(request, 'param', {})) # make a copy
|
||||
klass = kwargs.pop('klass', animation.FuncAnimation)
|
||||
if 'frames' not in kwargs:
|
||||
kwargs['frames'] = 5
|
||||
return klass(fig=fig, func=animate, init_func=init, **kwargs)
|
||||
|
||||
|
||||
class NullMovieWriter(animation.AbstractMovieWriter):
|
||||
"""
|
||||
A minimal MovieWriter. It doesn't actually write anything.
|
||||
It just saves the arguments that were given to the setup() and
|
||||
grab_frame() methods as attributes, and counts how many times
|
||||
grab_frame() is called.
|
||||
|
||||
This class doesn't have an __init__ method with the appropriate
|
||||
signature, and it doesn't define an isAvailable() method, so
|
||||
it cannot be added to the 'writers' registry.
|
||||
"""
|
||||
|
||||
def setup(self, fig, outfile, dpi, *args):
|
||||
self.fig = fig
|
||||
self.outfile = outfile
|
||||
self.dpi = dpi
|
||||
self.args = args
|
||||
self._count = 0
|
||||
|
||||
def grab_frame(self, **savefig_kwargs):
|
||||
self.savefig_kwargs = savefig_kwargs
|
||||
self._count += 1
|
||||
|
||||
def finish(self):
|
||||
pass
|
||||
|
||||
|
||||
def test_null_movie_writer(anim):
|
||||
# Test running an animation with NullMovieWriter.
|
||||
plt.rcParams["savefig.facecolor"] = "auto"
|
||||
filename = "unused.null"
|
||||
dpi = 50
|
||||
savefig_kwargs = dict(foo=0)
|
||||
writer = NullMovieWriter()
|
||||
|
||||
anim.save(filename, dpi=dpi, writer=writer,
|
||||
savefig_kwargs=savefig_kwargs)
|
||||
|
||||
assert writer.fig == plt.figure(1) # The figure used by anim fixture
|
||||
assert writer.outfile == filename
|
||||
assert writer.dpi == dpi
|
||||
assert writer.args == ()
|
||||
# we enrich the savefig kwargs to ensure we composite transparent
|
||||
# output to an opaque background
|
||||
for k, v in savefig_kwargs.items():
|
||||
assert writer.savefig_kwargs[k] == v
|
||||
assert writer._count == anim._save_count
|
||||
|
||||
|
||||
@pytest.mark.parametrize('anim', [dict(klass=dict)], indirect=['anim'])
|
||||
def test_animation_delete(anim):
|
||||
if platform.python_implementation() == 'PyPy':
|
||||
# Something in the test setup fixture lingers around into the test and
|
||||
# breaks pytest.warns on PyPy. This garbage collection fixes it.
|
||||
# https://foss.heptapod.net/pypy/pypy/-/issues/3536
|
||||
np.testing.break_cycles()
|
||||
anim = animation.FuncAnimation(**anim)
|
||||
with pytest.warns(Warning, match='Animation was deleted'):
|
||||
del anim
|
||||
np.testing.break_cycles()
|
||||
|
||||
|
||||
def test_movie_writer_dpi_default():
|
||||
class DummyMovieWriter(animation.MovieWriter):
|
||||
def _run(self):
|
||||
pass
|
||||
|
||||
# Test setting up movie writer with figure.dpi default.
|
||||
fig = plt.figure()
|
||||
|
||||
filename = "unused.null"
|
||||
fps = 5
|
||||
codec = "unused"
|
||||
bitrate = 1
|
||||
extra_args = ["unused"]
|
||||
|
||||
writer = DummyMovieWriter(fps, codec, bitrate, extra_args)
|
||||
writer.setup(fig, filename)
|
||||
assert writer.dpi == fig.dpi
|
||||
|
||||
|
||||
@animation.writers.register('null')
|
||||
class RegisteredNullMovieWriter(NullMovieWriter):
|
||||
|
||||
# To be able to add NullMovieWriter to the 'writers' registry,
|
||||
# we must define an __init__ method with a specific signature,
|
||||
# and we must define the class method isAvailable().
|
||||
# (These methods are not actually required to use an instance
|
||||
# of this class as the 'writer' argument of Animation.save().)
|
||||
|
||||
def __init__(self, fps=None, codec=None, bitrate=None,
|
||||
extra_args=None, metadata=None):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def isAvailable(cls):
|
||||
return True
|
||||
|
||||
|
||||
WRITER_OUTPUT = [
|
||||
('ffmpeg', 'movie.mp4'),
|
||||
('ffmpeg_file', 'movie.mp4'),
|
||||
('imagemagick', 'movie.gif'),
|
||||
('imagemagick_file', 'movie.gif'),
|
||||
('pillow', 'movie.gif'),
|
||||
('html', 'movie.html'),
|
||||
('null', 'movie.null')
|
||||
]
|
||||
|
||||
|
||||
def gen_writers():
|
||||
for writer, output in WRITER_OUTPUT:
|
||||
if not animation.writers.is_available(writer):
|
||||
mark = pytest.mark.skip(
|
||||
f"writer '{writer}' not available on this system")
|
||||
yield pytest.param(writer, None, output, marks=[mark])
|
||||
yield pytest.param(writer, None, Path(output), marks=[mark])
|
||||
continue
|
||||
|
||||
writer_class = animation.writers[writer]
|
||||
for frame_format in getattr(writer_class, 'supported_formats', [None]):
|
||||
yield writer, frame_format, output
|
||||
yield writer, frame_format, Path(output)
|
||||
|
||||
|
||||
# Smoke test for saving animations. In the future, we should probably
|
||||
# design more sophisticated tests which compare resulting frames a-la
|
||||
# matplotlib.testing.image_comparison
|
||||
@pytest.mark.parametrize('writer, frame_format, output', gen_writers())
|
||||
@pytest.mark.parametrize('anim', [dict(klass=dict)], indirect=['anim'])
|
||||
def test_save_animation_smoketest(tmpdir, writer, frame_format, output, anim):
|
||||
if frame_format is not None:
|
||||
plt.rcParams["animation.frame_format"] = frame_format
|
||||
anim = animation.FuncAnimation(**anim)
|
||||
dpi = None
|
||||
codec = None
|
||||
if writer == 'ffmpeg':
|
||||
# Issue #8253
|
||||
anim._fig.set_size_inches((10.85, 9.21))
|
||||
dpi = 100.
|
||||
codec = 'h264'
|
||||
|
||||
# Use temporary directory for the file-based writers, which produce a file
|
||||
# per frame with known names.
|
||||
with tmpdir.as_cwd():
|
||||
anim.save(output, fps=30, writer=writer, bitrate=500, dpi=dpi,
|
||||
codec=codec)
|
||||
|
||||
del anim
|
||||
|
||||
|
||||
@pytest.mark.parametrize('writer', [
|
||||
pytest.param(
|
||||
'ffmpeg', marks=pytest.mark.skipif(
|
||||
not animation.FFMpegWriter.isAvailable(),
|
||||
reason='Requires FFMpeg')),
|
||||
pytest.param(
|
||||
'imagemagick', marks=pytest.mark.skipif(
|
||||
not animation.ImageMagickWriter.isAvailable(),
|
||||
reason='Requires ImageMagick')),
|
||||
])
|
||||
@pytest.mark.parametrize('html, want', [
|
||||
('none', None),
|
||||
('html5', '<video width'),
|
||||
('jshtml', '<script ')
|
||||
])
|
||||
@pytest.mark.parametrize('anim', [dict(klass=dict)], indirect=['anim'])
|
||||
def test_animation_repr_html(writer, html, want, anim):
|
||||
if platform.python_implementation() == 'PyPy':
|
||||
# Something in the test setup fixture lingers around into the test and
|
||||
# breaks pytest.warns on PyPy. This garbage collection fixes it.
|
||||
# https://foss.heptapod.net/pypy/pypy/-/issues/3536
|
||||
np.testing.break_cycles()
|
||||
if (writer == 'imagemagick' and html == 'html5'
|
||||
# ImageMagick delegates to ffmpeg for this format.
|
||||
and not animation.FFMpegWriter.isAvailable()):
|
||||
pytest.skip('Requires FFMpeg')
|
||||
# create here rather than in the fixture otherwise we get __del__ warnings
|
||||
# about producing no output
|
||||
anim = animation.FuncAnimation(**anim)
|
||||
with plt.rc_context({'animation.writer': writer,
|
||||
'animation.html': html}):
|
||||
html = anim._repr_html_()
|
||||
if want is None:
|
||||
assert html is None
|
||||
with pytest.warns(UserWarning):
|
||||
del anim # Animation was never run, so will warn on cleanup.
|
||||
np.testing.break_cycles()
|
||||
else:
|
||||
assert want in html
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'anim',
|
||||
[{'save_count': 10, 'frames': iter(range(5))}],
|
||||
indirect=['anim']
|
||||
)
|
||||
def test_no_length_frames(anim):
|
||||
anim.save('unused.null', writer=NullMovieWriter())
|
||||
|
||||
|
||||
def test_movie_writer_registry():
|
||||
assert len(animation.writers._registered) > 0
|
||||
mpl.rcParams['animation.ffmpeg_path'] = "not_available_ever_xxxx"
|
||||
assert not animation.writers.is_available("ffmpeg")
|
||||
# something guaranteed to be available in path and exits immediately
|
||||
bin = "true" if sys.platform != 'win32' else "where"
|
||||
mpl.rcParams['animation.ffmpeg_path'] = bin
|
||||
assert animation.writers.is_available("ffmpeg")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"method_name",
|
||||
[pytest.param("to_html5_video", marks=pytest.mark.skipif(
|
||||
not animation.writers.is_available(mpl.rcParams["animation.writer"]),
|
||||
reason="animation writer not installed")),
|
||||
"to_jshtml"])
|
||||
@pytest.mark.parametrize('anim', [dict(frames=1)], indirect=['anim'])
|
||||
def test_embed_limit(method_name, caplog, tmpdir, anim):
|
||||
caplog.set_level("WARNING")
|
||||
with tmpdir.as_cwd():
|
||||
with mpl.rc_context({"animation.embed_limit": 1e-6}): # ~1 byte.
|
||||
getattr(anim, method_name)()
|
||||
assert len(caplog.records) == 1
|
||||
record, = caplog.records
|
||||
assert (record.name == "matplotlib.animation"
|
||||
and record.levelname == "WARNING")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"method_name",
|
||||
[pytest.param("to_html5_video", marks=pytest.mark.skipif(
|
||||
not animation.writers.is_available(mpl.rcParams["animation.writer"]),
|
||||
reason="animation writer not installed")),
|
||||
"to_jshtml"])
|
||||
@pytest.mark.parametrize('anim', [dict(frames=1)], indirect=['anim'])
|
||||
def test_cleanup_temporaries(method_name, tmpdir, anim):
|
||||
with tmpdir.as_cwd():
|
||||
getattr(anim, method_name)()
|
||||
assert list(Path(str(tmpdir)).iterdir()) == []
|
||||
|
||||
|
||||
@pytest.mark.skipif(os.name != "posix", reason="requires a POSIX OS")
|
||||
def test_failing_ffmpeg(tmpdir, monkeypatch, anim):
|
||||
"""
|
||||
Test that we correctly raise a CalledProcessError when ffmpeg fails.
|
||||
|
||||
To do so, mock ffmpeg using a simple executable shell script that
|
||||
succeeds when called with no arguments (so that it gets registered by
|
||||
`isAvailable`), but fails otherwise, and add it to the $PATH.
|
||||
"""
|
||||
with tmpdir.as_cwd():
|
||||
monkeypatch.setenv("PATH", ".:" + os.environ["PATH"])
|
||||
exe_path = Path(str(tmpdir), "ffmpeg")
|
||||
exe_path.write_bytes(b"#!/bin/sh\n[[ $@ -eq 0 ]]\n")
|
||||
os.chmod(exe_path, 0o755)
|
||||
with pytest.raises(subprocess.CalledProcessError):
|
||||
anim.save("test.mpeg")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("cache_frame_data", [False, True])
|
||||
def test_funcanimation_cache_frame_data(cache_frame_data):
|
||||
fig, ax = plt.subplots()
|
||||
line, = ax.plot([], [])
|
||||
|
||||
class Frame(dict):
|
||||
# this subclassing enables to use weakref.ref()
|
||||
pass
|
||||
|
||||
def init():
|
||||
line.set_data([], [])
|
||||
return line,
|
||||
|
||||
def animate(frame):
|
||||
line.set_data(frame['x'], frame['y'])
|
||||
return line,
|
||||
|
||||
frames_generated = []
|
||||
|
||||
def frames_generator():
|
||||
for _ in range(5):
|
||||
x = np.linspace(0, 10, 100)
|
||||
y = np.random.rand(100)
|
||||
|
||||
frame = Frame(x=x, y=y)
|
||||
|
||||
# collect weak references to frames
|
||||
# to validate their references later
|
||||
frames_generated.append(weakref.ref(frame))
|
||||
|
||||
yield frame
|
||||
|
||||
MAX_FRAMES = 100
|
||||
anim = animation.FuncAnimation(fig, animate, init_func=init,
|
||||
frames=frames_generator,
|
||||
cache_frame_data=cache_frame_data,
|
||||
save_count=MAX_FRAMES)
|
||||
|
||||
writer = NullMovieWriter()
|
||||
anim.save('unused.null', writer=writer)
|
||||
assert len(frames_generated) == 5
|
||||
np.testing.break_cycles()
|
||||
for f in frames_generated:
|
||||
# If cache_frame_data is True, then the weakref should be alive;
|
||||
# if cache_frame_data is False, then the weakref should be dead (None).
|
||||
assert (f() is None) != cache_frame_data
|
||||
|
||||
|
||||
@pytest.mark.parametrize('return_value', [
|
||||
# User forgot to return (returns None).
|
||||
None,
|
||||
# User returned a string.
|
||||
'string',
|
||||
# User returned an int.
|
||||
1,
|
||||
# User returns a sequence of other objects, e.g., string instead of Artist.
|
||||
('string', ),
|
||||
# User forgot to return a sequence (handled in `animate` below.)
|
||||
'artist',
|
||||
])
|
||||
def test_draw_frame(return_value):
|
||||
# test _draw_frame method
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
line, = ax.plot([])
|
||||
|
||||
def animate(i):
|
||||
# general update func
|
||||
line.set_data([0, 1], [0, i])
|
||||
if return_value == 'artist':
|
||||
# *not* a sequence
|
||||
return line
|
||||
else:
|
||||
return return_value
|
||||
|
||||
with pytest.raises(RuntimeError):
|
||||
animation.FuncAnimation(
|
||||
fig, animate, blit=True, cache_frame_data=False
|
||||
)
|
||||
|
||||
|
||||
def test_exhausted_animation(tmpdir):
|
||||
fig, ax = plt.subplots()
|
||||
|
||||
def update(frame):
|
||||
return []
|
||||
|
||||
anim = animation.FuncAnimation(
|
||||
fig, update, frames=iter(range(10)), repeat=False,
|
||||
cache_frame_data=False
|
||||
)
|
||||
|
||||
with tmpdir.as_cwd():
|
||||
anim.save("test.gif", writer='pillow')
|
||||
|
||||
with pytest.warns(UserWarning, match="exhausted"):
|
||||
anim._start()
|
||||
|
||||
|
||||
def test_no_frame_warning(tmpdir):
|
||||
fig, ax = plt.subplots()
|
||||
|
||||
def update(frame):
|
||||
return []
|
||||
|
||||
anim = animation.FuncAnimation(
|
||||
fig, update, frames=[], repeat=False,
|
||||
cache_frame_data=False
|
||||
)
|
||||
|
||||
with pytest.warns(UserWarning, match="exhausted"):
|
||||
anim._start()
|
||||
|
||||
|
||||
@check_figures_equal(extensions=["png"])
|
||||
def test_animation_frame(tmpdir, fig_test, fig_ref):
|
||||
# Test the expected image after iterating through a few frames
|
||||
# we save the animation to get the iteration because we are not
|
||||
# in an interactive framework.
|
||||
ax = fig_test.add_subplot()
|
||||
ax.set_xlim(0, 2 * np.pi)
|
||||
ax.set_ylim(-1, 1)
|
||||
x = np.linspace(0, 2 * np.pi, 100)
|
||||
line, = ax.plot([], [])
|
||||
|
||||
def init():
|
||||
line.set_data([], [])
|
||||
return line,
|
||||
|
||||
def animate(i):
|
||||
line.set_data(x, np.sin(x + i / 100))
|
||||
return line,
|
||||
|
||||
anim = animation.FuncAnimation(
|
||||
fig_test, animate, init_func=init, frames=5,
|
||||
blit=True, repeat=False)
|
||||
with tmpdir.as_cwd():
|
||||
anim.save("test.gif")
|
||||
|
||||
# Reference figure without animation
|
||||
ax = fig_ref.add_subplot()
|
||||
ax.set_xlim(0, 2 * np.pi)
|
||||
ax.set_ylim(-1, 1)
|
||||
|
||||
# 5th frame's data
|
||||
ax.plot(x, np.sin(x + 4 / 100))
|
||||
|
||||
|
||||
@pytest.mark.parametrize('anim', [dict(klass=dict)], indirect=['anim'])
|
||||
def test_save_count_override_warnings_has_length(anim):
|
||||
|
||||
save_count = 5
|
||||
frames = list(range(2))
|
||||
match_target = (
|
||||
f'You passed in an explicit {save_count=} '
|
||||
"which is being ignored in favor of "
|
||||
f"{len(frames)=}."
|
||||
)
|
||||
|
||||
with pytest.warns(UserWarning, match=re.escape(match_target)):
|
||||
anim = animation.FuncAnimation(
|
||||
**{**anim, 'frames': frames, 'save_count': save_count}
|
||||
)
|
||||
assert anim._save_count == len(frames)
|
||||
anim._init_draw()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('anim', [dict(klass=dict)], indirect=['anim'])
|
||||
def test_save_count_override_warnings_scaler(anim):
|
||||
save_count = 5
|
||||
frames = 7
|
||||
match_target = (
|
||||
f'You passed in an explicit {save_count=} ' +
|
||||
"which is being ignored in favor of " +
|
||||
f"{frames=}."
|
||||
)
|
||||
|
||||
with pytest.warns(UserWarning, match=re.escape(match_target)):
|
||||
anim = animation.FuncAnimation(
|
||||
**{**anim, 'frames': frames, 'save_count': save_count}
|
||||
)
|
||||
|
||||
assert anim._save_count == frames
|
||||
anim._init_draw()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('anim', [dict(klass=dict)], indirect=['anim'])
|
||||
def test_disable_cache_warning(anim):
|
||||
cache_frame_data = True
|
||||
frames = iter(range(5))
|
||||
match_target = (
|
||||
f"{frames=!r} which we can infer the length of, "
|
||||
"did not pass an explicit *save_count* "
|
||||
f"and passed {cache_frame_data=}. To avoid a possibly "
|
||||
"unbounded cache, frame data caching has been disabled. "
|
||||
"To suppress this warning either pass "
|
||||
"`cache_frame_data=False` or `save_count=MAX_FRAMES`."
|
||||
)
|
||||
with pytest.warns(UserWarning, match=re.escape(match_target)):
|
||||
anim = animation.FuncAnimation(
|
||||
**{**anim, 'cache_frame_data': cache_frame_data, 'frames': frames}
|
||||
)
|
||||
assert anim._cache_frame_data is False
|
||||
anim._init_draw()
|
||||
|
||||
|
||||
def test_movie_writer_invalid_path(anim):
|
||||
if sys.platform == "win32":
|
||||
match_str = re.escape("[WinError 3] The system cannot find the path specified:")
|
||||
else:
|
||||
match_str = re.escape("[Errno 2] No such file or directory: '/foo")
|
||||
with pytest.raises(FileNotFoundError, match=match_str):
|
||||
anim.save("/foo/bar/aardvark/thiscannotreallyexist.mp4",
|
||||
writer=animation.FFMpegFileWriter())
|
||||
@@ -0,0 +1,100 @@
|
||||
import re
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from matplotlib import _api
|
||||
|
||||
|
||||
@pytest.mark.parametrize('target,test_shape',
|
||||
[((None, ), (1, 3)),
|
||||
((None, 3), (1,)),
|
||||
((None, 3), (1, 2)),
|
||||
((1, 5), (1, 9)),
|
||||
((None, 2, None), (1, 3, 1))
|
||||
])
|
||||
def test_check_shape(target, test_shape):
|
||||
error_pattern = (f"^'aardvark' must be {len(target)}D.*" +
|
||||
re.escape(f'has shape {test_shape}'))
|
||||
data = np.zeros(test_shape)
|
||||
with pytest.raises(ValueError, match=error_pattern):
|
||||
_api.check_shape(target, aardvark=data)
|
||||
|
||||
|
||||
def test_classproperty_deprecation():
|
||||
class A:
|
||||
@_api.deprecated("0.0.0")
|
||||
@_api.classproperty
|
||||
def f(cls):
|
||||
pass
|
||||
with pytest.warns(_api.MatplotlibDeprecationWarning):
|
||||
A.f
|
||||
with pytest.warns(_api.MatplotlibDeprecationWarning):
|
||||
a = A()
|
||||
a.f
|
||||
|
||||
|
||||
def test_deprecate_privatize_attribute():
|
||||
class C:
|
||||
def __init__(self): self._attr = 1
|
||||
def _meth(self, arg): return arg
|
||||
attr = _api.deprecate_privatize_attribute("0.0")
|
||||
meth = _api.deprecate_privatize_attribute("0.0")
|
||||
|
||||
c = C()
|
||||
with pytest.warns(_api.MatplotlibDeprecationWarning):
|
||||
assert c.attr == 1
|
||||
with pytest.warns(_api.MatplotlibDeprecationWarning):
|
||||
c.attr = 2
|
||||
with pytest.warns(_api.MatplotlibDeprecationWarning):
|
||||
assert c.attr == 2
|
||||
with pytest.warns(_api.MatplotlibDeprecationWarning):
|
||||
assert c.meth(42) == 42
|
||||
|
||||
|
||||
def test_delete_parameter():
|
||||
@_api.delete_parameter("3.0", "foo")
|
||||
def func1(foo=None):
|
||||
pass
|
||||
|
||||
@_api.delete_parameter("3.0", "foo")
|
||||
def func2(**kwargs):
|
||||
pass
|
||||
|
||||
for func in [func1, func2]:
|
||||
func() # No warning.
|
||||
with pytest.warns(_api.MatplotlibDeprecationWarning):
|
||||
func(foo="bar")
|
||||
|
||||
def pyplot_wrapper(foo=_api.deprecation._deprecated_parameter):
|
||||
func1(foo)
|
||||
|
||||
pyplot_wrapper() # No warning.
|
||||
with pytest.warns(_api.MatplotlibDeprecationWarning):
|
||||
func(foo="bar")
|
||||
|
||||
|
||||
def test_make_keyword_only():
|
||||
@_api.make_keyword_only("3.0", "arg")
|
||||
def func(pre, arg, post=None):
|
||||
pass
|
||||
|
||||
func(1, arg=2) # Check that no warning is emitted.
|
||||
|
||||
with pytest.warns(_api.MatplotlibDeprecationWarning):
|
||||
func(1, 2)
|
||||
with pytest.warns(_api.MatplotlibDeprecationWarning):
|
||||
func(1, 2, 3)
|
||||
|
||||
|
||||
def test_deprecation_alternative():
|
||||
alternative = "`.f1`, `f2`, `f3(x) <.f3>` or `f4(x)<f4>`"
|
||||
@_api.deprecated("1", alternative=alternative)
|
||||
def f():
|
||||
pass
|
||||
assert alternative in f.__doc__
|
||||
|
||||
|
||||
def test_empty_check_in_list():
|
||||
with pytest.raises(TypeError, match="No argument to check!"):
|
||||
_api.check_in_list(["a"])
|
||||
@@ -0,0 +1,177 @@
|
||||
import pytest
|
||||
import platform
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.testing.decorators import image_comparison
|
||||
import matplotlib.patches as mpatches
|
||||
|
||||
|
||||
def draw_arrow(ax, t, r):
|
||||
ax.annotate('', xy=(0.5, 0.5 + r), xytext=(0.5, 0.5), size=30,
|
||||
arrowprops=dict(arrowstyle=t,
|
||||
fc="b", ec='k'))
|
||||
|
||||
|
||||
@image_comparison(['fancyarrow_test_image'])
|
||||
def test_fancyarrow():
|
||||
# Added 0 to test division by zero error described in issue 3930
|
||||
r = [0.4, 0.3, 0.2, 0.1, 0]
|
||||
t = ["fancy", "simple", mpatches.ArrowStyle.Fancy()]
|
||||
|
||||
fig, axs = plt.subplots(len(t), len(r), squeeze=False,
|
||||
figsize=(8, 4.5), subplot_kw=dict(aspect=1))
|
||||
|
||||
for i_r, r1 in enumerate(r):
|
||||
for i_t, t1 in enumerate(t):
|
||||
ax = axs[i_t, i_r]
|
||||
draw_arrow(ax, t1, r1)
|
||||
ax.tick_params(labelleft=False, labelbottom=False)
|
||||
|
||||
|
||||
@image_comparison(['boxarrow_test_image.png'])
|
||||
def test_boxarrow():
|
||||
|
||||
styles = mpatches.BoxStyle.get_styles()
|
||||
|
||||
n = len(styles)
|
||||
spacing = 1.2
|
||||
|
||||
figheight = (n * spacing + .5)
|
||||
fig = plt.figure(figsize=(4 / 1.5, figheight / 1.5))
|
||||
|
||||
fontsize = 0.3 * 72
|
||||
|
||||
for i, stylename in enumerate(sorted(styles)):
|
||||
fig.text(0.5, ((n - i) * spacing - 0.5)/figheight, stylename,
|
||||
ha="center",
|
||||
size=fontsize,
|
||||
transform=fig.transFigure,
|
||||
bbox=dict(boxstyle=stylename, fc="w", ec="k"))
|
||||
|
||||
|
||||
def __prepare_fancyarrow_dpi_cor_test():
|
||||
"""
|
||||
Convenience function that prepares and returns a FancyArrowPatch. It aims
|
||||
at being used to test that the size of the arrow head does not depend on
|
||||
the DPI value of the exported picture.
|
||||
|
||||
NB: this function *is not* a test in itself!
|
||||
"""
|
||||
fig2 = plt.figure("fancyarrow_dpi_cor_test", figsize=(4, 3), dpi=50)
|
||||
ax = fig2.add_subplot()
|
||||
ax.set_xlim([0, 1])
|
||||
ax.set_ylim([0, 1])
|
||||
ax.add_patch(mpatches.FancyArrowPatch(posA=(0.3, 0.4), posB=(0.8, 0.6),
|
||||
lw=3, arrowstyle='->',
|
||||
mutation_scale=100))
|
||||
return fig2
|
||||
|
||||
|
||||
@image_comparison(['fancyarrow_dpi_cor_100dpi.png'], remove_text=True,
|
||||
tol=0 if platform.machine() == 'x86_64' else 0.02,
|
||||
savefig_kwarg=dict(dpi=100))
|
||||
def test_fancyarrow_dpi_cor_100dpi():
|
||||
"""
|
||||
Check the export of a FancyArrowPatch @ 100 DPI. FancyArrowPatch is
|
||||
instantiated through a dedicated function because another similar test
|
||||
checks a similar export but with a different DPI value.
|
||||
|
||||
Remark: test only a rasterized format.
|
||||
"""
|
||||
|
||||
__prepare_fancyarrow_dpi_cor_test()
|
||||
|
||||
|
||||
@image_comparison(['fancyarrow_dpi_cor_200dpi.png'], remove_text=True,
|
||||
tol=0 if platform.machine() == 'x86_64' else 0.02,
|
||||
savefig_kwarg=dict(dpi=200))
|
||||
def test_fancyarrow_dpi_cor_200dpi():
|
||||
"""
|
||||
As test_fancyarrow_dpi_cor_100dpi, but exports @ 200 DPI. The relative size
|
||||
of the arrow head should be the same.
|
||||
"""
|
||||
|
||||
__prepare_fancyarrow_dpi_cor_test()
|
||||
|
||||
|
||||
@image_comparison(['fancyarrow_dash.png'], remove_text=True, style='default')
|
||||
def test_fancyarrow_dash():
|
||||
fig, ax = plt.subplots()
|
||||
e = mpatches.FancyArrowPatch((0, 0), (0.5, 0.5),
|
||||
arrowstyle='-|>',
|
||||
connectionstyle='angle3,angleA=0,angleB=90',
|
||||
mutation_scale=10.0,
|
||||
linewidth=2,
|
||||
linestyle='dashed',
|
||||
color='k')
|
||||
e2 = mpatches.FancyArrowPatch((0, 0), (0.5, 0.5),
|
||||
arrowstyle='-|>',
|
||||
connectionstyle='angle3',
|
||||
mutation_scale=10.0,
|
||||
linewidth=2,
|
||||
linestyle='dotted',
|
||||
color='k')
|
||||
ax.add_patch(e)
|
||||
ax.add_patch(e2)
|
||||
|
||||
|
||||
@image_comparison(['arrow_styles.png'], style='mpl20', remove_text=True,
|
||||
tol=0 if platform.machine() == 'x86_64' else 0.005)
|
||||
def test_arrow_styles():
|
||||
styles = mpatches.ArrowStyle.get_styles()
|
||||
|
||||
n = len(styles)
|
||||
fig, ax = plt.subplots(figsize=(8, 8))
|
||||
ax.set_xlim(0, 1)
|
||||
ax.set_ylim(-1, n)
|
||||
fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
|
||||
|
||||
for i, stylename in enumerate(sorted(styles)):
|
||||
patch = mpatches.FancyArrowPatch((0.1 + (i % 2)*0.05, i),
|
||||
(0.45 + (i % 2)*0.05, i),
|
||||
arrowstyle=stylename,
|
||||
mutation_scale=25)
|
||||
ax.add_patch(patch)
|
||||
|
||||
for i, stylename in enumerate([']-[', ']-', '-[', '|-|']):
|
||||
style = stylename
|
||||
if stylename[0] != '-':
|
||||
style += ',angleA=ANGLE'
|
||||
if stylename[-1] != '-':
|
||||
style += ',angleB=ANGLE'
|
||||
|
||||
for j, angle in enumerate([-30, 60]):
|
||||
arrowstyle = style.replace('ANGLE', str(angle))
|
||||
patch = mpatches.FancyArrowPatch((0.55, 2*i + j), (0.9, 2*i + j),
|
||||
arrowstyle=arrowstyle,
|
||||
mutation_scale=25)
|
||||
ax.add_patch(patch)
|
||||
|
||||
|
||||
@image_comparison(['connection_styles.png'], style='mpl20', remove_text=True)
|
||||
def test_connection_styles():
|
||||
styles = mpatches.ConnectionStyle.get_styles()
|
||||
|
||||
n = len(styles)
|
||||
fig, ax = plt.subplots(figsize=(6, 10))
|
||||
ax.set_xlim(0, 1)
|
||||
ax.set_ylim(-1, n)
|
||||
|
||||
for i, stylename in enumerate(sorted(styles)):
|
||||
patch = mpatches.FancyArrowPatch((0.1, i), (0.8, i + 0.5),
|
||||
arrowstyle="->",
|
||||
connectionstyle=stylename,
|
||||
mutation_scale=25)
|
||||
ax.add_patch(patch)
|
||||
|
||||
|
||||
def test_invalid_intersection():
|
||||
conn_style_1 = mpatches.ConnectionStyle.Angle3(angleA=20, angleB=200)
|
||||
p1 = mpatches.FancyArrowPatch((.2, .2), (.5, .5),
|
||||
connectionstyle=conn_style_1)
|
||||
with pytest.raises(ValueError):
|
||||
plt.gca().add_patch(p1)
|
||||
|
||||
conn_style_2 = mpatches.ConnectionStyle.Angle3(angleA=20, angleB=199.9)
|
||||
p2 = mpatches.FancyArrowPatch((.2, .2), (.5, .5),
|
||||
connectionstyle=conn_style_2)
|
||||
plt.gca().add_patch(p2)
|
||||
@@ -0,0 +1,564 @@
|
||||
import io
|
||||
from itertools import chain
|
||||
|
||||
import numpy as np
|
||||
|
||||
import pytest
|
||||
|
||||
import matplotlib.colors as mcolors
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib.patches as mpatches
|
||||
import matplotlib.lines as mlines
|
||||
import matplotlib.path as mpath
|
||||
import matplotlib.transforms as mtransforms
|
||||
import matplotlib.collections as mcollections
|
||||
import matplotlib.artist as martist
|
||||
import matplotlib.backend_bases as mbackend_bases
|
||||
import matplotlib as mpl
|
||||
from matplotlib.testing.decorators import check_figures_equal, image_comparison
|
||||
|
||||
|
||||
def test_patch_transform_of_none():
|
||||
# tests the behaviour of patches added to an Axes with various transform
|
||||
# specifications
|
||||
|
||||
ax = plt.axes()
|
||||
ax.set_xlim([1, 3])
|
||||
ax.set_ylim([1, 3])
|
||||
|
||||
# Draw an ellipse over data coord (2, 2) by specifying device coords.
|
||||
xy_data = (2, 2)
|
||||
xy_pix = ax.transData.transform(xy_data)
|
||||
|
||||
# Not providing a transform of None puts the ellipse in data coordinates .
|
||||
e = mpatches.Ellipse(xy_data, width=1, height=1, fc='yellow', alpha=0.5)
|
||||
ax.add_patch(e)
|
||||
assert e._transform == ax.transData
|
||||
|
||||
# Providing a transform of None puts the ellipse in device coordinates.
|
||||
e = mpatches.Ellipse(xy_pix, width=120, height=120, fc='coral',
|
||||
transform=None, alpha=0.5)
|
||||
assert e.is_transform_set()
|
||||
ax.add_patch(e)
|
||||
assert isinstance(e._transform, mtransforms.IdentityTransform)
|
||||
|
||||
# Providing an IdentityTransform puts the ellipse in device coordinates.
|
||||
e = mpatches.Ellipse(xy_pix, width=100, height=100,
|
||||
transform=mtransforms.IdentityTransform(), alpha=0.5)
|
||||
ax.add_patch(e)
|
||||
assert isinstance(e._transform, mtransforms.IdentityTransform)
|
||||
|
||||
# Not providing a transform, and then subsequently "get_transform" should
|
||||
# not mean that "is_transform_set".
|
||||
e = mpatches.Ellipse(xy_pix, width=120, height=120, fc='coral',
|
||||
alpha=0.5)
|
||||
intermediate_transform = e.get_transform()
|
||||
assert not e.is_transform_set()
|
||||
ax.add_patch(e)
|
||||
assert e.get_transform() != intermediate_transform
|
||||
assert e.is_transform_set()
|
||||
assert e._transform == ax.transData
|
||||
|
||||
|
||||
def test_collection_transform_of_none():
|
||||
# tests the behaviour of collections added to an Axes with various
|
||||
# transform specifications
|
||||
|
||||
ax = plt.axes()
|
||||
ax.set_xlim([1, 3])
|
||||
ax.set_ylim([1, 3])
|
||||
|
||||
# draw an ellipse over data coord (2, 2) by specifying device coords
|
||||
xy_data = (2, 2)
|
||||
xy_pix = ax.transData.transform(xy_data)
|
||||
|
||||
# not providing a transform of None puts the ellipse in data coordinates
|
||||
e = mpatches.Ellipse(xy_data, width=1, height=1)
|
||||
c = mcollections.PatchCollection([e], facecolor='yellow', alpha=0.5)
|
||||
ax.add_collection(c)
|
||||
# the collection should be in data coordinates
|
||||
assert c.get_offset_transform() + c.get_transform() == ax.transData
|
||||
|
||||
# providing a transform of None puts the ellipse in device coordinates
|
||||
e = mpatches.Ellipse(xy_pix, width=120, height=120)
|
||||
c = mcollections.PatchCollection([e], facecolor='coral',
|
||||
alpha=0.5)
|
||||
c.set_transform(None)
|
||||
ax.add_collection(c)
|
||||
assert isinstance(c.get_transform(), mtransforms.IdentityTransform)
|
||||
|
||||
# providing an IdentityTransform puts the ellipse in device coordinates
|
||||
e = mpatches.Ellipse(xy_pix, width=100, height=100)
|
||||
c = mcollections.PatchCollection([e],
|
||||
transform=mtransforms.IdentityTransform(),
|
||||
alpha=0.5)
|
||||
ax.add_collection(c)
|
||||
assert isinstance(c.get_offset_transform(), mtransforms.IdentityTransform)
|
||||
|
||||
|
||||
@image_comparison(["clip_path_clipping"], remove_text=True)
|
||||
def test_clipping():
|
||||
exterior = mpath.Path.unit_rectangle().deepcopy()
|
||||
exterior.vertices *= 4
|
||||
exterior.vertices -= 2
|
||||
interior = mpath.Path.unit_circle().deepcopy()
|
||||
interior.vertices = interior.vertices[::-1]
|
||||
clip_path = mpath.Path.make_compound_path(exterior, interior)
|
||||
|
||||
star = mpath.Path.unit_regular_star(6).deepcopy()
|
||||
star.vertices *= 2.6
|
||||
|
||||
fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)
|
||||
|
||||
col = mcollections.PathCollection([star], lw=5, edgecolor='blue',
|
||||
facecolor='red', alpha=0.7, hatch='*')
|
||||
col.set_clip_path(clip_path, ax1.transData)
|
||||
ax1.add_collection(col)
|
||||
|
||||
patch = mpatches.PathPatch(star, lw=5, edgecolor='blue', facecolor='red',
|
||||
alpha=0.7, hatch='*')
|
||||
patch.set_clip_path(clip_path, ax2.transData)
|
||||
ax2.add_patch(patch)
|
||||
|
||||
ax1.set_xlim([-3, 3])
|
||||
ax1.set_ylim([-3, 3])
|
||||
|
||||
|
||||
@check_figures_equal(extensions=['png'])
|
||||
def test_clipping_zoom(fig_test, fig_ref):
|
||||
# This test places the Axes and sets its limits such that the clip path is
|
||||
# outside the figure entirely. This should not break the clip path.
|
||||
ax_test = fig_test.add_axes([0, 0, 1, 1])
|
||||
l, = ax_test.plot([-3, 3], [-3, 3])
|
||||
# Explicit Path instead of a Rectangle uses clip path processing, instead
|
||||
# of a clip box optimization.
|
||||
p = mpath.Path([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]])
|
||||
p = mpatches.PathPatch(p, transform=ax_test.transData)
|
||||
l.set_clip_path(p)
|
||||
|
||||
ax_ref = fig_ref.add_axes([0, 0, 1, 1])
|
||||
ax_ref.plot([-3, 3], [-3, 3])
|
||||
|
||||
ax_ref.set(xlim=(0.5, 0.75), ylim=(0.5, 0.75))
|
||||
ax_test.set(xlim=(0.5, 0.75), ylim=(0.5, 0.75))
|
||||
|
||||
|
||||
def test_cull_markers():
|
||||
x = np.random.random(20000)
|
||||
y = np.random.random(20000)
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
ax.plot(x, y, 'k.')
|
||||
ax.set_xlim(2, 3)
|
||||
|
||||
pdf = io.BytesIO()
|
||||
fig.savefig(pdf, format="pdf")
|
||||
assert len(pdf.getvalue()) < 8000
|
||||
|
||||
svg = io.BytesIO()
|
||||
fig.savefig(svg, format="svg")
|
||||
assert len(svg.getvalue()) < 20000
|
||||
|
||||
|
||||
@image_comparison(['hatching'], remove_text=True, style='default')
|
||||
def test_hatching():
|
||||
fig, ax = plt.subplots(1, 1)
|
||||
|
||||
# Default hatch color.
|
||||
rect1 = mpatches.Rectangle((0, 0), 3, 4, hatch='/')
|
||||
ax.add_patch(rect1)
|
||||
|
||||
rect2 = mcollections.RegularPolyCollection(
|
||||
4, sizes=[16000], offsets=[(1.5, 6.5)], offset_transform=ax.transData,
|
||||
hatch='/')
|
||||
ax.add_collection(rect2)
|
||||
|
||||
# Ensure edge color is not applied to hatching.
|
||||
rect3 = mpatches.Rectangle((4, 0), 3, 4, hatch='/', edgecolor='C1')
|
||||
ax.add_patch(rect3)
|
||||
|
||||
rect4 = mcollections.RegularPolyCollection(
|
||||
4, sizes=[16000], offsets=[(5.5, 6.5)], offset_transform=ax.transData,
|
||||
hatch='/', edgecolor='C1')
|
||||
ax.add_collection(rect4)
|
||||
|
||||
ax.set_xlim(0, 7)
|
||||
ax.set_ylim(0, 9)
|
||||
|
||||
|
||||
def test_remove():
|
||||
fig, ax = plt.subplots()
|
||||
im = ax.imshow(np.arange(36).reshape(6, 6))
|
||||
ln, = ax.plot(range(5))
|
||||
|
||||
assert fig.stale
|
||||
assert ax.stale
|
||||
|
||||
fig.canvas.draw()
|
||||
assert not fig.stale
|
||||
assert not ax.stale
|
||||
assert not ln.stale
|
||||
|
||||
assert im in ax._mouseover_set
|
||||
assert ln not in ax._mouseover_set
|
||||
assert im.axes is ax
|
||||
|
||||
im.remove()
|
||||
ln.remove()
|
||||
|
||||
for art in [im, ln]:
|
||||
assert art.axes is None
|
||||
assert art.figure is None
|
||||
|
||||
assert im not in ax._mouseover_set
|
||||
assert fig.stale
|
||||
assert ax.stale
|
||||
|
||||
|
||||
@image_comparison(["default_edges.png"], remove_text=True, style='default')
|
||||
def test_default_edges():
|
||||
# Remove this line when this test image is regenerated.
|
||||
plt.rcParams['text.kerning_factor'] = 6
|
||||
|
||||
fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots(2, 2)
|
||||
|
||||
ax1.plot(np.arange(10), np.arange(10), 'x',
|
||||
np.arange(10) + 1, np.arange(10), 'o')
|
||||
ax2.bar(np.arange(10), np.arange(10), align='edge')
|
||||
ax3.text(0, 0, "BOX", size=24, bbox=dict(boxstyle='sawtooth'))
|
||||
ax3.set_xlim((-1, 1))
|
||||
ax3.set_ylim((-1, 1))
|
||||
pp1 = mpatches.PathPatch(
|
||||
mpath.Path([(0, 0), (1, 0), (1, 1), (0, 0)],
|
||||
[mpath.Path.MOVETO, mpath.Path.CURVE3,
|
||||
mpath.Path.CURVE3, mpath.Path.CLOSEPOLY]),
|
||||
fc="none", transform=ax4.transData)
|
||||
ax4.add_patch(pp1)
|
||||
|
||||
|
||||
def test_properties():
|
||||
ln = mlines.Line2D([], [])
|
||||
ln.properties() # Check that no warning is emitted.
|
||||
|
||||
|
||||
def test_setp():
|
||||
# Check empty list
|
||||
plt.setp([])
|
||||
plt.setp([[]])
|
||||
|
||||
# Check arbitrary iterables
|
||||
fig, ax = plt.subplots()
|
||||
lines1 = ax.plot(range(3))
|
||||
lines2 = ax.plot(range(3))
|
||||
martist.setp(chain(lines1, lines2), 'lw', 5)
|
||||
plt.setp(ax.spines.values(), color='green')
|
||||
|
||||
# Check *file* argument
|
||||
sio = io.StringIO()
|
||||
plt.setp(lines1, 'zorder', file=sio)
|
||||
assert sio.getvalue() == ' zorder: float\n'
|
||||
|
||||
|
||||
def test_None_zorder():
|
||||
fig, ax = plt.subplots()
|
||||
ln, = ax.plot(range(5), zorder=None)
|
||||
assert ln.get_zorder() == mlines.Line2D.zorder
|
||||
ln.set_zorder(123456)
|
||||
assert ln.get_zorder() == 123456
|
||||
ln.set_zorder(None)
|
||||
assert ln.get_zorder() == mlines.Line2D.zorder
|
||||
|
||||
|
||||
@pytest.mark.parametrize('accept_clause, expected', [
|
||||
('', 'unknown'),
|
||||
("ACCEPTS: [ '-' | '--' | '-.' ]", "[ '-' | '--' | '-.' ]"),
|
||||
('ACCEPTS: Some description.', 'Some description.'),
|
||||
('.. ACCEPTS: Some description.', 'Some description.'),
|
||||
('arg : int', 'int'),
|
||||
('*arg : int', 'int'),
|
||||
('arg : int\nACCEPTS: Something else.', 'Something else. '),
|
||||
])
|
||||
def test_artist_inspector_get_valid_values(accept_clause, expected):
|
||||
class TestArtist(martist.Artist):
|
||||
def set_f(self, arg):
|
||||
pass
|
||||
|
||||
TestArtist.set_f.__doc__ = """
|
||||
Some text.
|
||||
|
||||
%s
|
||||
""" % accept_clause
|
||||
valid_values = martist.ArtistInspector(TestArtist).get_valid_values('f')
|
||||
assert valid_values == expected
|
||||
|
||||
|
||||
def test_artist_inspector_get_aliases():
|
||||
# test the correct format and type of get_aliases method
|
||||
ai = martist.ArtistInspector(mlines.Line2D)
|
||||
aliases = ai.get_aliases()
|
||||
assert aliases["linewidth"] == {"lw"}
|
||||
|
||||
|
||||
def test_set_alpha():
|
||||
art = martist.Artist()
|
||||
with pytest.raises(TypeError, match='^alpha must be numeric or None'):
|
||||
art.set_alpha('string')
|
||||
with pytest.raises(TypeError, match='^alpha must be numeric or None'):
|
||||
art.set_alpha([1, 2, 3])
|
||||
with pytest.raises(ValueError, match="outside 0-1 range"):
|
||||
art.set_alpha(1.1)
|
||||
with pytest.raises(ValueError, match="outside 0-1 range"):
|
||||
art.set_alpha(np.nan)
|
||||
|
||||
|
||||
def test_set_alpha_for_array():
|
||||
art = martist.Artist()
|
||||
with pytest.raises(TypeError, match='^alpha must be numeric or None'):
|
||||
art._set_alpha_for_array('string')
|
||||
with pytest.raises(ValueError, match="outside 0-1 range"):
|
||||
art._set_alpha_for_array(1.1)
|
||||
with pytest.raises(ValueError, match="outside 0-1 range"):
|
||||
art._set_alpha_for_array(np.nan)
|
||||
with pytest.raises(ValueError, match="alpha must be between 0 and 1"):
|
||||
art._set_alpha_for_array([0.5, 1.1])
|
||||
with pytest.raises(ValueError, match="alpha must be between 0 and 1"):
|
||||
art._set_alpha_for_array([0.5, np.nan])
|
||||
|
||||
|
||||
def test_callbacks():
|
||||
def func(artist):
|
||||
func.counter += 1
|
||||
|
||||
func.counter = 0
|
||||
|
||||
art = martist.Artist()
|
||||
oid = art.add_callback(func)
|
||||
assert func.counter == 0
|
||||
art.pchanged() # must call the callback
|
||||
assert func.counter == 1
|
||||
art.set_zorder(10) # setting a property must also call the callback
|
||||
assert func.counter == 2
|
||||
art.remove_callback(oid)
|
||||
art.pchanged() # must not call the callback anymore
|
||||
assert func.counter == 2
|
||||
|
||||
|
||||
def test_set_signature():
|
||||
"""Test autogenerated ``set()`` for Artist subclasses."""
|
||||
class MyArtist1(martist.Artist):
|
||||
def set_myparam1(self, val):
|
||||
pass
|
||||
|
||||
assert hasattr(MyArtist1.set, '_autogenerated_signature')
|
||||
assert 'myparam1' in MyArtist1.set.__doc__
|
||||
|
||||
class MyArtist2(MyArtist1):
|
||||
def set_myparam2(self, val):
|
||||
pass
|
||||
|
||||
assert hasattr(MyArtist2.set, '_autogenerated_signature')
|
||||
assert 'myparam1' in MyArtist2.set.__doc__
|
||||
assert 'myparam2' in MyArtist2.set.__doc__
|
||||
|
||||
|
||||
def test_set_is_overwritten():
|
||||
"""set() defined in Artist subclasses should not be overwritten."""
|
||||
class MyArtist3(martist.Artist):
|
||||
|
||||
def set(self, **kwargs):
|
||||
"""Not overwritten."""
|
||||
|
||||
assert not hasattr(MyArtist3.set, '_autogenerated_signature')
|
||||
assert MyArtist3.set.__doc__ == "Not overwritten."
|
||||
|
||||
class MyArtist4(MyArtist3):
|
||||
pass
|
||||
|
||||
assert MyArtist4.set is MyArtist3.set
|
||||
|
||||
|
||||
def test_format_cursor_data_BoundaryNorm():
|
||||
"""Test if cursor data is correct when using BoundaryNorm."""
|
||||
X = np.empty((3, 3))
|
||||
X[0, 0] = 0.9
|
||||
X[0, 1] = 0.99
|
||||
X[0, 2] = 0.999
|
||||
X[1, 0] = -1
|
||||
X[1, 1] = 0
|
||||
X[1, 2] = 1
|
||||
X[2, 0] = 0.09
|
||||
X[2, 1] = 0.009
|
||||
X[2, 2] = 0.0009
|
||||
|
||||
# map range -1..1 to 0..256 in 0.1 steps
|
||||
fig, ax = plt.subplots()
|
||||
fig.suptitle("-1..1 to 0..256 in 0.1")
|
||||
norm = mcolors.BoundaryNorm(np.linspace(-1, 1, 20), 256)
|
||||
img = ax.imshow(X, cmap='RdBu_r', norm=norm)
|
||||
|
||||
labels_list = [
|
||||
"[0.9]",
|
||||
"[1.]",
|
||||
"[1.]",
|
||||
"[-1.0]",
|
||||
"[0.0]",
|
||||
"[1.0]",
|
||||
"[0.09]",
|
||||
"[0.009]",
|
||||
"[0.0009]",
|
||||
]
|
||||
for v, label in zip(X.flat, labels_list):
|
||||
# label = "[{:-#.{}g}]".format(v, cbook._g_sig_digits(v, 0.1))
|
||||
assert img.format_cursor_data(v) == label
|
||||
|
||||
plt.close()
|
||||
|
||||
# map range -1..1 to 0..256 in 0.01 steps
|
||||
fig, ax = plt.subplots()
|
||||
fig.suptitle("-1..1 to 0..256 in 0.01")
|
||||
cmap = mpl.colormaps['RdBu_r'].resampled(200)
|
||||
norm = mcolors.BoundaryNorm(np.linspace(-1, 1, 200), 200)
|
||||
img = ax.imshow(X, cmap=cmap, norm=norm)
|
||||
|
||||
labels_list = [
|
||||
"[0.90]",
|
||||
"[0.99]",
|
||||
"[1.0]",
|
||||
"[-1.00]",
|
||||
"[0.00]",
|
||||
"[1.00]",
|
||||
"[0.09]",
|
||||
"[0.009]",
|
||||
"[0.0009]",
|
||||
]
|
||||
for v, label in zip(X.flat, labels_list):
|
||||
# label = "[{:-#.{}g}]".format(v, cbook._g_sig_digits(v, 0.01))
|
||||
assert img.format_cursor_data(v) == label
|
||||
|
||||
plt.close()
|
||||
|
||||
# map range -1..1 to 0..256 in 0.01 steps
|
||||
fig, ax = plt.subplots()
|
||||
fig.suptitle("-1..1 to 0..256 in 0.001")
|
||||
cmap = mpl.colormaps['RdBu_r'].resampled(2000)
|
||||
norm = mcolors.BoundaryNorm(np.linspace(-1, 1, 2000), 2000)
|
||||
img = ax.imshow(X, cmap=cmap, norm=norm)
|
||||
|
||||
labels_list = [
|
||||
"[0.900]",
|
||||
"[0.990]",
|
||||
"[0.999]",
|
||||
"[-1.000]",
|
||||
"[0.000]",
|
||||
"[1.000]",
|
||||
"[0.090]",
|
||||
"[0.009]",
|
||||
"[0.0009]",
|
||||
]
|
||||
for v, label in zip(X.flat, labels_list):
|
||||
# label = "[{:-#.{}g}]".format(v, cbook._g_sig_digits(v, 0.001))
|
||||
assert img.format_cursor_data(v) == label
|
||||
|
||||
plt.close()
|
||||
|
||||
# different testing data set with
|
||||
# out of bounds values for 0..1 range
|
||||
X = np.empty((7, 1))
|
||||
X[0] = -1.0
|
||||
X[1] = 0.0
|
||||
X[2] = 0.1
|
||||
X[3] = 0.5
|
||||
X[4] = 0.9
|
||||
X[5] = 1.0
|
||||
X[6] = 2.0
|
||||
|
||||
labels_list = [
|
||||
"[-1.0]",
|
||||
"[0.0]",
|
||||
"[0.1]",
|
||||
"[0.5]",
|
||||
"[0.9]",
|
||||
"[1.0]",
|
||||
"[2.0]",
|
||||
]
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
fig.suptitle("noclip, neither")
|
||||
norm = mcolors.BoundaryNorm(
|
||||
np.linspace(0, 1, 4, endpoint=True), 256, clip=False, extend='neither')
|
||||
img = ax.imshow(X, cmap='RdBu_r', norm=norm)
|
||||
for v, label in zip(X.flat, labels_list):
|
||||
# label = "[{:-#.{}g}]".format(v, cbook._g_sig_digits(v, 0.33))
|
||||
assert img.format_cursor_data(v) == label
|
||||
|
||||
plt.close()
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
fig.suptitle("noclip, min")
|
||||
norm = mcolors.BoundaryNorm(
|
||||
np.linspace(0, 1, 4, endpoint=True), 256, clip=False, extend='min')
|
||||
img = ax.imshow(X, cmap='RdBu_r', norm=norm)
|
||||
for v, label in zip(X.flat, labels_list):
|
||||
# label = "[{:-#.{}g}]".format(v, cbook._g_sig_digits(v, 0.33))
|
||||
assert img.format_cursor_data(v) == label
|
||||
|
||||
plt.close()
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
fig.suptitle("noclip, max")
|
||||
norm = mcolors.BoundaryNorm(
|
||||
np.linspace(0, 1, 4, endpoint=True), 256, clip=False, extend='max')
|
||||
img = ax.imshow(X, cmap='RdBu_r', norm=norm)
|
||||
for v, label in zip(X.flat, labels_list):
|
||||
# label = "[{:-#.{}g}]".format(v, cbook._g_sig_digits(v, 0.33))
|
||||
assert img.format_cursor_data(v) == label
|
||||
|
||||
plt.close()
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
fig.suptitle("noclip, both")
|
||||
norm = mcolors.BoundaryNorm(
|
||||
np.linspace(0, 1, 4, endpoint=True), 256, clip=False, extend='both')
|
||||
img = ax.imshow(X, cmap='RdBu_r', norm=norm)
|
||||
for v, label in zip(X.flat, labels_list):
|
||||
# label = "[{:-#.{}g}]".format(v, cbook._g_sig_digits(v, 0.33))
|
||||
assert img.format_cursor_data(v) == label
|
||||
|
||||
plt.close()
|
||||
|
||||
fig, ax = plt.subplots()
|
||||
fig.suptitle("clip, neither")
|
||||
norm = mcolors.BoundaryNorm(
|
||||
np.linspace(0, 1, 4, endpoint=True), 256, clip=True, extend='neither')
|
||||
img = ax.imshow(X, cmap='RdBu_r', norm=norm)
|
||||
for v, label in zip(X.flat, labels_list):
|
||||
# label = "[{:-#.{}g}]".format(v, cbook._g_sig_digits(v, 0.33))
|
||||
assert img.format_cursor_data(v) == label
|
||||
|
||||
plt.close()
|
||||
|
||||
|
||||
def test_auto_no_rasterize():
|
||||
class Gen1(martist.Artist):
|
||||
...
|
||||
|
||||
assert 'draw' in Gen1.__dict__
|
||||
assert Gen1.__dict__['draw'] is Gen1.draw
|
||||
|
||||
class Gen2(Gen1):
|
||||
...
|
||||
|
||||
assert 'draw' not in Gen2.__dict__
|
||||
assert Gen2.draw is Gen1.draw
|
||||
|
||||
|
||||
def test_draw_wraper_forward_input():
|
||||
class TestKlass(martist.Artist):
|
||||
def draw(self, renderer, extra):
|
||||
return extra
|
||||
|
||||
art = TestKlass()
|
||||
renderer = mbackend_bases.RendererBase()
|
||||
|
||||
assert 'aardvark' == art.draw(renderer, 'aardvark')
|
||||
assert 'aardvark' == art.draw(renderer, extra='aardvark')
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,10 @@
|
||||
import numpy as np
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.axis import XTick
|
||||
|
||||
|
||||
def test_tick_labelcolor_array():
|
||||
# Smoke test that we can instantiate a Tick with labelcolor as array.
|
||||
ax = plt.axes()
|
||||
XTick(ax, 0, labelcolor=np.array([1, 0, 0, 1]))
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user