从数据框中绘制散点图和图例
Plot scatter and legends from a data frame
我在散点图中显示图例时遇到问题。向您展示我的问题的一个简单示例是:
从 1 到 10 的每个整数都有颜色。我想要显示十个数字及其颜色的标签(基本上:颜色及其对应的数字)
我把所有的值都放在一个数据框中(我给你看的数据框只是一个例子,真正的例子是由数百行组成的)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2,3,4,5,6,7,8,9,10])
y = 2*x
df = pd.DataFrame()
palette = {1: "blue", 2:"orange", 3:"green", 4:"red", 5:"purple", 6:"brown", 7:"pink", 8:"gray", 9:"olive", 10:"cyan"}
df["first"] = x
df["second"] = y
df["third"] = df["first"].apply(lambda x: palette[x])
plt.scatter(df["first"], df["second"], c=df["third"])
plt.legend()
plt.show()
将参数添加到散点线没有帮助(图例 = c=df2["third"])
我找不到解决方案。
谢谢指点
标准图例中的每一行对应一个带有标签的图。您可以一次绘制一种颜色的散点图,并分配相应的标签。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = 2 * x
df = pd.DataFrame()
palette = {1: "blue", 2: "orange", 3: "green", 4: "red", 5: "purple", 6: "brown", 7: "pink", 8: "gray", 9: "olive", 10: "cyan"}
df["first"] = x
df["second"] = y
df["third"] = df["first"].apply(lambda x: palette[x])
for number, color in palette.items():
plt.scatter(x="first", y="second", c=color, data=df[df["first"] == number], label=number)
plt.legend()
plt.show()
这个过程可以简化很多,在 Seaborn 中使用 hue:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = 2 * x
df = pd.DataFrame()
palette = {1: "blue", 2: "orange", 3: "green", 4: "red", 5: "purple", 6: "brown", 7: "pink", 8: "gray", 9: "olive", 10: "cyan"}
df["first"] = x
df["second"] = y
df["third"] = df["first"].apply(lambda x: palette[x])
sns.set()
sns.scatterplot(data=df, x="first", y="second", hue="first", palette=palette)
plt.show()
我在散点图中显示图例时遇到问题。向您展示我的问题的一个简单示例是: 从 1 到 10 的每个整数都有颜色。我想要显示十个数字及其颜色的标签(基本上:颜色及其对应的数字)
我把所有的值都放在一个数据框中(我给你看的数据框只是一个例子,真正的例子是由数百行组成的)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1,2,3,4,5,6,7,8,9,10])
y = 2*x
df = pd.DataFrame()
palette = {1: "blue", 2:"orange", 3:"green", 4:"red", 5:"purple", 6:"brown", 7:"pink", 8:"gray", 9:"olive", 10:"cyan"}
df["first"] = x
df["second"] = y
df["third"] = df["first"].apply(lambda x: palette[x])
plt.scatter(df["first"], df["second"], c=df["third"])
plt.legend()
plt.show()
将参数添加到散点线没有帮助(图例 = c=df2["third"])
我找不到解决方案。
谢谢指点
标准图例中的每一行对应一个带有标签的图。您可以一次绘制一种颜色的散点图,并分配相应的标签。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = 2 * x
df = pd.DataFrame()
palette = {1: "blue", 2: "orange", 3: "green", 4: "red", 5: "purple", 6: "brown", 7: "pink", 8: "gray", 9: "olive", 10: "cyan"}
df["first"] = x
df["second"] = y
df["third"] = df["first"].apply(lambda x: palette[x])
for number, color in palette.items():
plt.scatter(x="first", y="second", c=color, data=df[df["first"] == number], label=number)
plt.legend()
plt.show()
这个过程可以简化很多,在 Seaborn 中使用 hue:
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
y = 2 * x
df = pd.DataFrame()
palette = {1: "blue", 2: "orange", 3: "green", 4: "red", 5: "purple", 6: "brown", 7: "pink", 8: "gray", 9: "olive", 10: "cyan"}
df["first"] = x
df["second"] = y
df["third"] = df["first"].apply(lambda x: palette[x])
sns.set()
sns.scatterplot(data=df, x="first", y="second", hue="first", palette=palette)
plt.show()