图形与python中的颜色渐变

时间:2016-01-29 19:19:22

标签: python matlab matplotlib

我在Python中解决了一些运动方程,但是在想要绘制结果时我发现了一些问题。

我有不同的相空间曲线,即速度与位置曲线,我使用Pyplot绘制它们。

我想使用渐变颜色绘制图形,如下图所示。

enter image description here

此图表是在Matlab中制作的,但是使用Python我不能重复相同的图表。我至多有以下几点:

enter image description here

图形的每一行都是曲线不同的相空间,它是相同的曲线。然后我用的代码用于绘图:

import matplotlib              
import matplotlib.mlab as mlab 
import matplotlib.pyplot as plt

plt.figure()
#plt.title('Distribucion de velocidades en el slower Li-7')
for i in range(0,199):
    plt.plot(res_s7[i],res_v7[i],color="blue")
plt.ylim([-100,1000])
plt.xlim([-0.1,0.6])
plt.xlabel('Posicion [m]')
plt.ylabel('Velocidad [m/s]')

Where res_s7 and res_v7 [i] arrangements represents the ith phase space curve.

我希望我对自己想要的东西足够清楚,我希望你能帮助我,非常感谢你!

2 个答案:

答案 0 :(得分:1)

您可以计算每一行的颜色,例如计算红绿蓝值,每个值在区间[0,1]中:

import matplotlib.pyplot as plt

plt.figure()
for i in range(0,19):
    plt.plot((-0.1, 0.6),(i, i), color=((20-i)/20., 0, i/20.)) 
plt.ylim([0,20]) 
plt.xlim([-0.1,0.6]) 
plt.xlabel('Posicion [m]')      
plt.ylabel('Velocidad [m/s]') 
plt.show()

enter image description here

还要考虑指定一个颜色条并选择颜色值作为颜色条中的位置 - 这样可以让您快速适应不同的期刊'为了做到这一点,请查看其中一个matplotlib LineCollection examples:从长远来看,收藏品很适合,并且您已经为他们很好地组织了数据在res_?7。 colormap是LineCollection的一个属性,它为示例添加了一行:

line_segments.set_array(x)
line_segments.set_cmap(cm.coolwarm)  #this is the new line
ax.add_collection(line_segments)

结果:

enter image description here

答案 1 :(得分:1)

您可以从&mat;'matplotlib.cm`中定义的色彩图中获取颜色。例如,我在http://matplotlib.org/users/colormaps.html找到的一些蓝红色图是地震。

import matplotlib              
import matplotlib.mlab as mlab 
import matplotlib.pyplot as plt
import matplotlib.cm as cm

plt.figure()
#plt.title('Distribucion de velocidades en el slower Li-7')
for i in range(0,199):
    plt.plot(res_s7[i],res_v7[i],color=cm.seismic(i))
plt.ylim([-100,1000])
plt.xlim([-0.1,0.6])
plt.xlabel('Posicion [m]')
plt.ylabel('Velocidad [m/s]')
相关问题