jasper
jasper

Reputation: 193

matplotlib.scatter color argument not accepting numpy array

I have written the following python plotting script using matplotlib:

import pynbody as pyn
import numpy as np
import matplotlib.pyplot as plt
import glob

s = pyn.load('./ballsV2.00001')
sl = s.g[np.where((s.g['z'] < 0.005) & (s.g['z']>-0.005))]

sx = s.s['x'][0]
sy = s.s['y'][0]
sz = s.s['z'][0]
r2 = ((s.g['x']-sx)**2+(s.g['y']-sy)**2+(s.g['z']-sz)**2)
Flux = np.array(1./(4*np.pi*r2)*np.exp(-1*7.00114988051*np.sqrt(r2)))

print(type(np.log10(sl['radFlux'])))
print(type(np.log10(Flux)))

plt.figure(figsize = (15,12))
#plt.scatter(sl['x'],sl['y'],c=np.log10(sl['radFlux']),s=75,edgecolors='none', marker = '.',vmin=-6,vmax=1)
plt.scatter(sl['x'],sl['y'],c=np.log10(Flux),s=75,edgecolors='none', marker = '.',vmin=-8,vmax=4)
plt.xlim([-0.5,0.5])
plt.ylim([-0.5,0.5])
plt.xlabel("x")
plt.ylabel("y")
plt.colorbar(label="log(Code Flux)")
plt.savefig('./ballsV2_0.1.pdf')
plt.savefig('./ballsV2_0.1.png')
plt.show()
plt.close()

When I run the script I get the following error:

foo@bar ~/Data/RadTransfer/Scaling_Tests/ballsV2 $ py 
balls.py 
balls.py:15: RuntimeWarning: divide by zero encountered in log10
    print(type(np.log10(sl['radFlux'])))
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>

Traceback (most recent call last):
  File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 141, in to_rgba
    rgba = _colors_full_map.cache[c, alpha]
KeyError: (-4.1574455411341349, None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 192, in _to_rgba_no_colorcycle
    c = tuple(map(float, c))
TypeError: 'numpy.float64' object is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "balls.py", line 17, in <module>
    plt.scatter(sl['x'],sl['y'],c=np.log10(Flux),s=75,edgecolors='none', marker = '.',vmin=-8,vmax=4)
  File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 3435, in scatter
    edgecolors=edgecolors, data=data, **kwargs)
  File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py", line 1892, in inner
    return func(ax, *args, **kwargs)
  File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 4028, in scatter
    alpha=alpha
  File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/collections.py", line 890, in __init__
    Collection.__init__(self, **kwargs)
  File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/collections.py", line 139, in __init__
    self.set_facecolor(facecolors)
  File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/collections.py", line 674, in set_facecolor
    self._set_facecolor(c)
  File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/collections.py", line 659, in _set_facecolor
    self._facecolors = mcolors.to_rgba_array(c, self._alpha)
  File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 237, in to_rgba_array
    result[i] = to_rgba(cc, alpha)
  File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 143, in to_rgba
    rgba = _to_rgba_no_colorcycle(c, alpha)
  File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 194, in _to_rgba_no_colorcycle
    raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
ValueError: Invalid RGBA argument: -4.1574455411341349

Ignore the divide by zero stuff,the issue is the scatter plot function isn't taking my array of values to map colour to. What is strange is that the commented out scatter plot command above it runs fine. The only difference is the array of values I am passing it. I made sure to cast them to the same type (they are both <class 'numpy.ndarray'>). Also, the values themselves are more sane ranging between ~4000 and 1E-7 in the Flux array, it is only the np.log10(sl['radFlux'] that has the divide by zero errors and that one works. Any suggestions?

Upvotes: 2

Views: 3157

Answers (1)

jasper
jasper

Reputation: 193

Flux and np.log10(sl['radFlux']) ended up being different lengths. sl (a slice of s) was not used to compute r2, so Flux ended up being to big. It would be nice if matplotlib checked that the color array was the same length as the scatter x and y arrays and had an error message like it does when the x and y arrays are different lengths.

Upvotes: 4

Related Questions