问题
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.
回答1:
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()
回答2:
It might make more sense to plot using something like imshow
or pcolormesh
. This creates a "heatmap" across a grid of x,y coordinates. The x,y meshgrid is optional for these functions.
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)
You should pay attention to how you reshape colors
. You can fill either by rows or by columns. The default is by rows (last axis). This is also important to note in the other answer as you flatten your meshgrid.
来源:https://stackoverflow.com/questions/54099158/python-matplotlib-scatter-plot-colors-error