0

I have read a list of 3D points from a text file. The list looks like follows:

content = ['2.449,14.651,-0.992,', '6.833,13.875,-1.021,', '8.133,17.431,-1.150,', '3.039,13.724,-0.999,', '16.835,9.456,-1.031,', '16.835,9.457,-1.031,', '15.388,5.893,-0.868,', '13.743,25.743,-1.394,', '14.691,24.988,-1.387,', '15.801,25.161,-1.463,', '14.668,23.056,-1.382,', '22.378,20.268,-1.457,', '21.121,17.041,-1.353,', '19.472,13.555,-1.192,', '22.498,20.115,-1.436,', '13.344,-33.672,-0.282,', '13.329,-33.835,-0.279,', '13.147,-30.690,-0.305,', '13.097,-28.407,-0.339,', '13.251,-28.643,-0.366,', '13.527,-25.067,-0.481,', '19.433,-33.137,-0.408,', '19.445,-29.501,-0.345,', '20.592,-28.004,-0.312,', '19.109,-26.512,-0.380,', '18.521,-24.155,-0.519,', '22.837,48.245,-2.201,', '23.269,50.129,-2.282,', '23.499,46.652,-2.297,', '23.814,48.646,-2.271,', '30.377,46.501,-2.214,', '29.869,44.479,-2.143,', '29.597,41.257,-2.018,', '28.134,40.291,-2.159,', '-40.932,-0.320,-1.390,', '-36.808,0.442,-1.382,', '-30.831,0.548,-1.288,', '-29.404,1.235,-1.300,', '-26.453,1.424,-1.261,', '-30.559,2.775,-1.249,', '-27.714,3.439,-1.201,']

I want to plot all the points on a 3D plot. I have this so far:

#!/usr/bin/env python

import numpy as np
import matplotlib.pyplot as plt

with open("measurements.txt") as f:
    content = f.read().splitlines()
#print content

for value in content:
    x, y, z = value.split(',')

#print x, y, z 
fig = plt.figure()
ax = plt.axes(projection='3d')

ax.scatter(x, y, z)

fig.savefig('scatterplot.png')

It throws an error:

Traceback (most recent call last): File "plotting.py", line 11, in x, y, z = value.split(',') ValueError: too many values to unpack

How do I plot these points? Thank you for your help.

5 Answers 5

1

First of all you need to take the values into respective arrays by spitting lines in file then pass them to the function.

content = ['2.449,14.651,-0.992,', '6.833,13.875,-1.021,', '8.133,17.431,-1.150,', '3.039,13.724,-0.999,', '16.835,9.456,-1.031,', '16.835,9.457,-1.031,', '15.388,5.893,-0.868,', '13.743,25.743,-1.394,', '14.691,24.988,-1.387,', '15.801,25.161,-1.463,', '14.668,23.056,-1.382,', '22.378,20.268,-1.457,', '21.121,17.041,-1.353,', '19.472,13.555,-1.192,', '22.498,20.115,-1.436,', '13.344,-33.672,-0.282,', '13.329,-33.835,-0.279,', '13.147,-30.690,-0.305,', '13.097,-28.407,-0.339,', '13.251,-28.643,-0.366,', '13.527,-25.067,-0.481,', '19.433,-33.137,-0.408,', '19.445,-29.501,-0.345,', '20.592,-28.004,-0.312,', '19.109,-26.512,-0.380,', '18.521,-24.155,-0.519,', '22.837,48.245,-2.201,', '23.269,50.129,-2.282,', '23.499,46.652,-2.297,', '23.814,48.646,-2.271,', '30.377,46.501,-2.214,', '29.869,44.479,-2.143,', '29.597,41.257,-2.018,', '28.134,40.291,-2.159,', '-40.932,-0.320,-1.390,', '-36.808,0.442,-1.382,', '-30.831,0.548,-1.288,', '-29.404,1.235,-1.300,', '-26.453,1.424,-1.261,', '-30.559,2.775,-1.249,', '-27.714,3.439,-1.201,']

import numpy as np
import matplotlib.pyplot as plt

#with open("measurements.txt") as f:
    #content = f.read().splitlines()
#print content

#for value in content:
#    x, y, z = value.split(',')

x = [float(i.split(',')[0]) for i in content]
y = [float(i.split(',')[1]) for i in content]
z = [float(i.split(',')[2]) for i in content]
#print(x, y, z)
fig = plt.figure()
ax = plt.axes(projection='3d')

ax.scatter(x, y, z)

fig.savefig('scatterplot.png')

output

Output

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

Comments

1

It's clear ! when you do your split there is 4 values

content = ['2.449,14.651,-0.992,', '6.833,13.875,-1.021,', '8.133,17.431,-1.150,', '3.039,13.724,-0.999,', '16.835,9.456,-1.031,', '16.835,9.457,-1.031,', '15.388,5.893,-0.868,', '13.743,25.743,-1.394,', '14.691,24.988,-1.387,', '15.801,25.161,-1.463,', '14.668,23.056,-1.382,', '22.378,20.268,-1.457,', '21.121,17.041,-1.353,', '19.472,13.555,-1.192,', '22.498,20.115,-1.436,', '13.344,-33.672,-0.282,', '13.329,-33.835,-0.279,', '13.147,-30.690,-0.305,', '13.097,-28.407,-0.339,', '13.251,-28.643,-0.366,', '13.527,-25.067,-0.481,', '19.433,-33.137,-0.408,', '19.445,-29.501,-0.345,', '20.592,-28.004,-0.312,', '19.109,-26.512,-0.380,', '18.521,-24.155,-0.519,', '22.837,48.245,-2.201,', '23.269,50.129,-2.282,', '23.499,46.652,-2.297,', '23.814,48.646,-2.271,', '30.377,46.501,-2.214,', '29.869,44.479,-2.143,', '29.597,41.257,-2.018,', '28.134,40.291,-2.159,', '-40.932,-0.320,-1.390,', '-36.808,0.442,-1.382,', '-30.831,0.548,-1.288,', '-29.404,1.235,-1.300,', '-26.453,1.424,-1.261,', '-30.559,2.775,-1.249,', '-27.714,3.439,-1.201,']

Solution:

for value in content:
    x, y, z,parasitic_value = value.split(',')

3 Comments

a solution, if you can edit your data to get something like that '2.449,14.651,-0.992' or define 4 values and plot only 3
How will split to 4 values help?
@abunickabhi if you take a look at content there is a gamma at the end! so the problem is the split. when he do that he got 4 values and not 3. and the last value he don't need it.
1

The element in content are:

'2.449,14.651,-0.992,'

A slightly different way to extract the data to plot from this string is to consider it as a tuple, and to use eval().

data = [eval("("+x[:len(x)-1]+")") for x in content]

Which returns:

[(2.449, 14.651, -0.992),
 (6.833, 13.875, -1.021),
 (8.133, 17.431, -1.15),
 ...
 (-30.559, 2.775, -1.249),
 (-27.714, 3.439, -1.201)]

EDIT: the error you got means:

You want 3 values, X, Y and Z; but when I split at ",", There are more (too many values to unpack).

content[0].split(",")
Out[4]: ['2.449', '14.651', '-0.992', '']

Comments

1

I see at least one error in there.

The most obvious one (because you got an error), is in splitting. The third comma at the end is causing the string to be split into four elements

>>> l = 'a,b,c,'
>>> l.split(',')
['a', 'b', 'c', '']

you can work around that by using:

x,y,z,_ = value.split(',')

the next problem you'll run into is with your loop

for value in content:
    x, y, z = value.split(',')

you are only storing the last of your values, since you overwrite them multiple times. The easiest way to work around this is creating three lists and appending into them:

x = []
y = []
z = []
for measurement in content:
    a,b,c,_ = measurement.split(',')
    x.append(a)
    y.append(b)
    z.append(c)

This is not the most efficient way, but I think it should be easier to understand.

I recommend using it like this:

x = []
y = []
z = []
with open('measurements.txt') as file:
    for line in file:
        a,b,c,_ = line.split(',')
        x.append(a)
        y.append(b)
        z.append(c)

Comments

0

To solve the main issue , you have to edit the list , and make it a 3d numpy array , by copying all the values , traversing the list via re.

Rather than assuming the list as multiple points , try to take the first 2 points or 3 points as a image/3D graph , and use imshow or Axes3D to plot it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.