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

pokxtpni  于 5个月前  发布在  其他
关注(0)|答案(2)|浏览(55)

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

import numpy as np
import matplotlib.pyplot as plt
import scipy as sp 
from scipy import stats 
import numpy.random as rnd
import scipy.stats as sts
M = 365
dt=1/M
r = 0.2
sigma = 0.15
sdt =sigma*np.sqrt(dt)
S0 = 100

def N011(VA1, VA2) : 
    N01=np.sqrt(-2.*np.log(1-VA1))*np.cos(2*np.pi*VA2) 
    return N01

SLIST = []
SLIST.append(S0)
for i in range(0,M-1):
    VA11 = rnd.random()
    VA22 = rnd.random()
    while ((VA11 == 0.) | (VA11 == 1.)):
        VA11 = rnd.random()
    while ((VA22 == 0.) | (VA22 == 1.)):
        VA22 = rnd.random()
    N01 =N011(VA11, VA22)
    SLIST.append(SLIST[i]*(1.+N01*sdt+r*dt))

tlist = np.linspace(0,365, num = 365)    

plt.figure(num=0,dpi = 120)
plt.plot(tlist, SLIST)
plt.show()

字符串


的数据

qeeaahzv

qeeaahzv1#

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

plt.figure(num=0,dpi = 120)
plt.plot(tlist, SLIST, color=(1, 0, 0))  # `color` is the line color
ax = plt.gca()
ax.set_facecolor((0, 1, 0))  # to set the background
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#

import numpy as np
import matplotlib.pyplot as plt
import scipy as sp 
from scipy import stats 
import numpy.random as rnd
import scipy.stats as sts
M = 365
dt=1/M
r = 0.2
sigma = 0.15
sdt =sigma*np.sqrt(dt)
S0 = 100

def N011(VA1, VA2) : 
    N01=np.sqrt(-2.*np.log(1-VA1))*np.cos(2*np.pi*VA2) 
    return N01

SLIST = []
SLIST.append(S0)
for i in range(0,M-1):
    VA11 = rnd.random()
    VA22 = rnd.random()
    while ((VA11 == 0.) | (VA11 == 1.)):
        VA11 = rnd.random()
    while ((VA22 == 0.) | (VA22 == 1.)):
        VA22 = rnd.random()
    N01 =N011(VA11, VA22)
    SLIST.append(SLIST[i]*(1.+N01*sdt+r*dt))

tlist = np.linspace(0,365, num = 365)    
plt.figure(num=0,dpi = 120)
plt.plot(tlist, SLIST, color=(1, 0, 0))  # `color` is the line color
plt.title("Simulation du cours d'un actif financier")
ax = plt.gca()
ax.set_facecolor((0, 1, 1))  # to set the background
ax.get_children()[0].set_color((1, 0, 0))
plt.xlabel("t", fontweight='bold')
plt.ylabel("St", fontweight='bold')
plt.show()

字符串
这样做:


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

相关问题