柱状图
柱状图可以直观的反应不同类别数据之间分布情况的数量差异
import matplotlib.pyplot as plt import numpy as np fig,axes = plt.subplots() data_m=(40,120,20,100,30,200) data_f=(60,180,30,150,20,50) index = np.arange(6) width = 0.4 axes.bar(index,data_m,width,color='c',label='men') axes.bar(index+width,data_f,width,color='b',label='women') axes.set_xticks(index+width/2) axes.set_xticklabels(('Taxi','Metro','Walk','Bus','Bicycle','Driving')) axes.legend() plt.show()
# 柱状图叠加效果,将两个柱状图叠加显示 fig,axes = plt.subplots() data_m=(40,120,20,100,30,200) data_f=(60,180,30,150,20,50) width = 0.4 axes.bar(index,data_m,width,color='c',label='men') axes.bar(index,data_f,width,color='b',bottom=data_m,label='women') axes.set_xticks(index+width/2) axes.set_xticks(index+width/2) axes.set_xticklabels(('Taxi','Metro','Walk','Bus','Bicycle','Driving')) axes.legend() plt.show()
# 柱状图半重叠 fig,axes = plt.subplots() data_m=(40,120,20,100,30,200) data_f=(60,180,30,150,20,50) width = 0.4 axes.bar(index,data_m,width,color='c',label='men',align='center') axes.bar(index,data_f,width,color='b',label='women',align='edge') axes.set_xticks(index+width/2) axes.set_xticks(index+width/2) axes.set_xticklabels(('Taxi','Metro','Walk','Bus','Bicycle','Driving')) axes.legend() plt.show()
# 水平柱状图 fig,axes = plt.subplots() data_m=(40,120,20,100,30,200) data_f=(60,180,30,150,20,50) width = 0.4 axes.barh(index,data_m,width,color='c',label='men',align='center',alpha=0.4) axes.barh(index,data_f,width,color='b',label='women',align='edge',alpha=0.4) axes.set_yticks(index+width/2) axes.set_yticklabels(('Taxi','Metro','Walk','Bus','Bicycle','Driving')) axes.legend() plt.show()
折线图
折线图可以看出数据的变化趋势
import matplotlib.pyplot as plt import numpy as np fig,axes = plt.subplots() x = np.arange(10) y1 = np.random.rand(10) y2 = np.random.rand(10) axes.plot(x,y1,'-o',color='c') axes.plot(x,y2,'--o',color='b') plt.show()
表格
通过表和图的结合,既可以直观的看到数据的分布情况,也能看到详细的数据
import matplotlib.pyplot as plt import numpy as np fig,axes = plt.subplots() data_m=(40,120,20,100,30,200) data_f=(60,180,30,150,20,50) width = 0.4 index = np.arange(6) axes.bar(index,data_m,width,color='c',label='men') axes.bar(index,data_f,width,color='b',bottom=data_m,label='women') axes.set_xticks([]) axes.legend() # 表格 data=(data_m,data_f) rows =('male','female') columns = 'Taxi','Metro','Walk','Bus','Bicycle','Driving' axes.table(cellText=data,rowLabels=rows,colLabels=columns) plt.show()
不同坐标系下的图象
import matplotlib.pyplot as plt import numpy as np # 双扭线 fig,axes = plt.subplots() theta_list = np.arange(0,2*np.pi,0.01) r = [2*np.cos(2*theta) for theta in theta_list] # polar 极坐标系 # 建立一个投影为极坐标的axes axes = plt.subplot(projection='polar') # 使用plot函数生成函数曲线, axes.plot(theta_list,r) # 为了美观删除r轴上所有的刻度 axes.set_rticks([]) plt.show()
matplotlib3D
import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() axes = Axes3D(fig) # 设置随机种子 np.random.seed(100) x = np.random.rand(60) y = np.random.rand(60) z = np.random.rand(60) axes.scatter(x,y,x) plt.show()
# 使用pyplot实现 fig = plt.figure() axes = plt.subplot(projection="3d") x = np.random.rand(80) y = np.random.rand(80) z = np.random.rand(80) axes.scatter(x,y,x) plt.show()