Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用matplotlib的x-y散点图中错误条的颜色图_Python_Matplotlib_Scatter Plot_Color Mapping - Fatal编程技术网

Python 使用matplotlib的x-y散点图中错误条的颜色图

Python 使用matplotlib的x-y散点图中错误条的颜色图,python,matplotlib,scatter-plot,color-mapping,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

我有一个数据的时间序列,我有数量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个点,这使得散点图的彩色贴图消失在误差条后面。下面的图片显示了我的意思:


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

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

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

除了改变颜色,另一个建议是改变误差条相对于散点图的
zorder
。这将用户的注意力集中在数据上,并绘制出错误的一般形状(我认为这是您的意图)


我在寻找解决方案一段时间后,终于找到了解决方法:

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

很抱歉把这件事翻了回去,但我自己也遇到了类似的事情,这是我根据之前的回答提出的解决方案

这将标记、错误栏和封口设置为颜色贴图中的相同颜色:

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)
最后,为了完整起见,这里有一个它应该产生什么的图:

@Hooked,我认为Random希望他/她的错误条与他/她的数据点具有相同的颜色。您的解决方案很好,因为它将错误条放在数据点下方,提高了图形的清晰度,但它无法解决错误条颜色问题。@Moi Jaunvelo:我尝试过这个方法,但不幸的是,它对我无效。请您解释一下如何将时间数组(键入float并从[0,2000.]开始运行)转换为有效的ecolor数组?谢谢,Hooked!我不知道佐德·夸格的事。虽然这并不是我所希望的答案,但目前还可以。Moijaunvelo是对的,理想情况下,我希望用与数据点相同的颜色绘制错误条。很好的解决方案,但大写字母的颜色如何?有没有一种方法可以使用colormap更新封盖的颜色?
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)