Reputation: 43
I'm solving some equations of motion in Python, but I found some problems when wanting to plot my results.
I have different curves of phase space, ie velocity versus position curves and I'm using Pyplot to graph them.
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. 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!
Upvotes: 0
Views: 4909
Reputation: 16279
You can calculate the color of each line, e.g. calculating Red-Green-Blue values, each in the interval [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. The colormap is a property of the LineCollection, which adds one line to the example:
line_segments.set_array(x)
line_segments.set_cmap(cm.coolwarm) #this is the new line
ax.add_collection(line_segments)
result:
Upvotes: 2
Reputation: 2710
You can get a color from colormaps defined in 'matplotlib.cm`. For example, some blue-red colormap I found at http://matplotlib.org/users/colormaps.html was seismic.
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]')
Upvotes: 1