柱状图
import matplotlib.pyplot as plt
name_list =["Monday","Tuesday","Friday","Sunday"]
num_list = [1.5,0.6,7.8,6]
num_list2=[1,2,3,1]
x = list(range(len(num_list)))
total_width = 0.8
n=2
width = total_width/n
plt.bar(x, num_list, width = width, label = "boys")
for i in range(len(x)) :
x[i] = x[i]+width
plt.bar(x, num_list2, width = width, label = "girls", tick_label = name_list)
plt.legend(loc="best")
plt.show()
饼状图
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
labels = 'A','B','C','D'
sizes = [10,20,30,40]
explode = (0,0.2,0,0)
color = ['r','g','b','c']
#plt.pie(sizes, labels = labels,explode = explode,colors = color, autopct = '%1.1f')
plt.pie(sizes, labels = labels,explode = explode,colors = color, autopct = '%1.2f%%')
plt.title('简单饼状图')
plt.legend(loc ="upper right",fontsize = 8, borderaxespad = 0.3)
plt.show()
雷达图
import numpy as np
import matplotlib.pyplot as plt
theta = np.array([0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 0.25]) # theta定义了一个np.array数组存储多个数据
r = [75,60,50,70,50,85,45,70,75]
# 闭合(两组数据相同)
plt.polar(theta*np.pi,r,'ro-',lw = 2)
plt.fill(theta*np.pi,r,facecolor='r',alpha=0.5) #填充
plt.ylim(0,100)
plt.show()
雷达图实例
# 1.导入扩展库;
2.准备数据;
3.等分圆,曲线闭合(最后一个数据与第一个重合; (重点)
4.绘图;
5.设置标签;
6.填充雷达图。
import matplotlib.pyplot as plt
import numpy as np
courses = ['C++','python','高等数学','大学英语','软件工程','组成原理','操作系统','网络工程']
scorse = [82, 95, 72, 85, 45, 58, 65, 86]
datalength = len(scorse)
angles = np.linspace(0, 2*np.pi, datalength, endpoint=False)
scorse.append(scorse[0])
angles = np.append(angles,angles[0])
plt.polar(angles, scorse, 'rv--', lw = 2)
plt.thetagrids(angles*180/np.pi, courses, fontproperties = 'simhei')
plt.fill(angles,scorse,facecolor = 'r',alpha = 0.4)
plt.show()
1.linspace(x, y)产生一个有100个元素的行向量,其中的元素在区间[x, y]中等间隔分布。
2.linspace(x, y, n)产生x和y之间等间隔的n个数,如果n = 1,返回结果为y。
3D折线图
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d')
#测试数据
theta = np.linspace(-4 * np.pi, 4*np.pi, 100)
z = np.linspace(-4, 4, 100)*0.3
r = z**4 + 1
x = r*np.sin(theta)
y = r*np.cos(theta)
ax.plot(x,y,z,'b^-',label = '3D picture Test')
mpl.rcParams['legend.fontsize'] = 10
ax.legend()
plt.show()
3D条形图
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl(可去)
import mpl_toolkits.mplot3d
x = np.random.randint(0,40,10)
y = np.random.randint(0,40,10)
z = 80*abs(np.sin(x+y))
ax = plt.subplot(projection ='3d')
for xx,yy,zz in zip(x,y,z):
color = np.random.random(3)
ax.bar3d(xx,yy,0, dx =1,dy=1,dz=zz,color = color)
ax.set_xlabel('x')
ax.set_xlabel('y')
ax.set_xlabel('z')
plt.show()