0

Here is my code:

import numpy as np
import matplotlib.dates as mdates


fig, ax = plt.subplots(figsize = (10,10))

dateList = []
countries = []
casesList = []

colors = []

for e in sorted_date:

  countries.append(e[0])
  dateList.append(e[1][0])
  casesList.append(e[1][1])

# plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))

for country, date, case in zip(countries, dateList, casesList):
  ax.scatter(date, case, c = np.random.rand(3,), edgecolors='none', label=country )

plt.legend(loc=1)
plt.show()

My scatterplot works but I kept getting the error message for the color RGB or RGBA like this:

'c' argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with 'x' & 'y'. Please use a 2-D array with a single row if you really want to specify the same RGB or RGBA value for all points.

I think there is something wrong with my randomizing the color but not sure what to fix it.

xd3262nd
  • 7
  • 1
  • 5

2 Answers2

1

You can turn your RGB tuple into an hexadecimal string as showb here:

...
for country, date, case in zip(countries, dateList, casesList):
    r, g, b = (int(255*x) for x in np.random.rand(3,))
    hexa = "#%02x%02x%02x" %(r, g, b)
    ax.scatter(date, case, c=hexa, edgecolors='none', label=country)
...
Guimoute
  • 4,407
  • 3
  • 12
  • 28
  • Oh gotcha, I am not that familiar with rgb yet. But thanks for the link. – xd3262nd Mar 22 '20 at 22:33
  • Matplotlib supports all kinds of inputs: `single_color` or `[single_color]` or `[c1, c2, ... cn]` where each color be either hexa string or rgb(a) tuple. Here we just took an extra step to make your 0..1 random a 0..255 one. – Guimoute Mar 22 '20 at 22:41
1

You need to do exactly what the error suggests: Use a 2D array with a single row for the RGB array:

c = np.random.rand(1,3)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712