python x-y散点图中误差条的色彩Map表

qpgpyjmq  于 2023-04-10  发布在  Python
关注(0)|答案(4)|浏览(176)

我有一个时间序列的数据,我有数量y和它的误差yerr。我现在想创建一个图,显示y对相位(即时间/周期% 1)的垂直误差条(yerr)。为此,我通常使用pyplot.errorbar(time,y,yerr=yerr,...)
但是,我想使用一个颜色条/Map来指示同一图中的时间值。
我这样做是为了:

pylab.errorbar( phase, y, yerr=err, fmt=None, marker=None, mew=0 )
pylab.scatter( phase, y, c=time, cmap=cm )

不幸的是,这将绘制单色误差条(默认为蓝色)。由于我每个图有~1600个点,这使得散点图的色图消失在误差条后面。下面的图片显示了我的意思:

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

oipij1gg

oipij1gg1#

除了改变颜色之外,另一个建议是改变误差条与散点图的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()

qoefvg9y

qoefvg9y2#

我找了一段时间的解决方案,终于找到了一条路:

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

yquaqz183#

很抱歉把这个挖回来,但只是遇到类似的东西我自己,这是我的解决方案的基础上以前的React。
这会将标记、错误条和大写字母设置为色彩Map表中的相同颜色:

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)

最后,为了完整性,这里有一个它应该产生的图:

6tdlim6h

6tdlim6h4#

您可以在pylab.errorbar中使用ecolor可选参数,就像在pylab.scatter中使用color参数一样:

pylab.errorbar( phase, y, yerr=err, fmt=None, marker=None, mew=0, ecolor=time )

相关问题