8000 Merge branch 'v2.x' · matplotlib/matplotlib@4b3350d · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b3350d

Browse files
committed
Merge branch 'v2.x'
2 parents f3895f9 + 1918af6 commit 4b3350d

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3959,15 +3959,15 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
39593959

39603960
scales = s # Renamed for readability below.
39613961

3962-
# load default marker from rcParams
3963-
if marker is None:
3964-
marker = rcParams['scatter.marker']
3965-
39663962
# to be API compatible
39673963
if marker is None and not (verts is None):
39683964
marker = (verts, 0)
39693965
verts = None
39703966

3967+
# load default marker from rcParams
3968+
if marker is None:
3969+
marker = rcParams['scatter.marker']
3970+
39713971
if isinstance(marker, mmarkers.MarkerStyle):
39723972
marker_obj = marker
39733973
else:

lib/matplotlib/backends/backend_cairo.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
6868
BYTE_FORMAT = 1 # ARGB
6969

7070

71+
class ArrayWrapper:
72+
"""Thin wrapper around numpy ndarray to expose the interface
73+
expected by cairocffi. Basically replicates the
74+
array.array interface.
75+
"""
76+
def __init__(self, myarray):
77+
self.__array = myarray
78+
self.__data = myarray.ctypes.data
79+
self.__size = len(myarray.flatten())
80+
self.itemsize = myarray.itemsize
81+
82+
def buffer_info(self):
83+
return (self.__data, self.__size)
84+
85+
7186
class RendererCairo(RendererBase):
7287
fontweights = {
7388
100 : cairo.FONT_WEIGHT_NORMAL,
@@ -218,9 +233,25 @@ def draw_image(self, gc, x, y, im):
218233
im = im[:, :, (2, 1, 0, 3)]
219234
else:
220235
im = im[:, :, (3, 0, 1, 2)]
221-
surface = cairo.ImageSurface.create_for_data(
222-
memoryview(im.flatten()), cairo.FORMAT_ARGB32, im.shape[1], im.shape[0],
223-
im.shape[1]*4)
236+
if HAS_CAIRO_CFFI:
237+
# cairocffi tries to use the buffer_info from array.array
238+
# that we replicate in ArrayWrapper and alternatively falls back
239+
# on ctypes to get a pointer to the numpy array. This works
240+
# correctly on a numpy array in python3 but not 2.7. We replicate
241+
# the array.array functionality here to get cross version support.
242+
imbuffer = ArrayWrapper(im.flatten())
243+
else:
244+
# py2cairo uses PyObject_AsWriteBuffer
245+
# to get a pointer to the numpy array this works correctly
246+
# on a regular numpy array but not on a memory view.
247+
# At the time of writing the latest release version of
248+
# py3cairo still does not support create_for_data
249+
imbuffer = im.flatten()
250+
surface = cairo.ImageSurface.create_for_data(imbuffer,
251+
cairo.FORMAT_ARGB32,
252+
im.shape[1],
253+
im.shape[0],
254+
im.shape[1]*4)
224255
ctx = gc.ctx
225256
y = self.height - y - im.shape[0]
226257

Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ def test_scatter_plot():
12831283
@image_comparison(baseline_images=['scatter_marker'], remove_text=True,
12841284
extensions=['png'])
12851285
def test_scatter_marker():
1286-
fig, (ax0, ax1) = plt.subplots(ncols=2)
1286+
fig, (ax0, ax1, ax2) = plt.subplots(ncols=3)
12871287
ax0.scatter([3, 4, 2, 6], [2, 5, 2, 3],
12881288
c=[(1, 0, 0), 'y', 'b', 'lime'],
12891289
s=[60, 50, 40, 30],
@@ -1294,6 +1294,16 @@ def test_scatter_marker():
12941294
s=[60, 50, 40, 30],
12951295
edgecolors=['k', 'r', 'g', 'b'],
12961296
marker=mmarkers.MarkerStyle('o', fillstyle='top'))
1297+
# unit area ellipse
1298+
rx, ry = 3, 1
1299+
area = rx * ry * np.pi
1300+
theta = np.linspace(0, 2 * np.pi, 21)
1301+
verts = list(zip(np.cos(theta) * rx / area, np.sin(theta) * ry / area))
1302+
ax2.scatter([3, 4, 2, 6], [2, 5, 2, 3],
1303+
c=[(1, 0, 0), 'y', 'b', 'lime'],
1304+
s=[60, 50, 40, 30],
1305+
edgecolors=['k', 'r', 'g', 'b'],
1306+
verts=verts)
12971307

12981308

12991309
@image_comparison(baseline_images=['scatter_2D'], remove_text=True,

0 commit comments

Comments
 (0)
0