%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
线型图:展现变量的趋势变化
x = np.linspace(0.05,10,1000)
y = np.cos(x)
plt.plot(x,y,ls='-',lw=5,c='g',label='曲线标签')
plt.xlabel('x轴标签')
plt.ylabel('y轴标签')
plt.grid(linestyle=':',color='y')
plt.axhline(y=0.0,c='r',ls='-',lw=2)
plt.axvline(x=4.0,c='r',ls='-',lw=2)
plt.text(2.0,0.0,'y=cos(x)',weight="bold",color='b')
plt.title('示例曲线图')
plt.legend(loc='lower left')
plt.show()
散点图:寻找变量之间的关系
x = np.linspace(0.05,10,100)
y = np.random.rand(100)
plt.scatter(x,y,label="散点标签")
plt.legend()
plt.xlim(0.05,10)
plt.ylim(0,1)
plt.show()
柱状图:定性数据的分布特征
x = [1,2,3,4,5,6,7,8]
y = [1,4,6,3,7,4,8,6]
plt.bar(x,y,align='center',color='c',tick_label=['a','b','c','d','e','f','g','h'],hatch='/')
plt.xlabel('箱子编号')
plt.ylabel('箱子重量')
plt.show()
条形图:定性数的分布特征
x = [1,2,3,4,5,6,7,8]
y = [1,4,6,3,7,4,8,6]
plt.barh(x,y,align='center',color='c',tick_label=['a','b','c','d','e','f','g','h'],hatch='/')
plt.xlabel('箱子编号')
plt.ylabel('箱子重量')
plt.show()
直方图:定量数据的分布特征
boxWeight = np.random.randint(0,10,100)
x = boxWeight
bins = range(0,11,1)
plt.hist(x,bins=bins,color='g',histtype='bar',rwidth=1,alpha=0.6)
plt.xlabel('箱子编号')
plt.ylabel('箱子重量')
plt.show()
饼图:定性数据的不同类别的百分比
kinds = ['a','b','c','d']
colors = ['y','r','g','b']
soldNums = [0.05,0.45,0.15,0.35]
plt.pie(soldNums,labels=kinds,autopct="3.1f%%",startangle=60,colors=colors)
plt.title("饼图")
plt.show()
气泡图:用二维数据借助气泡的小展示三维2数据
import matplotlib as mpl
a = np.random.randn(100)
b = np.random.randn(100)
plt.scatter(a,b,s=np.power(10*a+20*b,2),c=np.random.rand(100),cmap=mpl.cm.RdYlBu,marker='o')
plt.show()
箱型图:查看数据分布情况
import matplotlib as mpl
x = np.random.randn(1000)
plt.boxplot(x)
plt.xticks([1],["x轴"])
plt.yticks([1],['y轴'])
plt.title('箱型图')
plt.grid(axis='y',ls=':',lw=1,color='gray',alpha=0.1)
plt.show()
import matplotlib as mpl
x = np.random.randn(1000)
plt.boxplot(x,vert=False,showfliers=True)
plt.xticks([1],["y轴"])
plt.yticks([1],['x轴'])
plt.title('箱型图')
plt.grid(axis='y',ls=':',lw=1,color='gray',alpha=0.1)
plt.show()