1

I want to exact the value of a column in pandas and plot them, I coded like that:

traffic = pd.read_csv('traffic.csv')
traffic.columns = ['X', 'Y', 'Zone', 'Potential']
X = traffic.iloc[0:579, 0:1]['X']
Y = traffic.iloc[0:579, 1:2]['Y']
Z = traffic.iloc[0:579, 3:4]['Potential']

fig = plt.figure()
for x, y, z in zip(list(X), list(Y), list(Z)):
    plt.plot(x, y, 'ro', markersize = Z, alpha = 0.5)
plt.show()

And got a TypeError: cannot convert the series to <class 'float'>. Now it seems it gives me a data with tails or something (if I print() them in the bottom there'll be some Name: X, dtype:float64), so failed the for loop.

So how can I get the value of a column?

The data is like that(copied from the .csv file):

413 359 A   1.7
403 343 A   2.1
383.5   351 A   2.2
381 377.5   A   1.7
339 376 A   2.1
335 383 A   2.5
317 362 A   2.4
334.5   353.5   A   2.4
333 342 A   2.1
282 325 A   1.6
...

1 Answer 1

1

You can use pandas' matplotlib wrapper

traffic.plot(x='X', y='Y', style='ro', markersize='Potential', alpha=0.5)
plt.show()

Edit :

Apparently, pandas wrapper does not allow markersize to be variable.

Though, you can use a standard scatter plot :

plt.scatter(df['X'],df['Y'], s=df['Potential'])
Sign up to request clarification or add additional context in comments.

3 Comments

Idk... it gave me ValueError: could not convert string to float: 'Potential'
try: traffic['Potential'] = traffic['Potential'].astype(float)
Apparently, makersize don't accept variable size. You can either, fix the size or use standard scatter plot.

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.