很抱歉将其重新挖掘,但我自己也遇到了类似的事情,这是我根据之前的回复提出的解决方案。
这将标记、误差条和大写设置为颜色图中的相同颜色:
import matplotlib.pyplot as plt
import numpy as np
#data
time = np.arange(100.)
signal = time**2
error = np.ones(len(time))*1000
#create a scatter plot
sc = plt.scatter(time,signal,s=20,c=time)
#create colorbar according to the scatter plot
clb = plt.colorbar(sc)
#convert time to a color tuple using the colormap used for scatter
time_color = clb.to_rgba(time)
#loop over each data point to plot
for x, y, e, color in zip(time, signal, error, time_color):
plt.errorbar(x, y, e, lw=1, capsize=3, color=color)
编辑:
更改为 matplotlib v3.1.1 后,上述停止工作,但这里有一个解决方法:
import matplotlib.pyplot as plt
import numpy as np
#data
time = np.arange(100.)
signal = time**2
error = np.ones(len(time))*1000
#create a scatter plot
sc = plt.scatter(time,signal,s=0,c=time)
#create colorbar according to the scatter plot
clb = plt.colorbar(sc)
#convert time to a color tuple using the colormap used for scatter
import matplotlib
import matplotlib.cm as cm
norm = matplotlib.colors.Normalize(vmin=min(signal), vmax=max(signal), clip=True)
mapper = cm.ScalarMappable(norm=norm, cmap='viridis')
time_color = np.array([(mapper.to_rgba(v)) for v in signal])
#loop over each data point to plot
for x, y, e, color in zip(time, signal, error, time_color):
plt.plot(x, y, 'o', color=color)
plt.errorbar(x, y, e, lw=1, capsize=3, color=color)
最后为了完整起见,这里是它应该产生的图表: