x-y 散点图中误差线的颜色图

问题描述 投票:0回答:4

我有一个时间序列数据,其中有数量 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 )

不幸的是,这将绘制单色错误栏(默认为蓝色)。由于我每个图有 ~1600 个点,这使得散点图的颜色图消失在误差线后面。这是一张图片,说明了我的意思:

enter image description here

有没有一种方法可以使用与散点图中使用的颜色图相同的颜色图来绘制误差线?我不想调用 errorbar 1600 次...

python matplotlib scatter-plot errorbar color-mapping
4个回答
17
投票

除了改变颜色,另一个建议是改变误差条与散点图的

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()

enter image description here


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')

7
投票

很抱歉把这个备份起来,但我自己遇到了类似的事情,这是我基于以前的回复的解决方案。

这会将标记、误差线和上限设置为颜色图中相同的颜色:

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)

最后为了完整起见,这里是它应该产生的图:


0
投票

您可以在

ecolor
中使用
pylab.errorbar
可选参数,因为您在
color
中使用
pylab.scatter
参数:

pylab.errorbar( phase, y, yerr=err, fmt=None, marker=None, mew=0, ecolor=time )
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.