Ran*_*dom 14 python matplotlib scatter-plot color-mapping
我有一个时间序列的数据,我有数量,y和它的错误,yerr.我现在想创建一个图表,显示y与相位(即时间/周期%1)和垂直错误栏(yerr).为此,我通常使用pyplot.errorbar(time,y,yerr = yerr,...)
但是,我想使用颜色条/贴图来指示同一图中的时间值.
我这样做的是以下几点:
pylab.errorbar( phase, y, yerr=err, fmt=None, marker=None, mew=0 )
pylab.scatter( phase, y, c=time, cmap=cm )
Run Code Online (Sandbox Code Playgroud)
不幸的是,这将绘制单色错误栏(默认为蓝色).由于每个绘图有大约1600个点,这使得散点图的色图在误差条后面消失.这是一张图片显示我的意思:

有没有办法让我可以使用与散点图中使用的色彩图相同的色彩图来绘制误差线?我不想调用错误栏1600次...
Hoo*_*ked 15
除了更改颜色外,另一个建议是更改zorder误差线与散点图.这使用户关注数据并绘制错误的一般形状(我认为这是你的意图).
from pylab import *
# Generate some random data that looks like yours
N = 1000
X = random(N)
Y = sin(X*5) + X*random(N)*.8
Z = random(N)
ERR = X*random(N)
# These are the new arguments that I used
scatter_kwargs = {"zorder":100}
error_kwargs = {"lw":.5, "zorder":0}
scatter(X,Y,c=Z,**scatter_kwargs)
errorbar(X,Y,yerr=ERR,fmt=None, marker=None, mew=0,**error_kwargs )
xlim(0,1)
show()
Run Code Online (Sandbox Code Playgroud)

小智 7
我一直在寻找解决方案,我终于找到了解决方法:
from pylab import *
#data
time = arange(100.)
signal = time**2
error = ones(len(time))*1000
figure(1)
#create a scatter plot
sc = scatter(time,signal,s=20,c=time)
#create colorbar according to the scatter plot
clb = colorbar(sc)
#create errorbar plot and return the outputs to a,b,c
a,b,c = errorbar(time,signal,yerr=error,marker='',ls='',zorder=0)
#convert time to a color tuple using the colormap used for scatter
time_color = clb.to_rgba(time)
#adjust the color of c[0], which is a LineCollection, to the colormap
c[0].set_color(time_color)
fig = gcf()
fig.show()
xlabel('time')
ylabel('signal')
Run Code Online (Sandbox Code Playgroud)
抱歉,我要备份这个内容,但我自己也遇到了类似的情况,这是我根据之前的回复提出的解决方案。
这会将标记、错误栏和上限设置为颜色图中的相同颜色:
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)
Run Code Online (Sandbox Code Playgroud)
编辑: 更改为 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)
Run Code Online (Sandbox Code Playgroud)
您可以像使用ecolor参数 in 一样使用可选参数 in :pylab.errorbarcolorpylab.scatter
pylab.errorbar( phase, y, yerr=err, fmt=None, marker=None, mew=0, ecolor=time )
Run Code Online (Sandbox Code Playgroud)