Somehow, assigning colors to circles works different from assigning colors in scatter plots:
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(6,6)) # give plots a rectangular frame
N = 4
r = 0.1
pos = 2.*np.random.rand(N,2) -1
# give different points different color
col = 1./N*np.arange(0,N)
# Method 1
for i,j,k in zip(pos[:,0],pos[:,1],col):
circle = plt.Circle((i,j), r, color = k)
fig.gca().add_artist(circle)
plt.show()
# Method 2
plt.scatter(pos[:,0],pos[:,1], c = col)
plt.show()
Why does Method 2 work while Method 1 gives the following error:
ValueError: to_rgba: Invalid rgba arg "0.0"
to_rgb: Invalid rgb arg "0.0"
cannot convert argument to rgb sequence
The error you're getting is because you need to use the string representation of the float rather than the float value directly, for example:
circle = plt.Circle((i,j), r, color=`k`) # or str(k)
Note in the above I'm using backward ticks, a shorthand for str(k)
, which converts a float to string, like str(.75) = "0.75"
, and will give different colors for each k
value.
Here are the docs on to_rgba
to which the error refers.
Edit:
There are many ways to specify a color in matplotlib. In the above, you set the float that references a colormap through a string representation of a float. The colormap for this could then be set through a PolyCollection.
In your case, to use Circle
more like scatter
, it's probably easiest to just set the color directly, and that can be done using an rgba
tuple, for example, one that can be looked up from a colormap.
Below is an example using three different colormaps for the different y
ranges.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as clrs
import matplotlib
N, r = 200, .1
cms = matplotlib.cm
maps = [cms.jet, cms.gray, cms.autumn]
fig = plt.figure(figsize=(6,6)) # give plots a rectangular frame
ax = fig.add_subplot(111)
pos = 2.999*np.random.rand(N,2)
for x, y in pos:
cmi = int(y) # an index for which map to use based on y-value
#fc = np.random.random() # use this for random colors selected from regional map
fc = x/3. # use this for x-based colors
color = maps[cmi](fc) # get the right map, and get the color from the map
# ie, this is like, eg, color=cm.jet(.75) or color=(1.0, 0.58, 0.0, 1.0)
circle = plt.Circle((x,y), r, color=color) # create the circle with the color
ax.add_artist(circle)
ax.set_xlim(0, 3)
ax.set_ylim(0, 3)
plt.show()
In the above I made the color for each band vary with x
because I thought it looked good, but you can also do random colors, of course. Just switch which fc
line is being used:
In order to use the predetermined colors of matplot lib, you should pass strings to the color field. In this case 'k' will be black color instead of simply k.
This code did not give errors for me:
for i,j,k in zip(pos[:,0],pos[:,1],col):
circle = plt.Circle((i,j), r, color = 'k')
fig.gca().add_artist(circle)
plt.show()
Please make sure in your next question you provide code that is runnable. In this case variables N and r were not defined.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.