0

I have the DataFrame:

df = 

sample_type         observed_data
     A          [0.2, 0.5, 0.17, 0.1]
     A          [0.9, 0.3, 0.24, 0.5]
     A          [0.9, 0.5, 0.6, 0.39]
     B          [0.01, 0.07, 0.15, 0.26]
     B          [0.08, 0.14, 0.32, 0.58]
     B          [0.01, 0.16, 0.42, 0.41]

where the data type in the observed_data column is np.array. What's the easiest and most efficient way of plotting each of the numpy arrays overlayed on the same plot using matplotlib and/or plotly and showing A and B as separate colors or line types (eg. dashed, dotted, etc.)?

1 Answer 1

3

You can use this...

df = pd.DataFrame({'sample_type' : ['A', 'A', 'A', 'B', 'B', 'B'], 
                   'observed_data' : [[0.2, 0.5, 0.17, 0.1], [0.9, 0.3, 0.24, 0.5], [0.9, 0.5, 0.6, 0.39], 
                                      [0.01, 0.07, 0.15, 0.26], [0.08, 0.14, 0.32, 0.58], [0.01, 0.16, 0.42, 0.41]]})

for ind, cell in df['observed_data'].iteritems():
    if len(cell) > 0:
        if df.loc[ind,'sample_type'] == 'A':
            plotted = plt.plot(np.linspace(0,1,len(cell)), cell, color='blue', marker = 'o', linestyle = '-.')
        else:
            plotted = plt.plot(np.linspace(0,1,len(cell)), cell, color='red', marker = '*', linestyle = ':')
plt.show()

enter image description here

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

4 Comments

was the reasoning for using np.linspace just because it fully covered the range of values in observed_data? If new values >1 need to be plotted, how can I amend the code above to just plot the numpy arrays regardless of the range of values?
The use of linspace is just to give the plot an x-axis which is equally spaced. Note that it has len(each_array) inside. You can change it from 0-1 or 0-23 or 0-100 or anything you like.... the x-axis will show the same range. If you don't need the x-ticklabels, it can beh hidden. What do you mean by new value > 1?
couldn't I just do plt.plot(cell, color='blue', marker = 'o', linestyle = '-.') without specifying the x-axis range?
I think you can, have you tried it? Think the x-axis tick labels might change. See if that works for you. If you don't care about the x-axis, you can hide it using plt.gca().axes.xaxis.set_visible(False)

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.