Home > database >  How do I cluster values of y axis against x axis in scatterplot?
How do I cluster values of y axis against x axis in scatterplot?

Time:11-30

Lets say I've 2 arrays x = [1,2,3,4,5,6,7] y = [1,2,2,2,3,4,5]

its scatter plot looks like this

enter image description here

what I want to do is that I want my x axis to look like this in the plot

0,4,8

as a result of which values of y in each piece of x should come closer . The similar behavior I've seen is bar plots where this is called clustering , how do I do the same in case of scatter plot , or is there any other plot I should be using ?

I hope my question is clear/understandable . All the help is appreciated

CodePudding user response:

With you plot, try this, before you display the plot.

plt.xticks([0,4,8]))

or

import numpy as np
plt.xticks(np.arange(0, 8 1, step=4))

Then to change the scale you can try something like this,

plt.xticks([0,4,8]))
plt.rcParams["figure.figsize"] = (10,5)

I got this with my example,

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 30)
y = np.sin(x)

plt.xticks([0,4,8])
plt.rcParams["figure.figsize"] = (7,3)
plt.plot(x, y, 'o', color='black')

output

enter image description here

CodePudding user response:

I think what you are looking for is close to enter image description here

Alternatively, if you don't care about keeping the order of your elements along the x-axis inside each category, then you can use swarmplot directly. The code can be seen below:

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

#Generating data
x= np.random.choice(8,size=(100))
y= np.random.choice(8,size=(100))


#Creating the spacing between categories
x[np.logical_and(x<2,x>=-2)]=0
x[np.logical_and(x<6, x>=2)]=4
x[np.logical_and(x<10, x>=6)]=8

#Plotting
sns.swarmplot(x=x,y=y)


plt.show()

And the output gives:

enter image description here

  • Related