I'm plotting something with seaborn's regplot
. As far as I understand, it uses pyplot.scatter
behind the scenes. So I assumed that if I specify colour for scatterplot as a sequence, I would then be able to just call plt.colorbar
, but it doesn't seem to work:
sns.regplot('mapped both', 'unique; repeated at least once', wt, ci=95, logx=True, truncate=True, line_kws={"linewidth": 1, "color": "seagreen"}, scatter_kws={'c':wt['Cis/Trans'], 'cmap':'summer', 's':75})
plt.colorbar()
Traceback (most recent call last):
File "<ipython-input-174-f2d61aff7c73>", line 2, in <module>
plt.colorbar()
File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 2152, in colorbar
raise RuntimeError('No mappable was found to use for colorbar '
RuntimeError: No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a contour set (with contourf).
Why doesn't it work, and is there a way around it?
I would be fine with using size of the dots instead of colour if there was an easy way to generate a legend for the sizes
Another approach would be
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
points = plt.scatter(tips["total_bill"], tips["tip"],
c=tips["size"], s=75, cmap="BuGn")
plt.colorbar(points)
sns.regplot("total_bill", "tip", data=tips, scatter=False, color=".1")
The color argument to regplot applies a single color to regplot elements (this is in the seaborn documentation). To control the scatterplot, you need to pass kwargs through:
import pandas as pd
import seaborn as sns
import numpy.random as nr
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
data = nr.random((9,3))
df = pd.DataFrame(data, columns=list('abc'))
out = sns.regplot('a','b',df, scatter=True,
ax=ax,
scatter_kws={'c':df['c'], 'cmap':'jet'})
Then you get the mappable thing (the collection made by scatter) out of the AxesSubplot seaborn returns, and specify that you want a colorbar for the mappable. Note my TODO comment, if you plan to run this with other changes to the plot.
outpathc = out.get_children()[3]
#TODO -- don't assume PathCollection is 4th; at least check type
plt.colorbar(mappable=outpathc)
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With