【问题标题】:How do I plot an equation with the different values of a sin function?如何绘制具有不同 sin 函数值的方程?
【发布时间】:2020-08-03 09:28:39
【问题描述】:

我正在尝试以类似于链接中的图中所示的方式绘制函数:

此图所基于的等式是:y= sin^2θ x /(e^x +1)

我之前使用以下代码绘制了上面没有正弦函数的方程:

# 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()

但是我怎样才能用不同的 sin 函数来绘制这个函数。我已尝试应用以下链接中使用的内容,但我不明白代码:How can I convert numbers to a color scale in matplotlib?

我是 python 新手,代码中几乎没有任何描述。


我也尝试应用How do I plot a function with different values of a parameter on the same plot in gnuplot 4.4? 中显示的方法,但我不断收到相同的错误消息“无效的 synthax pyplate E”

【问题讨论】:

  • 你的函数的变量参数是什么?
  • sin的值,随着角度的变化,sin得到不同的值。
  • 我发布了一个答案,但已将其删除,因为我可能误解了这个问题。您似乎想在同一轴上绘制多条曲线?但是您引用的只是x (y= sin^2 x /(e^x +1)) 的函数-您的意思是使用x 和其他一些变量的函数吗,然后针对 x 绘制该其他变量的一系列值?
  • 我不明白:您的函数 y(x) 仅取决于 x 值(x 轴),您如何期望您的 sin 对于相同的 x 具有不同的值?
  • 对不起,我写错了方程式, sin^2 应该写成 sin^2θ 。而sin^2θ呈现几个值

标签: python matplotlib plot


【解决方案1】:
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()

theta = [1,2,3] 的结果:

如果你想显示一个颜色图,你可以这样做(代码取自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()

结果:

【讨论】:

  • 我尝试添加一个颜色条,颜色代表每一行,使用 c = np.random.random(8) #8 是我得到的行数和 fig.colorbar() 但是它说我只有 8 种颜色,100 分,但我想要不同 thetas 的值,而不是 x 的值。你能帮帮我吗?
猜你喜欢
  • 2021-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-20
  • 1970-01-01
  • 2021-05-07
  • 2020-05-11
相关资源
最近更新 更多