MatPlotLib用于打印的随机颜色

2024-09-24 22:26:36 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码:

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()

我的散点图有效,但 我一直收到颜色RGB或RGBA的错误消息,如下所示:

'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.

我认为我随机化的颜色有问题,但不确定该怎么解决


Tags: inimportfordateasnppltrgb
2条回答

您需要完全按照错误建议执行操作:为RGB阵列使用具有单行的2D阵列:

c = np.random.rand(1,3)

您可以将RGB元组转换为十六进制字符串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)
...

相关问题 更多 >