1

I have a dataframe that looks like:

         date               value1          value2
0      2018-10-20             11             11
1      2018-10-20             10             10
2      2018-10-20             12              8
3      2018-10-20             12             14
4      2018-10-20             13             13
5      2018-10-20              9             12             
6      2018-10-20             11             15
7      2018-10-20             13              7
8      2018-10-20             10             11
9      2018-10-21             14             13
10     2018-10-21             11             14
11     2018-10-21             15              8
12     2018-10-21             8              11
13     2018-10-21             13             12
14     2018-10-21             14             14
15     2018-10-21             10             15
16     2018-10-22              9             11
17     2018-10-22              8              6
18     2018-10-22             12             15
19     2018-10-22             13             13
20     2018-10-22             11             10
21     2018-10-22             14              8
22     2018-10-22             15             17
23     2018-10-22             10             12
24     2018-10-22             11             11
25     2018-10-22             14              5
26     2018-10-22             17             17

I would like to plot a scattered Chart, where x=value1, y=value2 and the data are coloured depending on the date (in this case 3 different Colors, one for 20, one for 21 and one for 22).

Is that possible? Thank you in advance

Luca91
  • 541
  • 1
  • 7
  • 19

3 Answers3

1

Yes, seaborn is ideal for this and gives many options

import seaborn as sns
sns.scatterplot(x='value1', y='value2', hue='date', data=df)
Jondiedoop
  • 3,303
  • 9
  • 24
1

Yes, this is possible. Use c=to give the colors. Then, to create the colors based on dates, use something like:

v1 = [10, 11]
v2 = [11, 12]

d1 = datetime.datetime.strptime("2018-10-20", "%Y-%M-%d")
d2 = datetime.datetime.strptime("2018-10-22", "%Y-%M-%d")

# considering d1 as the minimum date, easy to retrieve
colors = np.array([(d1-d1).total_seconds(), (d2-d1).total_seconds()])
colors /= np.max(colors)

plt.scatter(x=v1, y=v2, c=colors)
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
0

I answered this question here

plt.scatter(x_vals, y_vals,  c = z_vals, cmap = 'rainbow')

Basically, set "c" equal to the dates and choose an appropriate color map.

mauve
  • 2,707
  • 1
  • 20
  • 34