import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
x = np.linspace(0,2*np.pi,100)
y1,y2 = np.sin(x),np.cos(x)
plt.title(“sin&cos”)
plt.xlabel(“x”)
plt.ylabel(“y”)
plt.plot(x,y1)
plt.plot(x,y2)
plt.show()
name_list = [“A”,“B”,“C”,“D”]
num_list = [1.5,0.6,7.8,6]
plt.bar(range(len(num_list)),num_list,tick_label=name_list,color=“rgb”)
plt.show()
堆叠柱状图
name_list = [“A”,“B”,“C”,“D”]
num_list = [1.5,0.6,7.8,6]
num_list1 = [1,2,3,4]
plt.bar(range(len(name_list)),num_list,label=“boy”,fc=“y”)
plt.bar(range(len(name_list)),num_list1,label=“girl”,fc=“g”)
plt.legend()
plt.show()
并列柱状图
name_list = [r"AAAA",r"BBBB",r"CCCC",r"DDDD"]
num_list = [1.5,0.6,7.8,6]
num_list1 = [1,2,3,4]
num_list2 = [0,0,0,0]
x = list(range(len(name_list)))
totle_width,n = 0.8,2
width = totle_width/n
plt.bar(x,num_list,width=width,label=“boy”,fc=“y”)
for i in range(len(x)):
x[i] = x[i]+width
plt.bar(x,num_list1,width=width,label=“girl”,fc=“r”)
x = list(range(len(name_list)))
for i in range(len(x)):
x[i] = x[i] + 0.2
plt.bar(x,num_list2,width=width,label=“girl”,fc=“r”,tick_label=name_list)
plt.legend()
plt.show()
饼状图
labels = [“A”,“B”,“C”,“D”]
faces = [15,30.55,44.44,10]
plt.axes(aspect=1)
explode = [0,0.1,0.2,0]
plt.pie(x=faces,labels=labels,autopct="%3.1f %%",shadow=True,startangle=90,explode=explode)
plt.show()
实时画图
ax = []
ay = []
plt.ion()
for i in range(100):
ax.append(i)
ay.append(i**2)
plt.clf()#清除
plt.plot(ax,ay)
plt.pause(1)
plt.ioff()
3D绘图
x = np.random.normal(0,1,100)
y = np.random.normal(0,1,100)
z = np.random.normal(0,1,100)
fig = plt.figure()
print(type(fig),fig)
ax = Axes3D(fig)
ax.scatter(x,y,z)
plt.show()