1

When I am plotting a grouped scatter plot from pandas (as described in the documentation) where the second group needs to contain a color bar, I get an error TypeError: You must first set_array for mappable.

Following other but different questions for ungrouped scatter plots, this is because cmap is only used if c is an array of floats. But stand-alone it works perfectly and the data is not manipulated between creating the two axes-objects.

Here is the code that I am using:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.rand(100, 5), columns=['A', 'B', 'C', 'D', 'E'])

# this works stand-alone
#df.plot(kind='scatter', x='A', y='B', c='C', cmap='Blues')

# why does this break?
ax = df.plot(kind='scatter', x='D', y='E', color='red', label='Other group')
df.plot(kind='scatter', x='A', y='B', c='C', cmap='Blues', ax=ax)
plt.show()

Both groups should be displayed in one plot. Note, that it is important for me to plot columns D and E before plotting A, B and C on top of them so the latter need to be in the second plot. Vice versa it works but for my requirements it breaks.

Does anyone know how to fix this and obtain the desired result?

Thanks in advance!

3
  • In your standalone, you are not using the same ax for both the figure. Both figures are plotted separately. In your problematic case, you are trying to plot both in the same figure using ax. What is the desired output? Two separate plots or a single plot with both Commented Jan 8, 2019 at 15:25
  • A single plot as in the answer below! Commented Jan 8, 2019 at 15:51
  • I will specify my question! Commented Jan 8, 2019 at 15:52

2 Answers 2

3

It seems pandas confuses itself about making a colorbar internally. You always have the options to create the colorbar with matplotlib though.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.rand(100, 5), columns=['A', 'B', 'C', 'D', 'E'])

ax = df.plot(kind='scatter', x='D', y='E', color='red', label='Other group')
df.plot(kind='scatter', x='A', y='B', c='C', cmap='Blues', ax=ax, colorbar=False)
ax.figure.colorbar(ax.collections[1])   # Note the index 1, which stands
                                        # for second scatter in the axes.
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! I have accepted the other answer as it was the first one and it still contains the colorbar label without additional code..
2

Reverse the order of you plotting. I think the colorbar is getting confused which chart to apply to. Hence, we try to do plot the first with color bar then apply the red scatter on top.

df = pd.DataFrame(np.random.rand(100, 5), columns=['A', 'B', 'C', 'D', 'E'])

# this works stand-alone
#df.plot(kind='scatter', x='A', y='B', c='C', cmap='Blues')

# why does this break?
# ax = df.plot(kind='scatter', x='D', y='E', color='red', abel='Other group')
ax = df.plot(kind='scatter', x='A', y='B', c='C', cmap='Blues', zorder=10)
df.plot(kind='scatter', x='D', y='E', color='red', label='Other group', ax=ax, zorder=1)
plt.show()

Output:

enter image description here

With zorder:

enter image description here

3 Comments

Thanks for your help. As I have mentioned, I need to plot D and E before columns A, B and C. Or result-wise the markers for A, B and C have to "cover" the markers for D and E. Is it somehow possible to get this result?
Try the zorder parameter. See updated solution. see matplotdocs and example
Perfect! zorder was the keyword and you saved my day ;-) Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.