擅长:python、mysql、java
<p>这里有一种方法可以在仍然使用plt.plot()的情况下执行此操作。基本上,你做一个一次性的情节,从那里得到色条。</p>
<pre><code>import matplotlib as mpl
import matplotlib.pyplot as plt
min, max = (-40, 30)
step = 10
# Setting up a colormap that's a simple transtion
mymap = mpl.colors.LinearSegmentedColormap.from_list('mycolors',['blue','red'])
# Using contourf to provide my colorbar info, then clearing the figure
Z = [[0,0],[0,0]]
levels = range(min,max+step,step)
CS3 = plt.contourf(Z, levels, cmap=mymap)
plt.clf()
# Plotting what I actually want
X=[[1,2],[1,2],[1,2],[1,2]]
Y=[[1,2],[1,3],[1,4],[1,5]]
Z=[-40,-20,0,30]
for x,y,z in zip(X,Y,Z):
# setting rgb color based on z normalized to my range
r = (float(z)-min)/(max-min)
g = 0
b = 1-r
plt.plot(x,y,color=(r,g,b))
plt.colorbar(CS3) # using the colorbar info I got from contourf
plt.show()
</code></pre>
<p>有点浪费,但很方便。如果您可以调用plt.colorbar()来生成多个图,而不必重新生成它的信息,这也不是很浪费。</p>
<p><img src="https://i.stack.imgur.com/nmhtK.png" alt="enter image description here"/></p>