Matplotlib 中 Z 轴标签未正确打印

问题描述 投票:0回答:1

我在做作业时遇到了一个烦人的问题。我正在尝试打印 3D 绘图,但 Z 轴标签打印不正确。有谁知道我该如何解决这个问题?

## Plotting data points with cluster colors
ax.scatter(x[:, 1], x[:, 2], x[:, 0], s=50, c=labels.astype(float))

## Plotting cluster centers 
markers = ['o', 'v', '^', '<', '>']
cluster_colors = {i: f'C{i}' for i in range(clusterNum)}
for i in range(clusterNum):
    cluster_mask = labels == i
    ax.scatter(k_means.cluster_centers_[i, 1],
           k_means.cluster_centers_[i, 2],
           k_means.cluster_centers_[i, 0],
           s=200, marker=markers[i],
           c=cluster_colors[i],
           edgecolors='k',
           alpha=0.5)

ax.scatter(x[cluster_mask, 1], 
           x[cluster_mask, 2], 
           x[cluster_mask, 0], 
           s=50, c=cluster_colors[i], 
           marker='o', 
           edgecolors='k', 
           alpha=0.5)

ax.set_xlabel('Age', fontsize=12)
ax.set_ylabel('Income', fontsize=12)
ax.set_zlabel('Gender (0 = Female, 1 = Male)', fontsize=12)
plt.title('K-Means Clusters (3D): Age|Income|Gender')

## Create badass legend entries
legend_entries = [plt.scatter([], [], s=50, c=cluster_colors[i], 
                          marker=markers[i], edgecolors='k', 
                          label='Cluster {}'.format(i))
                          for i in range(clusterNum)]

## Again, a legend without a title, is a fokin spaghetti without 
## little meat balls. :)
plt.legend(handles=legend_entries, scatterpoints=1,
       fontsize=10, title='Cluster Centers')

plt.show()`

enter image description here

期望打印完整的标签。

python matplotlib jupyter visualization
1个回答
0
投票

这个对于缩小一点以显示 z 标签很有用:

ax.set_box_aspect(aspect=None, zoom=0.8)

© www.soinside.com 2019 - 2024. All rights reserved.