收集整理了一些常用的或者一些奇淫技巧
比如我要绘制
y = { cos ( x ) , x < 0 1 , 0 < x < π / 2 sin ( x ) , x ≥ π / 2 y = \begin{cases} \cos (x), & x<0 \\ 1, & 0
import numpy as np
import matplotlib.pyplot as plt
# function
x = np.linspace(-2*np.pi, 2*np.pi, 100)
interval0 = [1 if (i<0) else 0 for i in x]
interval1 = [1 if (i>=0 and i<=np.pi/2) else 0 for i in x]
interval2 = [1 if (i>np.pi/2) else 0 for i in x]
y = np.cos(x)* interval0 + 1 * interval1 + np.sin(x)*interval2
plt.plot(x,y)
plt.show()
用到的函数有:
fill_between(Fill between two sets of x-values.)
fill_betweenx(Fill between two sets of y-values.)
给一个例子
import matplotlib.pyplot as plt
plt.xlim(-1, 6)
plt.ylim(-5, 30)
plt.fill_betweenx([-5, 30], 2, 4, facecolor='silver', alpha=0.3)
plt.show()
用到的函数有:vlines hlines
import matplotlib.pyplot as plt
plt.xlim(-1, 6)
plt.ylim(-5, 30)
plt.vlines([2, 4], -5, 30)
plt.hlines([0, 15], -1, 6, 'silver', 'dashed')
plt.show()
因为我还不知道取什么名字,看图
import numpy as np
import matplotlib.pyplot as plt
n = 10
alpha = np.random.rand(n)
x = np.linspace(0, n - 0.001, 1000 * n)
y = np.array([alpha[int(i)] for i in x])
plt.xlim(0, n)
plt.ylim(0, 1.5)
plt.plot(x, y)
plt.show()
RUNOOB
莫烦python