[英]Graphic with color gradient in python
I'm solving some equations of motion in Python, but I found some problems when wanting to plot my results. 我正在用Python解决一些运动方程,但是当想要绘制结果时发现了一些问题。
I have different curves of phase space, ie velocity versus position curves and I'm using Pyplot to graph them. 我有不同的相空间曲线,即速度与位置曲线,并且我正在使用Pyplot对其进行绘图。
I would like to graph them using a gradient of colors, like the figure below. 我想使用颜色渐变来绘制它们的图形,如下图所示。
This chart was made in Matlab, however with Python I can not repeat the same graph. 该图表是在Matlab中制作的,但是使用Python我无法重复相同的图表。 At most I have the following:
最多我有以下内容:
Where each line of the graphic is a curve different phase space, it is the same curve. 图形的每条线是相空间不同的曲线,它是同一条曲线。 Then the code I'm using to plot:
然后是我用来绘制的代码:
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.
I hope I was clear enough with what I want, and I hope you can help me, thank you very much in advance! 我希望我对我想要的东西足够清楚,希望您能对我有所帮助,非常感谢!
You can calculate the color of each line, eg calculating Red-Green-Blue values, each in the interval [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()
Also look into specifying a colorbar and choosing color values as a position in the colorbar -- that will let you adapt quickly to different journals' standards, or colorblindness-kind representations, etc. To do that, check out one of the matplotlib LineCollection examples : collections are nice to work with in the long run, and you've already organized your data well for them in res_?7. 另外,还要考虑指定一个颜色条并选择颜色值作为颜色条中的位置-这样您就可以快速适应不同期刊的标准或色盲表示形式等。为此,请查看matplotlib LineCollection示例之一 :从长远来看,可以很好地使用集合,并且您已经在res_?7中为它们很好地组织了数据。 The colormap is a property of the LineCollection, which adds one line to the example:
colormap是LineCollection的属性,该属性在示例中添加了一行:
line_segments.set_array(x)
line_segments.set_cmap(cm.coolwarm) #this is the new line
ax.add_collection(line_segments)
result: 结果:
You can get a color from colormaps defined in 'matplotlib.cm`. 您可以从“ matplotlib.cm”中定义的颜色图中获取颜色。 For example, some blue-red colormap I found at http://matplotlib.org/users/colormaps.html was seismic.
例如,我在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]')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.