Home > Enterprise >  How to plot coordinates from single pandas series
How to plot coordinates from single pandas series

Time:10-30

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?

CodePudding user response:

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”)

CodePudding user response:

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])

CodePudding user response:

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

  • Related