【问题标题】:Graphic with color gradient in pythonpython中带有颜色渐变的图形
【发布时间】:2016-01-30 04:24:33
【问题描述】:

我正在用 Python 求解一些运动方程,但在绘制结果时发现了一些问题。

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

我想用颜色渐变来绘制它们,如下图所示。

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

其中图形的每条线是不同相空间的曲线,它是同一条曲线。然后是我用来绘图的代码:

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.

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

【问题讨论】:

  • 我没有使用过 pyplot,但您似乎在告诉它将每一行绘制为蓝色(plt.plot(res_s7[i], res_v7[i], color="blue"))。检查有关颜色属性的文档,并为每个属性传递一个不同的属性。

标签: python matlab matplotlib


【解决方案1】:

您可以计算每条线的颜色,例如计算 Red-Green-Blue 值,每个值都在区间 [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()

还可以考虑指定颜色条并选择颜色值作为颜色条中的位置——这将使您能够快速适应不同期刊的标准或色盲类表示等。为此,请查看其中一个matplotlib LineCollection examples:从长远来看,集合很适合使用,并且您已经在 res_?7 中为它们很好地组织了数据。颜色图是 LineCollection 的一个属性,它在示例中添加了一行:

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

结果:

【讨论】:

    【解决方案2】:

    您可以从“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]')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2020-06-24
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多