python matplotlib散点图颜色错误 [英] python matplotlib scatter plot colors error

查看:63
本文介绍了python matplotlib散点图颜色错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

# 生成 x(0 - 359) 和 y(-90 - 90) 的范围x,y = np.meshgrid(范围(0,360),范围(-90,90))颜色 = [一个很长的列表(64800 个值,每个点一个)]打印(颜色)plt.scatter(x, y, c=colors)plt.colorbar()plt.show()

错误:

<块引用>

  Traceback(最近一次通话最近):文件C:\python3.6.6\lib\site-packages\matplotlib\colors.py",第 158 行,在 to_rgba 中rgba = _colors_full_map.cache [c,alpha]KeyError:(1.0986122886681098,None)在处理上述异常的过程中,又发生了一个异常:回溯(最近一次调用最后一次):分散在第4210行的文件"C:\ python3.6.6 \ lib \ site-packages \ matplotlib \ axes \ _axes.py"颜色 = mcolors.to_rgba_array(c)to_rgba_array中的文件"C:\ python3.6.6 \ lib \ site-packages \ matplotlib \ colors.py",第259行result [i] = to_rgba(cc,alpha)to_rgba中的第160行的文件"C:\ python3.6.6 \ lib \ site-packages \ matplotlib \ colors.py"rgba = _to_rgba_no_colorcycle(c,alpha)_to_rgba_no_colorcycle中的第211行的文件"C:\ python3.6.6 \ lib \ site-packages \ matplotlib \ colors.py"raise ValueError("无效的 RGBA 参数:{!r}".format(orig_c))值错误:无效的 RGBA 参数:1.0986122886681098在处理上述异常的过程中,又发生了一个异常:回溯(最近一次调用最后一次):在模块"中的文件"C:/用户/允许中性标记/文档/文档/alpha/grbs data/grbs/find_burst_location.py",第168行.主要的()主目录中的文件"C:/Users/mit neumark/Documents/עמית/alpha/grbs data/grbs/find_burst_location.py"ra2,dec2 = chi_square(模型,关系)文件C:/Users/amit neumark/Documents/עמית/alpha/grbs data/grbs/find_burst_location.py",第 33 行,在 chi_square 中create_plot(总和)文件C:/Users/amit neumark/Documents/עמית/alpha/grbs data/grbs/find_burst_location.py",第 134 行,在 create_plot 中plt.scatter(x, y, c=colors)分散在第2793行的文件"C:\ python3.6.6 \ lib \ site-packages \ matplotlib \ pyplot.py"verts = verts,edgecolors = edgecolors,data = data,** kwargs)内部文件1785行中的文件"C:\ python3.6.6 \ lib \ site-packages \ matplotlib \ __ init__.py"返回 func(ax, *args, **kwargs)文件C:\python3.6.6\lib\site-packages\matplotlib\axes\_axes.py",第 4223 行,散点图.format(nc = n_elem,xs = x.size,ys = y.size)ValueError: 'c' 参数有 64800 个元素,不能与大小为 64800 的 'x' 和大小为 64800 的 'y' 一起使用.

解决方案

问题出在你的 xy 数据而不是颜色 c 参数.您的 x 和 y 当前是一个二维数组(网格网格).它应该是职位列表.一种方法是展平二维网格以获得一维数组.将保持 x 和 y 数据点之间的一一对应关系.网格物体在散点3d图中正常工作.

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

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

I am trying to create a scatter plot with x and y grid where every point gets a color by a preassigned value:

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

I have a list of x and y and another list for the values, tried using this:

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

Errors:

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.

解决方案

The problem is in your x and y data and not in the colors c parameter. Your x and y is currently a 2d array (meshgrid). It should be a list of positions. One way to do so is to flatten your 2d meshgrids to get a 1-d array. The one to one correspondence between x and y data points will be maintained. The meshgrids work normally for scatter 3d plots.

I am choosing some random colors to provide a solution.

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

这篇关于python matplotlib散点图颜色错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆