Home > Mobile >  How can I draw a scatter plot using matplotlib, each x_tick for one column
How can I draw a scatter plot using matplotlib, each x_tick for one column

Time:11-22

I have a dataset like:

a1 = [81, 42, 73, 94, 85, 66]
a2 = [63, 55, 79, 65, 94, 76]
a3 = [3, 5, 4, 8, 7, 6]

I want to draw a scatter plot that the x_ticks will be a1, a2, a3 and the each y_tick is their scores, for example above a1 x_tick there's 6 dots.

CodePudding user response:

From what I could make of the question, this could be one simple implementation:

import matplotlib.pyplot as plt

a1 = [81, 42, 73, 94, 85, 66]
a2 = [63, 55, 79, 65, 94, 76]
a3 = [3, 5, 4, 8, 7, 6]

plt.scatter(x=[0]*len(a1), y=a1)
plt.scatter(x=[1]*len(a2), y=a2)
plt.scatter(x=[2]*len(a3), y=a3)

plt.xticks(ticks=[0,1,2], labels=["a1", "a2", "a3"])

plt.show()

With the following output: enter image description here

If you also want to only display the y values at the y axis you can add this line: plt.yticks(ticks=a1 a2 a3, labels=a1 a2 a3)

But for y values that are very close to each other (see values for a3) this will get crowded.

  • Related