带有色相颜色图和图例的Python 3D散点图

cud*_*c80 5 python matplotlib seaborn

我一直在用seaborn搜索python中的3D图,但还没有看到。我想3D绘制最初使用seaborn pairplot绘制的数据集。谁能帮我解决以下两个问题:

  1. 我无法获得与sns pairplot相同的调色板,例如,如何从图2获取调色板并将其应用于图1上的点?
  2. 图例不符合情节或在pairplot上显示不佳,例如,当plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.,ncol=4)我看到以下错误时:anaconda2 / lib / python2.7 / site-packages / matplotlib / axes / _axes.py:545: UserWarning:未找到带标签的对象。在各个图上使用label ='...'kwarg。warnings.warn(“未找到带标签的对象。”

提前致谢 !我的参考资料:如何在Python中制作3D散点图? https://pythonspot.com/3d-scatterplot/ https://jakevdp.github.io/PythonDataScienceHandbook/04.12-three-Dimension-plotting.html

这是MWE:

import re, seaborn as sns, numpy as np, pandas as pd, random
from pylab import *
from matplotlib.pyplot import plot, show, draw, figure, cm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
sns.set_style("whitegrid", {'axes.grid' : False})

fig = plt.figure(figsize=(6,6))

ax = Axes3D(fig) # Method 1
# ax = fig.add_subplot(111, projection='3d') # Method 2

x = np.random.uniform(1,20,size=20)
y = np.random.uniform(1,100,size=20)
z = np.random.uniform(1,100,size=20)


ax.scatter(x, y, z, c=x, marker='o')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()
Run Code Online (Sandbox Code Playgroud)

3D图

#Seaborn pair plot
df_3d = pd.DataFrame()
df_3d['x'] = x
df_3d['y'] = y
df_3d['z'] = z

sns.pairplot(df_3d, hue='x')
Run Code Online (Sandbox Code Playgroud)

Seaborn对图

Léo*_*ard 9

  1. Seaborn 的调色板可以从ListedColorMap类的实例转换为 Matplotlib 颜色图,该类的实例使用 Seaborn 调色板中的颜色列表初始化,并使用as_hex()方法(如本原始答案中所提议的那样)。

  2. Matplotlib 文档中,您可以从散点图生成图例,并获取scatter函数输出的句柄和标签。

代码的结果如下图所示。请注意,我生成了更多数据点,以便更好地查看颜色图是否相同。此外,输出的ListedColorMap输出带有透明度变化的颜色图,因此我必须alpha在散点图中手动设置为 1。

import re, seaborn as sns
import numpy as np

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import ListedColormap

# generate data
n = 200
x = np.random.uniform(1, 20, size=n)
y = np.random.uniform(1, 100, size=n)
z = np.random.uniform(1, 100, size=n)

# axes instance
fig = plt.figure(figsize=(6,6))
ax = Axes3D(fig, auto_add_to_figure=False)
fig.add_axes(ax)

# get colormap from seaborn
cmap = ListedColormap(sns.color_palette("husl", 256).as_hex())

# plot
sc = ax.scatter(x, y, z, s=40, c=x, marker='o', cmap=cmap, alpha=1)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

# legend
plt.legend(*sc.legend_elements(), bbox_to_anchor=(1.05, 1), loc=2)

# save
plt.savefig("scatter_hue", bbox_inches='tight')
Run Code Online (Sandbox Code Playgroud)

带有seaborn色调调色板和图例的散点图


小智 3

图 2 没有调色板规范,但看起来它是来自 matplotlib 的配对定性颜色图(来自此处)。因此,您需要在 3D 绘图的代码中使用cmap参数和palette配对图中的选项来指定这一点。

传说更难。您可以从 legend_elements 制作一个。这里更好地解释。

所以你的代码看起来像这样(我摆脱了未使用的导入):

import seaborn as sns, numpy as np, pandas as pd, random
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
sns.set_style("whitegrid", {'axes.grid' : False})

fig = plt.figure(figsize=(6,6))

ax = Axes3D(fig)

x = np.random.uniform(1,20,size=20)
y = np.random.uniform(1,100,size=20)
z = np.random.uniform(1,100,size=20)


g = ax.scatter(x, y, z, c=x, marker='o', depthshade=False, cmap='Paired')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

# produce a legend with the unique colors from the scatter
legend = ax.legend(*g.legend_elements(), loc="lower center", title="X Values", borderaxespad=-10, ncol=4)
ax.add_artist(legend)

plt.show()
Run Code Online (Sandbox Code Playgroud)