0

I want to plot Points with x and y-Values and colour them depending on a corresponding time value. Data is stored in a Dataframe.

The solution should be the c-parameter of matplotlib's scatter function, but for some reason its not working for me. The times-column is a List of float values between 0 and 3. Plotting the Points without c-parameter is working.

import matplotlib.pyplot as plt

c=list(df_result_local['times'])
for i in range(len(df_result_local['Points'])):
    plt.scatter(df_result_local['Points'][i].x, df_result_local['Points'][i].y, c=c, alpha = 0.5)

Here I get a ValueError: 'c' argument has 1698 elements, which is not acceptable for use with 'x' with size 1, 'y' with size 1.

3
  • Well your error states, that your x and y axis have a length of 1, while c has a length of 1698. So you probably have to convert x and y to lists as well. Commented Nov 4, 2019 at 9:40
  • You need to convert c[i] to a color of your liking. See matplotlib.org/tutorials/colors/colormaps.html Viridis is often considered an optimal map. Commented Nov 4, 2019 at 9:41
  • x,y are the same? I mean, if x = 0, y = 0 and x = 5, y =5 and so on? You are getting x and y from the same pandas column, it's not wrong, but it's a bit weirdo. Commented Nov 4, 2019 at 9:41

2 Answers 2

1

Try this

import matplotlib.pyplot as plt

c=list(df_result_local['times'])
x = []
y = []
for i in range(len(df_result_local['Points'])):
    x.append(df_result_local['Points'][i].x)
    y.append(df_result_local['Points'][i].y)

plt.scatter(df_result_local['Points'][i].x, df_result_local['Points'][i].y, c=c, alpha = 0.5)
Sign up to request clarification or add additional context in comments.

Comments

0

I think you need to use the index on c as well. So

plt.scatter(df_result_local['Points'][i].x, df_result_local['Points'][i].y, c=c[i], alpha = 0.5)

3 Comments

I tried this allready: I get error: tuple index out of range. df_result_local['times'][0] --> 0.31, type(df_result_local['times'][0]) --> numpy.float64
As it is said in other notes, you need to have same size for c and your data
It is the same size but the point was to handle x and y as lists as well. Thnak you!

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.