Matplotlib中的gnuplot linecolor变量? [英] gnuplot linecolor variable in matplotlib?

查看:132
本文介绍了Matplotlib中的gnuplot linecolor变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个y值数组,它们构成一条线.另外,我有一个数组,该数组的元素数与值的y数组(范围为0到1)相同.我们将此数组称为"z".我想绘制y值数组,以便每个点的颜色与z值相对应.

I have an array of y-values that form a line. Additionally, I have an array with the same number of elements as the y-array of values ranging from 0 to 1. We'll call this array 'z'. I want to plot the array of y-values so that the color of each point corresponds with the z-value.

在gnuplot中,您可以使用"lc变量"进行此操作:

In gnuplot, you can do this using the 'lc variable':

plot ’data’ using 1:2:3 with points lc variable  

使用此处的建议: Matplotlib散点图;颜色作为第三个变量的函数 ,我能够使用散点图情节,那是行得通的:

Using the advice from here: Matplotlib scatterplot; colour as a function of a third variable , I was able to use a scatter plot, which did work:

import matplotlib as mpl  
import matplotlib.pyplot as plt  

plt.scatter(x, y, c=z, s=1, edgecolors='none', cmap=mpl.cm.jet)  
plt.colorbar()  
plt.show()  

有没有办法用matplotlib中的plot方法做到这一点?

Is there a way to do this with the plot method in matplotlib, similar to this?

plt.plot(x, y, c=z)

当我尝试上面的代码时,所有行都显示为黑色.

When I tried the above code, all of the lines just appeared black.

推荐答案

您可以使用散点图:

plt.scatter(range(len(y)), y, c=z, cmap=cm.hot)

在这里有ipython -pylab会话:

here you have the ipython -pylab session:

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>

如果要使用绘图,则可以使用(pycrust会话)获得与上述等效的图形:

If you want to use plot you can get the equivalent figure as above with (pycrust session):

>>> from matplotlib import pyplot as plt
>>> from matplotlib import 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]
>>> for x, (v, c) in enumerate(zip(y,z)):
...      plt.plot(x,v,marker='o', color=cm.hot(c))
...      
[<matplotlib.lines.Line2D object at 0x0000000008C42518>]
[<matplotlib.lines.Line2D object at 0x0000000008C426D8>]
[<matplotlib.lines.Line2D object at 0x0000000008C42B38>]
[<matplotlib.lines.Line2D object at 0x0000000008C452B0>]
[<matplotlib.lines.Line2D object at 0x0000000008C45438>]
[<matplotlib.lines.Line2D object at 0x0000000008C45898>]
[<matplotlib.lines.Line2D object at 0x0000000008C45CF8>]
[<matplotlib.lines.Line2D object at 0x0000000008C48198>]
[<matplotlib.lines.Line2D object at 0x0000000008C485F8>]
[<matplotlib.lines.Line2D object at 0x0000000008C48A58>]
[<matplotlib.lines.Line2D object at 0x0000000008C4B1D0>]
>>> plt.show()
>>> 

这篇关于Matplotlib中的gnuplot linecolor变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆