我想要实现的是拥有像
这样的情节
因此,如果绿线的颜色应为19的颜色,则颜色应与右侧的19到20颜色相同。我也确切地知道我将拥有值的范围(这里是17到25)。
下面的方法有时会起作用,但奇怪的是它只会每次都有效。当我再次执行它时,我突然得到一条完全蓝色的线(rgb 0 0 255)。我的做法有问题吗?
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
cmap = plt.get_cmap('cubehelix')
minval = 17
maxval = 25
bounds = np.arange(minval, maxval+1)
mynorm = matplotlib.colors.Normalize(vmin = minval, vmax = maxval)
sm = matplotlib.cm.ScalarMappable(norm=mynorm, cmap=cmap)
color = sm.to_rgba(20)
ax.plot([0, 100], [0, 100], c=color, lw=2)
ax2 = fig.add_axes([0.90, 0.1, 0.03, 0.8])
cb = matplotlib.colorbar.ColorbarBase(ax2, cmap=cmap, norm=mynorm, spacing='proportional', ticks=bounds, boundaries=bounds, format='%1i')
plt.show()
我正在使用Python 3.5.0和matplotlib 1.4.3。
答案 0 :(得分:2)
I have found that the c
parameter can sometimes produce non-intuitive results. However, simply using the color
parameter tends to be much more consistent.
Does this code fix your issue?
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
cmap = plt.get_cmap('cubehelix')
minval = 17
maxval = 25
bounds = np.arange(minval, maxval+1)
mynorm = matplotlib.colors.Normalize(vmin = minval, vmax = maxval)
sm = matplotlib.cm.ScalarMappable(norm=mynorm, cmap=cmap)
color = sm.to_rgba(20)
ax.plot([0, 100], [0, 100], color=color, lw=2) # Using 'color' instead of 'c'
ax2 = fig.add_axes([0.90, 0.1, 0.03, 0.8])
cb = matplotlib.colorbar.ColorbarBase(
ax2, cmap=cmap, norm=mynorm, spacing='proportional',
ticks=bounds, boundaries=bounds, format='%1i')
plt.show()
This issue is referenced here, https://github.com/matplotlib/matplotlib/issues/5197 it will be fixed in 1.5.0
答案 1 :(得分:-2)
您的代码看起来很好。我在Python 2.7.10 | Anaconda 2.3.0(x86_64)| OS X 10.10.5上运行它而没有收到您报告的错误。