user3652633
user3652633

Reputation: 41

Matplotlib's PatchCollections with set_array: how to match face-color with edge-color?

I have written a python code that generates Rectangles with different dimensions. However, I'm unable to make the edgecolor match the facecolor. Here's the relevant portion of my code:

# Mapping row information to geometry and color of plot points
x = np.array(info[0])-time
y = np.array(info[1])
x_width = np.array(info[2])
y_width = np.array(info[3])
colors =  np.array(info[4])
colors = np.log10(colors)

patches = []

# Save rectangle object-datapoints to patches
(time_min,time_max) = (x-x_width/2., x+x_width/2.)
(freq_min, freq_max) = (y-y_width/2., y+y_width/2.)

for i in range(len(x)):
    f_min_plot = max(freq_min[i],f_min)
    patches.append(Rectangle((time_min[i], f_min_plot), x_width[i], freq_max[i]-f_min_plot)) 

fig = pl.figure()
pl.rc('text', usetex=True) 
ax = fig.add_subplot(111)
p = PatchCollection(patches, match_original=False)
p.set_array(colors)
ax.add_collection(p)
cbar = pl.colorbar(p)
#cbar.set_label('$\log_{10}$ SNR', rotation = 270)
pl.xlabel("Time-%.1f" %(time)) 
pl.ylabel("Frequency")
pl.semilogy()
pl.title("%s1 omicron triggers"%(detector))
pl.xlim(-time_interval,time_interval)
pl.ylim(20,2000)
pl.savefig("%s/%s1-omicron-trigs-%.1f-%d.png" %(file_path, detector, time,  time_interval))

This code generates the default rectangle facecolors with black borders. I need the border colors to match the facecolor. Note that I have tried removing the border with edgecolor = "none". Unfortunately, this makes seeing the color of some of the rectangles (those with small width) rather difficult. I have also tried adding color = matplotlib.cm.jet(color_norm[i] # color_norm is the "normalized" color array: color/max(color)) in the Rectangle() statement (and of course match_original = True in the PatchCollections() statement). But that doesn't work either. It seems to me that set_array() is what eventually determines the colors. I couldn't find any elaborate documentation on what set_array does exactly and how one could use it.

Is there any way to get this code to plot rectangles with identical face-color and edge-color? I sincerely appreciate any help on this problem.

Upvotes: 4

Views: 1226

Answers (1)

red_tiger
red_tiger

Reputation: 1492

You can use the set_edgecolor command using the 'face' key-word like this:

p.set_edgecolor('face')

This does the trick for me and sets the edge colour to the same value as the face colour.

Upvotes: 2

Related Questions