Ich muss eine Sammlung von Patches mit einer benutzerdefinierten Colormap plotten. Ich habe das funktioniert gut, aber ich kann alpha nicht richtig mit meiner benutzerdefinierten Colormap verwenden. Wie Sie in den Bildern und dem angehängten Code sehen können, wird das Alpha auf die Patches angewendet, aber die Farbleiste wird immer noch mit Alpha = 1 angezeigt, was die Karte "falsch" macht. Um das zu überprüfen, habe ich das mit Jet getestet, und die Farbleiste spiegelt das richtige Alpha wieder. Ich gehe davon aus, etwas in der Art und Weise fehlt ich meine colorbar definiert, aber die Dokumentation wird in herauszufinden, was ...implementieren Sie Alpha für benutzerdefinierte Colormap
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
x = np.random.rand(50,1)*500; y = np.random.rand(50,1)*500;
radius = np.random.rand(50,1)*50
patches = []
for i in range(len(radius)):
circle = matplotlib.patches.Circle((x[i], y[i]), radius[i])
patches.append(circle)
fig, ax = plt.subplots()
colors = 100*np.random.rand(len(patches))
p = matplotlib.collections.PatchCollection(patches)
p.set(array = colors, cmap = 'Spectral', alpha=0.5)
ax.add_collection(p)
plt.colorbar(p, alpha=0.5)
plt.xlim(0,500);plt.ylim(0,500);
plt.gca().set_aspect(1)
plt.show()
############
r = np.hstack((np.zeros(425),np.linspace(0,255,430), np.linspace(254,0,425)))
g = np.hstack((np.linspace(0,255,430), np.linspace(254,0,425), np.zeros(425)))
b = np.hstack((np.linspace(255,0,430), np.zeros(425),np.linspace(0,254,425)))
c = np.array([r,g,b]).T
mycm = matplotlib.colors.ListedColormap(c/255.0)
###########
x = np.random.rand(50,1)*500; y = np.random.rand(50,1)*500;
radius = np.random.rand(50,1)*50
patches = []
for i in range(len(radius)):
circle = matplotlib.patches.Circle((x[i], y[i]), radius[i])
patches.append(circle)
fig, ax = plt.subplots()
colors = 100*np.random.rand(len(patches))
p = matplotlib.collections.PatchCollection(patches)
p.set(array = colors, cmap = mycm, alpha=0.5)
ax.add_collection(p)
plt.colorbar(p, alpha=0.5)
plt.xlim(0,500);plt.ylim(0,500);
plt.gca().set_aspect(1)
plt.show()
Relevant: https: //github.com/matplotlib/matplotlib/issues/5151 – tom