Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_facecolors() reports incorrect colors #7312

Closed
nschloe opened this issue Oct 20, 2016 · 11 comments
Closed

get_facecolors() reports incorrect colors #7312

nschloe opened this issue Oct 20, 2016 · 11 comments

Comments

@nschloe
Copy link
Contributor

nschloe commented Oct 20, 2016

MWE:

import matplotlib
from matplotlib import pyplot as plt
import numpy as np

fig = plt.figure()

N = 20
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2
plt.scatter(x, y, s=area, c=colors, alpha=0.5)

children = fig.get_children()
for child in children:
    if isinstance(child, matplotlib.axes.Subplot):
        cc = child.get_children()
        print(cc[0].get_facecolors())

plt.show()

Output:

[[ 0.   0.   1.   0.5]]
@WeatherGod
Copy link
Member

This is a similar problem to one that has been found in mplot3d. The issue
is that for ScalarMappables like scatter plots, the colors aren't set until
plotting, so they start off with the default polygon/patch color.

On Thu, Oct 20, 2016 at 9:00 AM, Nico Schlömer notifications@github.com
wrote:

MWE:

import matplotlibfrom matplotlib import pyplot as pltimport numpy as np

fig = plt.figure()
N = 20
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2
plt.scatter(x, y, s=area, c=colors, alpha=0.5)

children = fig.get_children()for child in children:
if isinstance(child, matplotlib.axes.Subplot):
cc = child.get_children()
print(cc[0].get_facecolors())

plt.show()

Output:

[[ 0. 0. 1. 0.5]]


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#7312, or mute the thread
https://github.com/notifications/unsubscribe-auth/AARy-LmuMzjJptr0ib5rVUEuKM_LJNJkks5q12YLgaJpZM4KcGTC
.

@nschloe
Copy link
Contributor Author

nschloe commented Oct 20, 2016

Mhm. Any other ways of retrieving the colors from the figure then?

@tacaswell
Copy link
Member

Why are you trying to do this?

To expand on what @WeatherGod said, the reason that the colors are not set until late is to avoid extra computation as you can change the underlying data, norm and colormap of the ScalarMappable.

@nschloe
Copy link
Contributor Author

nschloe commented Oct 20, 2016

Why are you trying to do this?

This is for matplotlib2tikz, in particular nschloe/tikzplotlib#120.

Perhaps there's an mpl function then that returns the color data from the figure?

@anntzer
Copy link
Contributor

anntzer commented Oct 21, 2016

Actually this issue is solved as of 2.0b4 (I did check that I could reproduce it with 1.5.3).

@anntzer anntzer closed this as completed Oct 21, 2016
@nschloe
Copy link
Contributor Author

nschloe commented Oct 22, 2016

@anntzer Tried this with 2.0b4 and got

[[ 0.12156863  0.46666667  0.70588235  0.5       ]]

This is still one entry for 20 circles. Do you get the same? If yes, I guess a reopen is in order.

@anntzer
Copy link
Contributor

anntzer commented Oct 22, 2016

Oh, I see. I saw that the color was not 0, 0, 1, .5 anymore and thought that was enough, sorry about the premature closing.

@anntzer anntzer reopened this Oct 22, 2016
@anntzer
Copy link
Contributor

anntzer commented Oct 23, 2016

You can (should?) call fig.canvas.draw() first. Then the facecolors become correctly set.

@nschloe
Copy link
Contributor Author

nschloe commented Oct 23, 2016

Indeed, when inserting fig.canvas.draw() I get

[[ 0.9553    0.901065  0.118128  0.5     ]
 [ 0.120092  0.600104  0.54253   0.5     ]
 # ...
 [ 0.945636  0.899815  0.112838  0.5     ]]

which are the actual sizes after colormap matching. Better would be to get the values of the c argument of the scatter call, much like get_sizes() returns the s argument. Any way to do that?

@anntzer
Copy link
Contributor

anntzer commented Oct 23, 2016

Ah. For that, you have cc[0].get_array().

@nschloe
Copy link
Contributor Author

nschloe commented Oct 23, 2016

Just what I was looking for. Thanks!

@nschloe nschloe closed this as completed Oct 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants