Matplotlib中Z轴标签未正确打印

0 投票
1 回答
32 浏览
提问于 2025-04-14 15:31

我在做作业的时候遇到了一个烦人的问题。我想打印一个三维图,但是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()`

在这里输入图片描述

我希望标签能够完整打印出来。

1 个回答

1

这个功能可以帮助你稍微缩小视图,以便显示 z 轴的标签:

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

撰写回答