Hexbin plot, PolyCollection and inconsistent facecolor values

Hi,

I am trying to change the edgecolor of some hexbin patches in a hexbin
plot. Fiddling around with it to understand how this could work, I
encountered a behaviour I cannot understand. I initially opened an issue at
seaborn here https://github.com/mwaskom/seaborn/issues/1734 . I get the
same behaviour with pure matplotlib and am now asking here for help.

Calling get_facecolors() on the PolyCollection returned by plt.hexbin gives
different results depending on the jupyter notebook cell within which the
call is made.

Here's my example (assuming the code is run in a notebook, using Python
3.6.8, NumPy 1.16.2, Matplotlib 3.0.3, Jupyter 4.4.0)

Cell 1:

···

-------
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
-------

Cell 2:
-------
x = np.array([1,1,1,1.5,1.5,2])
y = np.array([1,1,1,1,1,1])
-------

Cell 3:
-------
polycollection = plt.hexbin(x, y, C=np.ones(x.shape),
reduce_C_function=np.sum, extent=[1, 2, 0.5, 1.5], gridsize=3,
linewidths=1, edgecolors='face')
print(polycollection.get_facecolors())
-------
This plots three hexbins with different counts and different colours. Here,
get_facecolors() returns a (1,4) array, namely,
[[0.12156863 0.46666667 0.70588235 1. ]]

Cell 4:
-------
print(polycollection.get_facecolors())
-------
When calling get_facecolors() again in the next cell, a (3,4) array is
returned, namely,
[[0.993248 0.906157 0.143936 1. ]
[0.127568 0.566949 0.550556 1. ]
[0.267004 0.004874 0.329415 1. ]]

The second array seems to be the correct one I think (there are different
colors for each of the three hexbins). What is happening here? I am very
confused!

Thanks,
Niklas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190430/ed8df340/attachment.html>

Just a quick update in case somebody else has a similar problem.

I needed to call plt.draw() within the Jupyter Notebook cell to populate
the polycollection, that is, directly after the plt.hexhin() call and
before the print() statement.

Cheers,
Niklas

···

On Tue, 30 Apr 2019 at 10:02, Niklas Berliner <niklas.berliner at gmail.com> wrote:

Hi,

I am trying to change the edgecolor of some hexbin patches in a hexbin
plot. Fiddling around with it to understand how this could work, I
encountered a behaviour I cannot understand. I initially opened an issue at
seaborn here hexbin plot, PolyCollection and inconsistent facecolor values · Issue #1734 · mwaskom/seaborn · GitHub . I get the
same behaviour with pure matplotlib and am now asking here for help.

Calling get_facecolors() on the PolyCollection returned by plt.hexbin
gives different results depending on the jupyter notebook cell within which
the call is made.

Here's my example (assuming the code is run in a notebook, using Python
3.6.8, NumPy 1.16.2, Matplotlib 3.0.3, Jupyter 4.4.0)

Cell 1:
-------
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
-------

Cell 2:
-------
x = np.array([1,1,1,1.5,1.5,2])
y = np.array([1,1,1,1,1,1])
-------

Cell 3:
-------
polycollection = plt.hexbin(x, y, C=np.ones(x.shape),
reduce_C_function=np.sum, extent=[1, 2, 0.5, 1.5], gridsize=3,
linewidths=1, edgecolors='face')
print(polycollection.get_facecolors())
-------
This plots three hexbins with different counts and different colours.
Here, get_facecolors() returns a (1,4) array, namely,
[[0.12156863 0.46666667 0.70588235 1. ]]

Cell 4:
-------
print(polycollection.get_facecolors())
-------
When calling get_facecolors() again in the next cell, a (3,4) array is
returned, namely,
[[0.993248 0.906157 0.143936 1. ]
[0.127568 0.566949 0.550556 1. ]
[0.267004 0.004874 0.329415 1. ]]

The second array seems to be the correct one I think (there are different
colors for each of the three hexbins). What is happening here? I am very
confused!

Thanks,
Niklas

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190509/26b2c1d6/attachment.html&gt;