python中带颜色渐变的图形

2024-04-30 00:48:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用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.

我希望我对我想要的足够清楚,我希望你能帮助我,提前非常感谢你!在


Tags: importmatplotlibas图表绘制resplt曲线
2条回答

您可以计算每行的颜色,例如计算红、绿、蓝值,每行的间隔为[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

另外,还可以考虑指定一个colorbar并选择color值作为colorbar中的一个位置,这样可以让您快速适应不同期刊的标准,或者色盲类型的表示等。为此,请查看matplotlib LineCollection examples:从长远来看,集合很适合使用,你已经为他们整理好你的数据了?7colormap是LineCollection的属性,它向示例中添加一行:

^{pr2}$

结果:

enter image description here

可以从'地图库.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]')

相关问题 更多 >