0

Here is my code (adapted from here):

df_1 = pd.DataFrame({'Cells' : np.arange(0,100), 'Delta_7' : np.random.rand(100,), 'Delta_10' : np.random.rand(100,), 'Delta_14' : np.random.rand(100,)}, columns = ['Cells','Delta_7', 'Delta_10', 'Delta_14'])



#figure
fig, ax1 = plt.subplots()
fig.set_size_inches(13, 10)



#c sequence
c = df_1['Delta_7']

#plot

plt.scatter(np.full((len(df_1), 1), 1), df_1['Delta_7'] , s = 50, c=c, cmap = 'viridis')
plt.scatter(np.full((len(df_1), 1), 2), df_1['Delta_10'] , s = 50, c=c, cmap = 'viridis')
plt.scatter(np.full((len(df_1), 1), 3), df_1['Delta_14'] , s = 50, c=c, cmap = 'viridis')
cbar = plt.colorbar()

I would like to make a beautiful jitterplot (like on R or seaborn) with matplotlib. The thing is that I would like to give each cell a color based on its 'Delta_7' value. And this color would be kept when plotting 'Delta_10' and 'Delta_14', that I didn't manage to do with seaborn. Please, could you let me know if you have any clue (python package, coding tricks …)?

Kindly,

2
  • When you tried with seaborn did you use the hue argument? Commented Feb 14, 2020 at 16:11
  • Hi @tomjn! The problem is I do not know how to plot all the 'Delta's column on a same seaborn plot… However, when I tried ax = sns.stripplot(y="Delta_7", hue='Cells', palette='viridis', data=df_1, dodge=True, jitter=0.2) all dots have the same color Commented Feb 14, 2020 at 16:36

1 Answer 1

5

The positions of the dots can be obtained from the list returned by scatter. These positions can be jittered, for example only in the x-direction. Possibly the range of the x-axis needs to be extended a bit to show every displaced dot.

Here is some code to start experimenting:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

def jitter_dots(dots):
    offsets = dots.get_offsets()
    jittered_offsets = offsets
    # only jitter in the x-direction
    jittered_offsets[:, 0] += np.random.uniform(-0.3, 0.3, offsets.shape[0])
    dots.set_offsets(jittered_offsets)

df_1 = pd.DataFrame({'Cells': np.arange(0, 100),
                     'Delta_7': np.random.rand(100),
                     'Delta_10': np.random.rand(100),
                     'Delta_14': np.random.rand(100)})
fig, ax1 = plt.subplots()

columns = df_1.columns[1:]
c = df_1['Delta_7']
for i, column in enumerate(columns):
    dots = plt.scatter(np.full((len(df_1), 1), i), df_1[column], s=50, c=c, cmap='plasma')
    jitter_dots(dots)
plt.xticks(range(len(columns)), columns)
xmin, xmax = plt.xlim()
plt.xlim(xmin - 0.3, xmax + 0.3)  # make some room to show the jittered dots
cbar = plt.colorbar()
plt.show()

sample result

Sign up to request clarification or add additional context in comments.

2 Comments

Full pandas noob here... what would I need to change to jitter both in x and y direction?
@SebastianP. jittered_offsets[:, 0] += np.random.uniform(-0.3, 0.3, offsets.shape[0]) changes the x-positions of the dots. jittered_offsets[:, 1] += np.random.uniform(-0.3, 0.3, offsets.shape[0]) would change the y-positions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.