Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.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中的gnuplot LINECLOR变量?_Python_Plot_Matplotlib_Gnuplot - Fatal编程技术网

Python matplotlib中的gnuplot LINECLOR变量?

Python matplotlib中的gnuplot LINECLOR变量?,python,plot,matplotlib,gnuplot,Python,Plot,Matplotlib,Gnuplot,我有一个y值数组,它们形成一条直线。此外,我有一个数组,它的元素数与y数组的元素数相同,值的范围从0到1。我们将此数组称为“z”。我想绘制y值数组,以便每个点的颜色与z值对应 在gnuplot中,可以使用'lc variable'执行此操作: plot ’data’ using 1:2:3 with points lc variable 使用此处的建议: ,我能够使用散点图,它确实有效: 将matplotlib导入为mpl 将matplotlib.pyplot作为plt导入 plt.散射

我有一个y值数组,它们形成一条直线。此外,我有一个数组,它的元素数与y数组的元素数相同,值的范围从0到1。我们将此数组称为“z”。我想绘制y值数组,以便每个点的颜色与z值对应

在gnuplot中,可以使用'lc variable'执行此操作:

plot ’data’ using 1:2:3 with points lc variable  
使用此处的建议: ,我能够使用散点图,它确实有效:

将matplotlib导入为mpl
将matplotlib.pyplot作为plt导入
plt.散射(x,y,c=z,s=1,edgecolors='none',cmap=mpl.cm.jet)
plt.colorbar()
plt.show()
matplotlib中的plot方法是否有类似的实现方法

plt.plot(x,y,c=z)
当我尝试上面的代码时,所有的行都显示为黑色。

您可以使用散射:

plt.scatter(range(len(y)), y, c=z, cmap=cm.hot)
这里是ipython-pylab会话:

In [27]: z = [0.3,0.4,0.5,0.6,0.7,0.2,0.3,0.4,0.5,0.8,0.9]

In [28]: y = [3, 7, 5, 6, 4, 8, 3, 4, 5, 2, 9]

In [29]: plt.scatter(range(len(y)), y, s=60, c=z, cmap=cm.hot)
Out[29]: <matplotlib.collections.PathCollection at 0x9ec8400>
[27]中的
z=[0.3,0.4,0.5,0.6,0.7,0.2,0.3,0.4,0.5,0.8,0.9]
在[28]中:y=[3,7,5,6,4,8,3,4,5,2,9]
[29]中:plt.散射(范围(len(y)),y,s=60,c=z,cmap=cm.热)
出[29]:

如果要使用plot,可以使用(PyCastle会话)获得如上所示的等效图:

>>从matplotlib导入pyplot作为plt
>>>从matplotlib导入cm
>>>y=[3,7,5,6,4,8,3,4,5,2,9]
>>>z=[0.3,0.4,0.5,0.6,0.7,0.2,0.3,0.4,0.5,0.8,0.9]
>>>对于枚举(zip(y,z))中的x,(v,c):
...      plt.plt(x,v,marker='o',color=cm.hot(c))
...      
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
>>>plt.show()
>>> 

我也遇到了同样的问题:我想用不均匀的颜色绘制线条,这取决于第三个变量(z)

但我确实想用一条线,而不是标记(如@joaquin的回答)。 我在中找到了一个解决方案,使用类
matplotlib.collections.LineCollection
(链接)

下面是我的示例,它在底图中绘制轨迹,并根据其高度为其着色:

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib.collections import LineCollection
import numpy as np

m = Basemap(llcrnrlon=-42,llcrnrlat=0,urcrnrlon=5,urcrnrlat=50, resolution='h')
fig = plt.figure()
m.drawcoastlines()
m.drawcountries()

for i in trajectorias:
    # for each i, the x (longitude), y (latitude) and z (height)
    # are read from a file and stored as numpy arrays

    points = np.array([x, y]).T.reshape(-1, 1, 2)
    segments = np.concatenate([points[:-1], points[1:]], axis=1)

    lc = LineCollection(segments, cmap=plt.get_cmap('Spectral'),
                        norm=plt.Normalize(250, 1500))
    lc.set_array(z)
    lc.set_linewidth(2)

    plt.gca().add_collection(lc)

axcb = fig.colorbar(lc)
axcb.set_label('cota (m)')

plt.show()

谢谢joaquin。我决定使用散点而不是绘图。在gnuplot
lc变量中,variable
根据最后一列的值选择线型索引。要将其用作颜色,请使用例如
lc调色板z
。如果您希望线段之间的过渡更平滑,可以执行
segments=np。连接([点[:-2],点[1:-1],点[2:],轴=1)
相反,我试图绘制一个时间序列,其中垂直轴是粒子的垂直位移,我想使用其他属性(质量)作为颜色贴图。我得到一个错误:“TypeError:float()参数必须是字符串或数字,而不是“datetime.datetime”。有什么解决办法吗?
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib.collections import LineCollection
import numpy as np

m = Basemap(llcrnrlon=-42,llcrnrlat=0,urcrnrlon=5,urcrnrlat=50, resolution='h')
fig = plt.figure()
m.drawcoastlines()
m.drawcountries()

for i in trajectorias:
    # for each i, the x (longitude), y (latitude) and z (height)
    # are read from a file and stored as numpy arrays

    points = np.array([x, y]).T.reshape(-1, 1, 2)
    segments = np.concatenate([points[:-1], points[1:]], axis=1)

    lc = LineCollection(segments, cmap=plt.get_cmap('Spectral'),
                        norm=plt.Normalize(250, 1500))
    lc.set_array(z)
    lc.set_linewidth(2)

    plt.gca().add_collection(lc)

axcb = fig.colorbar(lc)
axcb.set_label('cota (m)')

plt.show()