1

I have a list, the form is [[(x1, y1), (x2, y2), (x3, y3)], [...], [...]]

[[(10.0, -1.0), (7.0, 0.05889647076017157), (13.0, 0.47096776983628086)], [(10.5, -1.0), (13.0, 0.07080679131269396), (7.5, 0.16547229577841294)], [(11.0, -1.0), (8.0, 0.27471205881135075), (13.5, 0.682988382311833)]]

I would like to extract the first index in tuples from the list.

For example, the list above would be -> [[(10.0), (7.0), (13.0)], [(10.5), (13.0), (7.5)], [(11.0), (8.0), (13.5)]] (The form: [[(x1), (x2), (x3)], [(x4), (x5), (x6)], [(x7), (x8), (x9)]]

and then turn into a plot with errorbars. (The first value in the tuples would be the main values, and other two values would be errors)

This is what I am trying to get:

enter image description here

How can I do this? I can't find any similar example online.

2 Answers 2

1

You can get the list out in this way:

vals = [[i[0] for i in tup] for tup in lst ]
vals

[[10.0, 7.0, 13.0], [10.5, 13.0, 7.5], [11.0, 8.0, 13.5]]

To plot, it's easier to have it in a np matrix, and sorted because the error bar function needs the length as input and not the coordinate:

import numpy as np
import matplotlib.pyplot as plt

vals = np.sort(np.array(vals))
vals[:,[0,2]] = vals[:,[0,2]] - vals[:,1].reshape(-1,1)
vals

array([[-3. , 10. ,  3. ],
       [-3. , 10.5,  2.5],
       [-3. , 11. ,  2.5]])

fig, ax = plt.subplots(1, 1)
ax.errorbar(vals[:,1], vals[:,1], yerr=[-vals[:,0],vals[:,2]], fmt='o')
plt.show()

enter image description here

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

Comments

0

You can try

import matplotlib.pyplot as plt

x = [[(10.0, -1.0), (7.0, 0.05889647076017157), (13.0, 0.47096776983628086)], [(10.5, -1.0), (13.0, 0.07080679131269396), (7.5, 0.16547229577841294)], [(11.0, -1.0), (8.0, 0.27471205881135075), (13.5, 0.682988382311833)]]

plt.boxplot([[j[0] for j in i] for i in x])

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.