Marine regplot with color bar?

I draw something with a marine regplot. As far as I understand, he uses pyplot.scatterbehind the scenes. Therefore, I suggested that if I specify the color for the scatter chart as a sequence, I could just call plt.colorbar, but it does not 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?


It would be nice for me to use dot size instead of color if there was an easy way to generate legend for dimensions

+4
source share
2 answers

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")

enter image description here

+8
source

The color parameter for regplot applies a single color to the regplot elements (this is in the documentation for the sea). To control the scatterplot, you need to go through 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'})

(, ) AxesSubplot , . TODO, .

outpathc = out.get_children()[3] 
#TODO -- don't assume PathCollection is 4th; at least check type

plt.colorbar(mappable=outpathc)

plt.show()

enter image description here

+6

Source: https://habr.com/ru/post/1589034/


All Articles