【问题标题】:python matplotlib scatter plot colors errorpython matplotlib散点图颜色错误
【发布时间】:2021-04-15 17:41:03
【问题描述】:

我正在尝试使用 x 和 y 网格创建散点图,其中每个点都通过预先分配的值获得颜色:

{x: 1, y: 2, value: n}

我有一个 x 和 y 列表以及另一个值列表,尝试使用这个:

# make range of x(0 - 359) and y(-90 - 90)
x, y = np.meshgrid(range(0, 360), range(-90, 90))
colors = [a very long list (64800 values, one for each point)]
print(colors)
plt.scatter(x, y, c=colors)
plt.colorbar()
plt.show()

错误:

Traceback (most recent call last):
  File "C:\python3.6.6\lib\site-packages\matplotlib\colors.py", line 158, in to_rgba
    rgba = _colors_full_map.cache[c, alpha]
KeyError: (1.0986122886681098, None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\python3.6.6\lib\site-packages\matplotlib\axes\_axes.py", line 4210, in scatter
    colors = mcolors.to_rgba_array(c)
  File "C:\python3.6.6\lib\site-packages\matplotlib\colors.py", line 259, in to_rgba_array
    result[i] = to_rgba(cc, alpha)
  File "C:\python3.6.6\lib\site-packages\matplotlib\colors.py", line 160, in to_rgba
    rgba = _to_rgba_no_colorcycle(c, alpha)
  File "C:\python3.6.6\lib\site-packages\matplotlib\colors.py", line 211, in _to_rgba_no_colorcycle
    raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
ValueError: Invalid RGBA argument: 1.0986122886681098

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/amit neumark/Documents/עמית/alpha/grbs data/grbs/find_burst_location.py", line 168, in <module>
    main()
  File "C:/Users/amit neumark/Documents/עמית/alpha/grbs data/grbs/find_burst_location.py", line 161, in main
    ra2, dec2 = chi_square(model, relations)
  File "C:/Users/amit neumark/Documents/עמית/alpha/grbs data/grbs/find_burst_location.py", line 33, in chi_square
    create_plot(sums)
  File "C:/Users/amit neumark/Documents/עמית/alpha/grbs data/grbs/find_burst_location.py", line 134, in create_plot
    plt.scatter(x, y, c=colors)
  File "C:\python3.6.6\lib\site-packages\matplotlib\pyplot.py", line 2793, in scatter
    verts=verts, edgecolors=edgecolors, data=data, **kwargs)
  File "C:\python3.6.6\lib\site-packages\matplotlib\__init__.py", line 1785, in inner
    return func(ax, *args, **kwargs)
  File "C:\python3.6.6\lib\site-packages\matplotlib\axes\_axes.py", line 4223, in scatter
    .format(nc=n_elem, xs=x.size, ys=y.size)
ValueError: 'c' argument has 64800 elements, which is not acceptable for use with 'x' with size 64800, 'y' with size 64800.

【问题讨论】:

  • 我们需要知道颜色列表中的值。

标签: python matplotlib scatter-plot


【解决方案1】:

问题在于您的 xy 数据,而不是颜色 c 参数。您的 x 和 y 当前是二维数组(网格)。它应该是一个职位列表。一种方法是展平二维网格以获得一维数组。将保持 x 和 y 数据点之间的一一对应关系。网格网格通常适用于散点 3d 图。

我正在选择一些随机颜色来提供解决方案。

x, y = np.meshgrid(range(0, 360), range(-90, 90))
colors = np.random.random(360*180)
plt.scatter(x.flatten(), y.flatten(), c=colors)
plt.colorbar()

【讨论】:

    【解决方案2】:

    使用imshowpcolormesh 之类的东西可能更有意义。这会在 x,y 坐标网格上创建一个“热图”。 x,y 网格对于这些功能是可选的。

    colors = np.arange(64800)
    
    plt.pcolormesh(colors.reshape(360, 180).T)
    # OR #
    x, y = np.meshgrid(range(0, 360), range(-90, 90))
    plt.pcolormesh(x, y, colors.reshape(360, 180).T)
    

    你应该注意你如何重塑colors。您可以按行或按列填充。默认是按行(最后一个轴)。当您展平网格时,在其他答案中也需要注意这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-10
      • 1970-01-01
      • 2020-12-30
      • 2011-08-29
      • 2015-04-19
      • 1970-01-01
      • 2013-11-19
      相关资源
      最近更新 更多