我想创建一个带有大小和 colored颜色 图例的三维散点图.但是, colored颜色 的图例仅显示列表中的第一种 colored颜色 .

import matplotlib.pyplot as plt
import matplotlib.colors
# Visualizing 5-D mix data using bubble charts
# leveraging the concepts of hue, size and depth
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
t = fig.suptitle('Wine Residual Sugar - Alcohol Content - Acidity - Total Sulfur Dioxide - Type', fontsize=14)

xs = [1,2,3,5,4]
ys = [6,7,3,5,4]
zs = [1,5,3,9,4]
data_points = [(x, y, z) for x, y, z in zip(xs, ys, zs)]

ss = [100,200,390,500,400]
colors = ['red','red','blue','yellow','yellow']

scatter = ax.scatter(xs, ys, zs, alpha=0.4, c=colors, s=ss)

ax.set_xlabel('Residual Sugar')
ax.set_ylabel('Alcohol')
ax.set_zlabel('Fixed Acidity')


legend1 = ax.legend(*scatter.legend_elements()[0],
                    loc="upper right", title="Classes", labels=colors, bbox_to_anchor=(1.5, 1),prop={'size': 20})
ax.add_artist(legend1)

# produce a legend with a cross section of sizes from the scatter
handles, labels = scatter.legend_elements(prop="sizes", alpha=0.6)
legend2 = ax.legend(handles, labels, loc="upper right", title="Sizes", bbox_to_anchor=(1.5, 0.5), prop={'size': 20})

enter image description here

推荐答案

问题可能是因为matplotlib只接收一个要打印的序列,因此假设一个图例条目就足够了.如果我分别绘制红色、蓝色和黄色系列的散点图,那么所有三个类都会正确显示在图例中(但在使用大小绘制图例时会出现问题).

这可能不是最优雅的解决方案,但可以手动创建带有类的图例:

import matplotlib.pyplot as plt
import matplotlib.colors
from matplotlib.lines import Line2D

# Visualizing 5-D mix data using bubble charts
# leveraging the concepts of hue, size and depth
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
t = fig.suptitle('Wine Residual Sugar - Alcohol Content - Acidity - Total Sulfur Dioxide - Type', fontsize=14)

xs = [1,2,3,5,4]
ys = [6,7,3,5,4]
zs = [1,5,3,9,4]
data_points = [(x, y, z) for x, y, z in zip(xs, ys, zs)]

ss = [100,200,390,500,400]
colors = ['red','red','blue','yellow','yellow']

scatter = ax.scatter(xs, ys, zs, alpha=0.4, c=colors, s=ss)

ax.set_xlabel('Residual Sugar')
ax.set_ylabel('Alcohol')
ax.set_zlabel('Fixed Acidity')

# Create additional legend
UniqueColors = list(dict.fromkeys(colors))
Legend2Add = []
for color in UniqueColors:
    Legend2Add.append( Line2D([0], [0], marker='o', color='w', label=color,
           markerfacecolor=color, markersize=15, alpha=0.4) )

# Produce a legend with a cross section of sizes from the scatter
handles, labels = scatter.legend_elements(prop="sizes", alpha=0.6)
legend1 = ax.legend(handles,
                    loc="upper right", title="Classes", handles=Legend2Add, bbox_to_anchor=(1.5, 1),prop={'size': 20})
ax.add_artist(legend1)
legend2 = ax.legend(handles, labels, loc="upper right", title="Sizes", bbox_to_anchor=(1.5, 0.5), prop={'size': 20})

plt.show()

Python相关问答推荐

ValueRight:参数目标和输出必须具有相同的形状.接收:目标.形状=(无,512),输出.形状=(无,3)

回归回溯-2D数组中的单词搜索

Pandas 群内滚动总和

使用Python Great Expectations和python-oracledb

Image Font生成带有条形码Code 128的条形码时出现枕头错误OSErsor:无法打开资源

Pandas :多索引组

使用scipy. optimate.least_squares()用可变数量的参数匹配两条曲线

Python 3.12中的通用[T]类方法隐式类型检索

大Pandas 胚胎中产生组合

如何使用symy打印方程?

Pystata:从Python并行运行stata实例

韦尔福德方差与Numpy方差不同

如何在python polars中停止otherate(),当使用when()表达式时?

如何在Python脚本中附加一个Google tab(已经打开)

未知依赖项pin—1阻止conda安装""

Django RawSQL注释字段

无论输入分辨率如何,稳定扩散管道始终输出512 * 512张图像

Numpyro AR(1)均值切换模型抽样不一致性

如何在Gekko中使用分层条件约束

BeautifulSoup:超过24个字符(从a到z)的迭代失败:降低了首次深入了解数据集的复杂性: