Swarmplot "градиент" - PullRequest
       18

Swarmplot "градиент"

0 голосов
/ 01 апреля 2020

Я хочу построить Swarmplot, как этот

enter image description here

Я могу выделить указанную c точку. Но я хочу, чтобы все точки получили вид карты цветов. И указанная точка c имеет соответственно цвет. Я пробую оттенок и палитру, но это не работает.

palette = sns.light_palette("purple", reverse=False,  n_colors=df[typeId].max())
ax = sns.swarmplot(x = df[typeId], ax = ax, hue = df[typeId], alpha = 0.8, size = 8)
axData = ax.get_children()
for a in axData:
    if type(a) is matplotlib.collections.PathCollection:
        offsets = a.get_offsets()
        break
ax.scatter(offsets[index,0], offsets[index,1], marker='o', color='red', zorder=10, s=200, edgecolor = "white")
ax.text(offsets[index,0], offsets[index,1]-0.1,"Value: " + str(player[typeId]) + "\nPercentile rank: " + str("{0:.2f}".format(player[typeRank]*100)), ha = "center", color = "white", zorder = 9, fontproperties=prop_bold,fontsize=10)

enter image description here

1 Ответ

0 голосов
/ 02 апреля 2020

Похоже, что при отсутствии y=, swarmplot() не учитывает параметр hue=. (И когда y является константой, код кажется зависшим.)

Обходной путь - установить цвет вручную:

import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
import numpy as np

type_ids = np.random.binomial(200, 0.7, 500)
ax = plt.gca()

plt.style.use("dark_background")
ax = sns.swarmplot(x=type_ids, ax=ax, size=8)
for a in ax.get_children():
    if type(a) is matplotlib.collections.PathCollection:
        offsets = a.get_offsets()
        cmap = sns.light_palette("purple", reverse=False, as_cmap=True)
        norm = plt.Normalize(vmin=offsets[:,0].min(), vmax=offsets[:,0].max())
        facecolors = [cmap(norm(x)) for x, y in offsets]
        a.set_color(facecolors)
        break
index = 20
ax.scatter(offsets[index, 0], offsets[index, 1], marker='o', color='red', zorder=10, s=200, edgecolor="white")
ax.text(offsets[index, 0], offsets[index, 1] - 0.1, "\nPercentile rank:  ...",
        ha="center", color="white", zorder=9, fontsize=10)
plt.show()

resulting plot

...