我正在使用以下虛擬代碼生成 3D 圖。
import random
from matplotlib import pyplot as plt
random.seed(0)
D = [[random.random() for x in range(3)] for y in range(1000)]
df = pd.DataFrame(D,columns=['Feature_1','Feature_2','Feature_3'])
predictions = [random.randint(0,4) for x in range(1000)]
df['predictions'] = predictions
plt.rcParams["figure.figsize"]=(10,10)
plt.rcParams['legend.fontsize'] = 10
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(df['Feature_1'],df['Feature_2'],df['Feature_3'], c=df['predictions'], s =150,cmap='rainbow')
ax.legend(loc = 'upper left')
ax.set_xlabel('Feature_1',fontsize=20,labelpad=10)
ax.set_ylabel('Feature_2', fontsize=20, rotation=150,labelpad=10)
ax.set_zlabel('Feature_3', fontsize=20, rotation=60,labelpad=15)
plt.show()
我使用列預測作為標記顏色,我希望該列的每個元素都出現在圖例中,但它沒有。
這是結果圖的螢屏截圖

uj5u.com熱心網友回復:
您忘記將標簽句柄放入 scatter 函式中。如果您用以下行替換您的 scatter 呼叫,則會顯示一個圖例:
ax.scatter(
df['Feature_1'], df['Feature_2'], df['Feature_3'],
c=df['predictions'], s=150, cmap='rainbow', label='Dummy data'
)
或者將預測類顯示為標簽:
scatter = ax.scatter(df['Feature_1'], df['Feature_2'], df['Feature_3'],
c=df['predictions'], s=150, cmap='rainbow')
legend1 = ax.legend(*scatter.legend_elements(),
loc="upper left", title="Classes")
ax.add_artist(legend1)

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/353572.html
標籤:Python matplotlib
