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!