python - Swarmplot“梯度”
问题描述
我想绘制这样的 swarmplot
我可以强调具体点。但我希望所有的点都得到一种颜色图。并且具体点有各自的颜色。我正在尝试色调和调色板,但它不起作用。
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)
解决方案
似乎当没有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()
推荐阅读
- python-3.x - 为什么我会得到
而不是数字/字符串 - javascript - 如何使用计数器打破循环
- python - 错误:在 _update_shadow self._shadow = App.get_running_app().theme_cls.quad_shadow
- json - 如何获取嵌套的 JSON 键值然后分配给变量?
- unity3d - Unity3d:找到哪个游戏对象在前面
- swift - 如何将本地网络名称从(例如 en0)更改为实际网络名称
- r - 在 r 中的数据帧的每一列中识别具有最大值的行
- powershell-5.0 - gwmi win32_service -ComputerName IP –credential userName 返回访问被拒绝但 Enter-PSSession -ComputerName IP -Credential Personal 有效
- javascript - 如何为使用 Flowplayer javascript API 嵌入的视频创建 onclick 事件?
- salesforce - 如何处理动作状态“未完成”