6.1 Matplotlib基础绘图类型

6.1.1 plot(x,y)

import matplotlib.pyplot as plt
import numpy as np

#make data
x=np.linspace(-np.pi,np.pi,1000)
y=2*np.sin(x)*np.cos(x)

#plot
fig=plt.figure()
ax=plt.subplot()
ax.plot(x,y,linewidth=2.0)
ax.set(xlim=(-4,4),xticks=np.arange(-4,4),
ylim=(-1,1),yticks=np.arange(-2,2,0.25))

plt.show()

在这里插入图片描述

6.1.2 scatter(x,y)

import matplotlib.pyplot as plt
import numpy as np

#make data
np.random.seed(3)
x=4+np.random.normal(0,2,24)
y=4+np.random.normal(0,2,len(x))

#size and color
sizes=np.random.uniform(15,80,len(x))
#np.random.uniform(a,b,N)返回N个[a,b]之间的浮点数,b可以包括也可以不包括,具体取决于b在不在其中
colors=np.random.uniform(15,80,len(x))

#plot
fig=plt.figure()
ax=plt.subplot()

ax.scatter(x,y,s=sizes,c=colors,vmin=0,vmax=100)
ax.set(xlim=(0,8),xticks=np.arange(1,8),
ylim=(0,8),yticks=np.arange(1,8))
plt.show()

在这里插入图片描述

6.1.3 bar(x,y)

import matplotlib.pyplot as plt
import numpy as np

#make data
np.random.seed(3)
x=0.5+np.arange(8)
y=np.random.uniform(2,7,len(x))

#plot
fig=plt.figure()
ax=plt.subplot()

ax.bar(x,y,width=1,edgecolor="white",linewidth=0.7)

ax.set(xlim=(0,8),xticks=np.arange(1,8),
ylim=(0,8),yticks=np.arange(1,8))
plt.show()

在这里插入图片描述

6.1.4 stem(x,y)

import matplotlib.pyplot as plt
import numpy as np

#make data
np.random.seed(3)
x=0.5+np.arange(8)
y=np.random.uniform(2,7,len(x))

#plot
fig=plt.figure()
ax=plt.subplot()

ax.stem(x,y)
plt.show()

在这里插入图片描述

6.1.5 step(x,y)

import matplotlib.pyplot as plt
import numpy as np

#make data
np.random.seed(3)
x=0.5+np.arange(8)
y=np.random.uniform(2,7,len(x))

#plot
fig=plt.figure()
ax=plt.subplot()

ax.step(x,y,linewidth=2.5)
plt.show()

在这里插入图片描述

6.1.6 fill_between(x,y1,y2)

import matplotlib.pyplot as plt
import numpy as np

#make data
np.random.seed(1)
x=np.linspace(0,8,16)
y1=3+4*x/8+np.random.uniform(0.0,0.5,len(x))
y2=1+2*x/8+np.random.uniform(0.0,0.5,len(x))

#plot
fig=plt.figure()
ax=plt.subplot()

ax.fill_between(x,y1,y2,alpha=.5,linewidth=0)
ax.plot(x,(y1+y2)/2,linewidth=2)

plt.show()

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41857385/article/details/126184837
6.1