scipy 如何将Matplotlib图表背景设置为彩色绿色,将线条设置为彩色红色?

pokxtpni  于 2024-01-09  发布在  其他
关注(0)|答案(2)|浏览(219)

如何将Matplotlib图表背景设置为彩色绿色,将线条设置为彩色红色?

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import scipy as sp
  4. from scipy import stats
  5. import numpy.random as rnd
  6. import scipy.stats as sts
  7. M = 365
  8. dt=1/M
  9. r = 0.2
  10. sigma = 0.15
  11. sdt =sigma*np.sqrt(dt)
  12. S0 = 100
  13. def N011(VA1, VA2) :
  14. N01=np.sqrt(-2.*np.log(1-VA1))*np.cos(2*np.pi*VA2)
  15. return N01
  16. SLIST = []
  17. SLIST.append(S0)
  18. for i in range(0,M-1):
  19. VA11 = rnd.random()
  20. VA22 = rnd.random()
  21. while ((VA11 == 0.) | (VA11 == 1.)):
  22. VA11 = rnd.random()
  23. while ((VA22 == 0.) | (VA22 == 1.)):
  24. VA22 = rnd.random()
  25. N01 =N011(VA11, VA22)
  26. SLIST.append(SLIST[i]*(1.+N01*sdt+r*dt))
  27. tlist = np.linspace(0,365, num = 365)
  28. plt.figure(num=0,dpi = 120)
  29. plt.plot(tlist, SLIST)
  30. plt.show()

字符串


的数据

qeeaahzv

qeeaahzv1#

您可以通过调整打印生成代码来设置背景和线条颜色,如下所示:

  1. plt.figure(num=0,dpi = 120)
  2. plt.plot(tlist, SLIST, color=(1, 0, 0)) # `color` is the line color
  3. ax = plt.gca()
  4. ax.set_facecolor((0, 1, 0)) # to set the background
  5. plt.show()

字符串
参数是你想要的颜色的(red, green, blue)分量。如果你愿意,你可以从specifying colors的其他方式中选择。
我在this stack overflow post中找到了背景颜色问题的答案,在matplotlib.pyplot.plot documentation中可以找到有关更改线条颜色的信息。(您也可以通过从ax中检索适当的线条艺术家并使用其set_color方法(例如ax.get_children()[0].set_color((1, 0, 0)))来设置线条颜色,但这更复杂。)

nxagd54h

nxagd54h2#

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import scipy as sp
  4. from scipy import stats
  5. import numpy.random as rnd
  6. import scipy.stats as sts
  7. M = 365
  8. dt=1/M
  9. r = 0.2
  10. sigma = 0.15
  11. sdt =sigma*np.sqrt(dt)
  12. S0 = 100
  13. def N011(VA1, VA2) :
  14. N01=np.sqrt(-2.*np.log(1-VA1))*np.cos(2*np.pi*VA2)
  15. return N01
  16. SLIST = []
  17. SLIST.append(S0)
  18. for i in range(0,M-1):
  19. VA11 = rnd.random()
  20. VA22 = rnd.random()
  21. while ((VA11 == 0.) | (VA11 == 1.)):
  22. VA11 = rnd.random()
  23. while ((VA22 == 0.) | (VA22 == 1.)):
  24. VA22 = rnd.random()
  25. N01 =N011(VA11, VA22)
  26. SLIST.append(SLIST[i]*(1.+N01*sdt+r*dt))
  27. tlist = np.linspace(0,365, num = 365)
  28. plt.figure(num=0,dpi = 120)
  29. plt.plot(tlist, SLIST, color=(1, 0, 0)) # `color` is the line color
  30. plt.title("Simulation du cours d'un actif financier")
  31. ax = plt.gca()
  32. ax.set_facecolor((0, 1, 1)) # to set the background
  33. ax.get_children()[0].set_color((1, 0, 0))
  34. plt.xlabel("t", fontweight='bold')
  35. plt.ylabel("St", fontweight='bold')
  36. plt.show()

字符串
这样做:


的数据
一个修复,使颜色彩色绿色和红色彭博风格作为一个完整的.py文件,自我编译将不胜感激。

展开查看全部

相关问题