[英]Legend colors mismatch plot colors
我正在嘗試設置散點圖中每個點的圖例。 我的主要問題是每個點的顏色與圖例中的顏色不匹配。 我做錯了什么,我該如何糾正?
def scatter(self, indep, dep, labl):
x = self.df_input[indep]
y = self.df_input[dep]
random = np.random.RandomState(0)
colors = random.rand(len(labl)+1)
fig = plt.figure()
ax = fig.add_subplot(111)
for leg in labl:
ax.scatter(x, y, c=colors, cmap='gist_ncar', label=leg)
ax.legend()
ax.set_xlabel(indep)
ax.set_ylabel(dep)
ax.axis('tight')
plt.show()
看起來您可能正在嘗試在數據框中繪制組。 所以像這樣的東西可能有效:
import matplotlib.pyplot as plt
import pandas as pd
data = [['a', 1, 3],
['a', 2, 2],
['b', 2, 4],
['b', 1, 5],
['b', 3, 5],
]
df = pd.DataFrame(data, columns=['cat', 'x', 'y'])
for name, group in df.groupby('cat'):
plt.scatter(group.x, group.y, label=name)
plt.legend()
這會產生:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.