0

I'm tiring to create a scatter plot that will have multiple points on y axis per x value. my data frame is the combination of the concatenation of multiple series so it looks like :

        sample 1
var A   [6.1348741][5.98921666][5.98921666]
var B   [6.06300458][6.14294531][6.14294531]
var C   [6.18084289][6.33433549 8.36140445 8.8569102 ][6.33433549 8.36140445 8.8569102 ]
var D   [6.20022251][2.99138873 6.19556828 8.36455137][2.99138873 6.19556828 8.36455137]
var E   [6.1560241][6.30790417][6.30790417]
var F   [6.17638675][6.34667509 8.74861151][6.34667509 8.74861151]

I've tried to use regex to extract the data but i'm only getting the first value and would anyway like to ideally treat each squire bracket list independently. anyone has any idea on how to go about it?

I would like to end up with a scatter plot that has the index on the x axis and the points on the y axis.

1 Answer 1

1

You could iterate through the rows, and convert each string to a list of floats. Then plot those lists:

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

df = pd.DataFrame(data={"sample 1": ['[6.1348741][5.98921666][5.98921666]',
                                     '[6.06300458][6.14294531][6.14294531]',
                                     '[6.18084289][6.33433549 8.36140445 8.8569102 ][6.33433549 8.36140445 8.8569102 ]',
                                     '[6.20022251][2.99138873 6.19556828 8.36455137][2.99138873 6.19556828 8.36455137]',
                                     '[6.1560241][6.30790417][6.30790417]',
                                     '[6.17638675][6.34667509 8.74861151][6.34667509 8.74861151]']},
                  index=['var A', 'var B', 'var C', 'var D', 'var E', 'var F'])

for idx, val in df.iterrows():
    ys = [float(v) for v in str(val[0]).replace('[', ' ').replace(']', ' ').split()]
    plt.scatter(np.repeat(idx, len(ys)), ys, c='crimson')
plt.show()

scatter plot

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

Comments

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.