Reputation: 23
I am trying to plot a function in a way similar to the one shown in the figure in the link:
The equation in which this plot is based on is: y= sin^2θ x /(e^x +1)
I have previously plotted the equation above without the sine function, using the following code:
# Generate some data.
x = np.logspace(-1, 3, 100)
y= x * 1/ (np.exp(x) + 1)
plt.loglog(x, y)
plt.autoscale(enable=True, axis='x', tight=True) # optionally set a tight x-axis
#adding labels to axis
plt.xlabel('y=E/T')
plt.ylabel('$f_s$')
plt.show()
But how can I plot this function with a varying sin function. I have tried applying what was used in the following link but I don't understand the code: How can I convert numbers to a color scale in matplotlib?
I am new to python and the code has barely any description in it.
I have also tried to apply the method shown in How do I plot a function with different values of a parameter on the same plot in gnuplot 4.4? but I keep on getting the same error message, "Invalid synthax pyplate E"
Upvotes: 0
Views: 289
Reputation: 1197
theta = [...] # your values of theta you'd like to plot
x = np.linspace(-1, 3, 100) # Or np.logspace, as you want. You'll have to change the parameters though, if you want to display properly
y = x / (np.exp(x)+1)
for t in theta :
plt.plot(x, np.sin(2*t)*y, label = 'theta = %s' % t)
plt.legend()
plt.show()
Result for theta = [1,2,3]
:
If you want to display a colormap, you can do this (code taken from here) :
import matplotlib
norm = matplotlib.colors.Normalize(vmin=np.min(theta), vmax=np.max(theta))
c_m = matplotlib.cm.cool
s_m = matplotlib.cm.ScalarMappable(cmap=c_m, norm=norm)
s_m.set_array([])
for t in theta :
plt.plot(x, np.sin(2*t)*y, color=s_m.to_rgba(t))
plt.colorbar(s_m)
plt.show()
Result :
Upvotes: 1