Matplotlib:具有多组独立垂直散点图的散点图
Matplotlib: Scatter plot with multiple groups of individual vertical scatter plots
在 中,我得到了一个问题的答案,该问题涉及使用两个垂直散点图设置散点图,每个散点图都有自己的标签。
我现在有兴趣创建一个更详细的情节,但我什至不确定如何做。理想情况下,我想创建第二组图,其颜色与我刚刚绘制的那些颜色相同,但组合在一起并在旧图旁边绘制。每个组现在都有 x 轴标签“算法 1”和“算法 2”,“第 1 部分”和“第 2 部分”标签将与图例中点的颜色相关联。
我希望它看起来像这样,但有多个点而不是条:
我真的不确定如何在 matplotlib 中实现这一点。
我不确定我是否正确理解了你的问题。你可以做一些类似于你 link 上一个问题的事情,但是你添加颜色作为标签并让 xticks 读取算法编号。
我正在用一些随机数据进行演示。
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2022)
alg1_p1 = np.random.random(5).tolist()
alg1_p2 = np.random.random(5).tolist()
alg2_p1 = np.random.random(5).tolist()
alg2_p2 = np.random.random(5).tolist()
fig, ax = plt.subplots()
# we will keep the algorithm group locations at 1 and 2
xvals = ([0.9] * len(alg1_p1)) + ([1.9] * len(alg2_p1))
# plot the part1 points
ax.scatter(xvals, alg1_p1 + alg2_p1, c='red', label='part 1')
# plot the part2 points
xvals = ([1.1] * len(alg1_p2)) + ([2.1] * len(alg2_p2))
ax.scatter(xvals, alg1_p2 + alg2_p2, c='blue', label='part 2')
ax.set_xticks([1, 2])
ax.set_xticklabels(['Algorithm 1', 'Algorithm 2'])
ax.legend()
plt.show()
在
我现在有兴趣创建一个更详细的情节,但我什至不确定如何做。理想情况下,我想创建第二组图,其颜色与我刚刚绘制的那些颜色相同,但组合在一起并在旧图旁边绘制。每个组现在都有 x 轴标签“算法 1”和“算法 2”,“第 1 部分”和“第 2 部分”标签将与图例中点的颜色相关联。
我希望它看起来像这样,但有多个点而不是条:
我真的不确定如何在 matplotlib 中实现这一点。
我不确定我是否正确理解了你的问题。你可以做一些类似于你 link 上一个问题的事情,但是你添加颜色作为标签并让 xticks 读取算法编号。
我正在用一些随机数据进行演示。
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2022)
alg1_p1 = np.random.random(5).tolist()
alg1_p2 = np.random.random(5).tolist()
alg2_p1 = np.random.random(5).tolist()
alg2_p2 = np.random.random(5).tolist()
fig, ax = plt.subplots()
# we will keep the algorithm group locations at 1 and 2
xvals = ([0.9] * len(alg1_p1)) + ([1.9] * len(alg2_p1))
# plot the part1 points
ax.scatter(xvals, alg1_p1 + alg2_p1, c='red', label='part 1')
# plot the part2 points
xvals = ([1.1] * len(alg1_p2)) + ([2.1] * len(alg2_p2))
ax.scatter(xvals, alg1_p2 + alg2_p2, c='blue', label='part 2')
ax.set_xticks([1, 2])
ax.set_xticklabels(['Algorithm 1', 'Algorithm 2'])
ax.legend()
plt.show()