1

I do have a list of data:

a = [[Timestamp('2015-01-01 15:00:00', tz=None), 53.0, 958.0],
     [Timestamp('2015-01-01 16:00:00', tz=None), 0.0, 900.0],
     [Timestamp('2015-01-02 11:00:00', tz=None), 543.0, 820.0], .....]

My goal is to plot the second element of each list entry vs. the third element of each list entry colorcoded by the timestamp.

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
smap = ax.scatter(a[:,1], a[:,2]) 
plt.show()

As soon as I change the line for plotting to smap = ax.scatter(a[:,1], a[:,2], c = a[:,0])

I receive the error message:

'Timestamp' object has no attribute 'view'.

I think my general question is: Is there any solution in Python to plot two columns of data color-coded by date using a third column which is a timestamp or datetime-object?

1 Answer 1

1
a = [[pd.Timestamp('2015-01-01 15:00:00', tz=None), 53.0, 958.0], 
     [pd.Timestamp('2015-01-01 16:00:00', tz=None), 0.0, 900.0], 
     [pd.Timestamp('2015-01-02 11:00:00', tz=None), 543.0, 820.0]]

df = pd.DataFrame(a).add_prefix('Col_')
df

Image

df.dtypes

Col_0    datetime64[ns]
Col_1           float64
Col_2           float64
dtype: object

Map each color to every value present in the datetime column by defining a list of desired colors inside a dictionary.

c_dict = df['Col_0'].map(pd.Series(data=list('rgb'), index=df['Col_0'].values).to_dict())
df.plot.scatter(x='Col_1', y='Col_2', c=c_dict, alpha=0.8, title='Scatter-Plot')

Image

It wouldn't be practical to use a list and populate colors for every value to be mapped against. In such cases, you are better of using colormaps to do the mapping.

c_dict = df['Col_0'].map(pd.Series(data=np.arange(3), index=df['Col_0'].values).to_dict())
df.plot.scatter(x='Col_1', y='Col_2', c=c_dict, title='Scatter-Plot', cmap=plt.cm.rainbow)

Image

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

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.