在Matplotlib中使用scatter()添加图例到3D散点图

在Matplotlib中使用scatter()添加图例到3D散点图

参考: Add a legend in a 3D scatterplot with scatter() in Matplotlib

在数据可视化中,3D散点图是一种常用的图形,用于展示三个维度的数据关系。Matplotlib库提供了强大的工具来创建和定制这类图表。本文将详细介绍如何在Matplotlib中使用scatter()函数创建3D散点图,并添加图例来增强图表的信息表达能力。

1. Matplotlib和3D图的基础

在开始绘制3D散点图之前,需要先了解如何在Matplotlib中设置和使用3D图形。Matplotlib通过mpl_toolkits.mplot3d模块提供了3D图形的支持。

示例代码1:创建一个基本的3D散点图

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.random.standard_normal(100)
y = np.random.standard_normal(100)
z = np.random.standard_normal(100)

ax.scatter(x, y, z)
ax.set_title("3D Scatter Plot - how2matplotlib.com")
plt.show()

Output:

在Matplotlib中使用scatter()添加图例到3D散点图

2. 添加图例

图例(Legend)是图表中用来标示各类数据的说明。在3D散点图中添加图例可以帮助观众更好地理解数据的分类或分布情况。

示例代码2:在3D散点图中添加单个图例

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.random.standard_normal(100)
y = np.random.standard_normal(100)
z = np.random.standard_normal(100)

scatter = ax.scatter(x, y, z, color='b', label='Blue Points')
ax.legend(title="Legend")
ax.set_title("3D Scatter Plot with Legend - how2matplotlib.com")
plt.show()

Output:

在Matplotlib中使用scatter()添加图例到3D散点图

示例代码3:在3D散点图中添加多个图例

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.random.standard_normal(100)
y = np.random.standard_normal(100)
z = np.random.standard_normal(100)

scatter1 = ax.scatter(x, y, z, color='b', label='Blue Points')
scatter2 = ax.scatter(x, -y, -z, color='r', label='Red Points')
ax.legend(title="Legend")
ax.set_title("3D Scatter Plot with Multiple Legends - how2matplotlib.com")
plt.show()

Output:

在Matplotlib中使用scatter()添加图例到3D散点图

3. 定制图例

Matplotlib提供了多种定制图例的方式,包括位置调整、字体修改、边框处理等。

示例代码4:调整图例位置

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.random.standard_normal(100)
y = np.random.standard_normal(100)
z = np.random.standard_normal(100)

scatter = ax.scatter(x, y, z, color='g', label='Green Points')
ax.legend(loc='upper left', title="Legend")
ax.set_title("3D Scatter Plot with Adjusted Legend Position - how2matplotlib.com")
plt.show()

Output:

在Matplotlib中使用scatter()添加图例到3D散点图

示例代码5:修改图例字体和边框

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.random.standard_normal(100)
y = np.random.standard_normal(100)
z = np.random.standard_normal(100)

scatter = ax.scatter(x, y, z, color='m', label='Magenta Points')
legend = ax.legend(loc='best', title="Legend")
legend.get_title().set_fontsize('13')  # 设置图例标题的字体大小
legend.get_frame().set_edgecolor('black')  # 设置图例边框颜色
ax.set_title("3D Scatter Plot with Custom Legend - how2matplotlib.com")
plt.show()

Output:

在Matplotlib中使用scatter()添加图例到3D散点图

4. 图例的高级应用

在复杂的数据集中,可能需要更高级的图例定制,如图例分组、图例图标大小调整等。

示例代码6:图例分组显示

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.random.standard_normal(100)
y = np.random.standard_normal(100)
z = np.random.standard_normal(100)

scatter1 = ax.scatter(x, y, z, color='c', label='Cyan Points')
scatter2 = ax.scatter(x, -y, -z, color='y', label='Yellow Points')
legend1 = ax.legend([scatter1], ['Group 1'], loc='upper left', title="Legend Group 1")
legend2 = ax.legend([scatter2], ['Group 2'], loc='upper right', title="Legend Group 2")
ax.add_artist(legend1)
ax.set_title("3D Scatter Plot with Grouped Legends - how2matplotlib.com")
plt.show()

Output:

在Matplotlib中使用scatter()添加图例到3D散点图

示例代码7:调整图例图标大小

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.random.standard_normal(100)
y = np.random.standard_normal(100)
z = np.random.standard_normal(100)

scatter = ax.scatter(x, y, z, color='k', label='Black Points')
legend = ax.legend(loc='best', title="Legend")
legend.legendHandles[0]._sizes = [30]  # 调整图例图标的大小
ax.set_title("3D Scatter Plot with Custom Legend Icon Size - how2matplotlib.com")
plt.show()

5. 结论

在本文中,我们详细介绍了如何在Matplotlib中创建3D散点图,并通过多个示例代码展示了如何添加和定制图例。通过这些技巧,可以使3D散点图的信息表达更加清晰和有效。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程