Mav
Mav

Reputation: 117

Matplotlib: Scatter plot with multiple groups of individual vertical scatter plots

In a previous question, I was provided with the answer to a question involving the set up of a scatter plot with two vertical scatter plots, each with their own label.

I'm now interested in creating a more detailed plot, but I'm not even sure how. I'd ideally like to create a second set of plots with the same colors as the ones I just plotted, but grouped together and plotted beside the old ones. Each group will now have the x-axis labels "Algorithm 1" and "Algorithm 2" and the "Part 1" and "Part 2" labels will be associated with the color of the dots in the legend.

I'd like it to look something like this, but with multiple dots as opposed to bars: enter image description here

I'm really not sure how I would even achieve this in matplotlib.

Upvotes: 0

Views: 1015

Answers (1)

stochastic13
stochastic13

Reputation: 423

I am not sure if I understand your question correctly. You can do something similar to the previous question you link, but you add the color as a label and have the xticks read the algorithm number.

I am demonstrating with some random data.

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()

enter image description here

Upvotes: 1

Related Questions