1

I have a pandas series called df1['geometry.coordinates'] of coordinate values in the following format:

geometry.coordinates
0   [150.792711, -34.210868]
1   [151.551228, -33.023339]
2   [148.92149870748742, -34.767207772932835]
3   [151.033742, -33.919998]
4   [150.953963043732, -32.3935017885229]
...     ...
432     [114.8927165, -28.902492300000002]
433     [115.34601918477634, -30.041742290803096]
434     [115.4632611, -30.8581035]
435     [121.42151909999998, -30.7804027]
436     [115.69424934340425, -30.680970908597665]

I want to plot each point on a graph, probably through using a scatter plot.

I tried: df1['geometry.coordinates'].plot.scatter() but it gets confused because it only reads it as one list value rather than two and therefore I always get the following error:

TypeError: scatter() missing 2 required positional arguments: 'x' and 'y'

Anyone know how I can solve this?

5 Answers 5

1

You need to separate the column containing the list so that you can specify x and y in the plot call.

You can split a column containing a list by constructing a data frame from a list.

pd.DataFrame(df2["geometry.coordinates"].to_list(), columns=['x', 'y']).plot.scatter(x=“x”, y=“y”)
Sign up to request clarification or add additional context in comments.

Comments

0

Step 1: Split array into multiple columns

df1[['x','y']] = pd.DataFrame(df1['geometry.coordinates'].tolist(), index= df1.index)

Step 2: Plot

df1.plot.scatter(x = 'x', y = 'y', s = 30) #s is size of dots

Tried at my end Output

6 Comments

Thanks for that, I thought this might work but I get the following error now: TypeError: object of type 'float' has no len()
getting this error at the step 1?
Yes at step 1, converting to a list works fine, but the error happens when the list is in the dataframe
I did it exactly as you wrote, I just tried it using just the two values you did and that worked. I think it might be because I have NaN values in the series, I'll try get rid of them and give it another go.
Ok dropping the NaNs worked, thanks for that!
|
0

You are not giving the parameters to scatter(), so the error is quite logical. Something among the lines of df.scatter.plot(df[0],df[1]) should work.

Also, as you are working working with column vectors, you need to transpose your data for it to be viewed as rows: df.scatter.plot(df.T[0],df.T[1])

Comments

0

I did it this way.

import matplotlib.pyplot as plt

geometry = pd.Series([
       [150.792711, -34.210868],
       [151.551228, -33.023339],
       [148.92149870748742, -34.767207772932835],
       [151.033742, -33.919998],
       [150.953963043732, -32.3935017885229]])

df = pd.DataFrame(geometry.to_list(), columns = ['x','y'])

plt.scatter(x = df['x'], y = df['y'],
            edgecolor ='black')
plt.grid(alpha=.15)

enter image description here

Comments

0

you can try

import pandas as pd
geometry_coordinates=[[150.792711, -34.210868],
[151.551228, -33.023339],
[148.92149870748742, -34.767207772932835],
[151.033742, -33.919998],
[150.953963043732, -32.3935017885229],
[114.8927165, -28.902492300000002],
[115.34601918477634, -30.041742290803096],
[115.4632611, -30.8581035],
[121.42151909999998, -30.7804027],
[115.69424934340425, -30.680970908597665]]

geometry_coordinates=pd.DataFrame(geometry_coordinates,columns=['lat','long'])
geometry_coordinates.plot.scatter(x='lat',y='long')

enter image description here

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.