二、
plt.gca #获取当前的坐标轴spines['right'].set_color('red’) #右边框为红色# 分别把x轴与y轴的刻度设置为bottom与leftxaxis.set_ticks_position('bottom')yaxis.set_ticks_position('left’)# 分别v把bottom和left类型设置为data,交点为(0,0)spines['bottom'].set_position(('data',0))spines['left'].set_position(('data',0))例如:ax = plt.gca()ax.spines['right'].set_color(‘red')ax.spines['top'].set_color(‘red’)三、
l1, = plt.plot(x,y1,color='red',linewidth=1.0,linestyle='—') #设置两条线为l1,l2 注:应该在后面加上,l2, = plt.plot(x,y2,color="blue",linewidth=5.0,linestyle="-")plt.legend(handles=[l1,l2],labels=['test1','test2'],loc='best’) #将l1,l2绘制于一张图中,其中名字分别是l1,l2,位置自动取在最佳位置 设置备注 x0 = 0.5y0 = 2*x0 + 1# 画点plt.scatter(x0,y0,s=50,color='blue')# 画虚线plt.plot([x0,x0],[y0,0],'k--',lw=2)#[x0,x0],[y0,0]代表x0,y0点作虚线交于x0,0 k--代表颜色的虚线,lw代表宽度plt.annotate(r'$2x+1=%s$' % y0,xy=(x0,y0),xytext=(+30,-30),textcoords='offset points',fontsize=16,arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=.2'))#xy=(x0,y0)指在x0,y0点,xytext=(+30,-30)指在点向右移动30,向下移动30,textcoords='offset points'指以点为起点#arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=.2')指弧度曲线, .2指弧度plt.text(-2,2,r'$This is the text$',fontsize=16,color='red’) #-2,2指从-2,2开始写 散点图 x = np.random.normal(0,1,500)y = np.random.normal(0,1,500)plt.scatter(x,y,s=50,color='blue',alpha=0.5) #s指点大小,alpha指透明度plt.show() 柱状图 x = np.arange(10)y = 2**x + 10plt.bar(x,y,facecolor='#9999ff',edgecolor='white')#柱颜色,柱边框颜色for x,y in zip(x,y):#zip指把x,y结合为一个整体,一次可以读取一个x和一个y plt.text(x,y,'%.2f' % y,ha='center',va='bottom')#指字体在中间和柱最顶的顶部plt.show() 等高图 def f(x,y): #用来生成高度 return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2)x = np.linspace(-3,3,100)y = np.linspace(-3,3,100)X,Y = np.meshgrid(x,y)#将x,y指传入网格中plt.contourf(X,Y,f(X,Y),8,alpha=0.75,cmap=plt.cm.hot)#8指图中的8+1根线,绘制等温线,其中cmap指颜色C = plt.contour(X,Y,f(X,Y),8,colors='black',linewidth=.5)#colors指等高线颜色plt.clabel(C,inline=True,fontsize=10)#inline=True指字体在等高线中plt.xticks(())plt.yticks(())plt.show() matplotlib绘制3D图 from mpl_toolkits.mplot3d import Axes3D#动态图所需要的包fig = plt.figure()ax = Axes3D(fig)x = np.arange(-4,4,0.25)#0.25指-4至4间隔为0.25y = np.arange(-4,4,0.25)X,Y = np.meshgrid(x,y)#x,y放入网格R = np.sqrt(X**2 + Y**2)Z = np.sin(R)ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'))#rstride=1指x方向和y方向的色块大小ax.contourf(X,Y,Z,zdir='z',offset=-2,cmap='rainbow')#zdir指映射到z方向,-2代表映射到了z=-2ax.set_zlim(-2,-2)plt.show() 子图像 plt.figure()plt.subplot(2,2,1)#建立一个两行两列的画布,第一个plt.plot([0,1],[0,1])plt.subplot(2,2,2)#第二个plt.plot([0,1],[0,1])plt.subplot(2,2,3)#第三个plt.plot([0,1],[0,1])plt.subplot(2,2,4)#第四个plt.plot([0,1],[0,1])plt.show() plt.figure()plt.subplot(2,1,1)#建立一个两行两列的画布,第一个plt.plot([0,1],[0,1])plt.subplot(2,3,4)#第二个plt.plot([0,1],[0,1])plt.subplot(2,3,5)#第三个plt.plot([0,1],[0,1])plt.subplot(2,3,6)#第四个plt.plot([0,1],[0,1])plt.show() 动态图 from matplotlib import animation#动态图所需要的包fig,ax = plt.subplots()#子图像x = np.arange(0,2*np.pi,0.01)line, = ax.plot(x,np.sin(x))def animate(i): line.set_ydata(np.sin(x+i/10))#用来改变的y对应的值 return line,def init(): line.set_ydata(np.sin(x))#动态图初始图像 return line,ani = animation.FuncAnimation(fig=fig,func=animate,init_func=init,interval=20)#动态作图的方法,func动态图函数,init_func初始化函数,interval指图像改变的时间间隔plt.show()注:若想看动态效果请在ipython中使用
使用颜色映射颜色映射(colormap)是一系列颜色,它们从起始颜色渐变到结束颜色。在可视化中,颜色映射用于突出数据的规律,例如,你可能用较浅的颜色来显示较小的值,并使用较深的颜色来显示较大的值。
import matplotlib.pyplot as plt x_values = list(range(1001)) y_values = [x**2 for x in x_values] plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues, edgecolor='none', s=40)这些代码将y值较小的点显示为浅蓝色,并将y值较大的点显示为深蓝色。
Matplotlib进阶-Seaborn
Seaborn其实是在matplotlib的基础上进行了更高级的API封装,从而使得作图更加容易,在大多数情况下使用seaborn就能做出很具有吸引力的图。
安装方式安装方式类似于matplotlib , 在Windows下和Linux下面都可以采用pip安装方式。
set_style( )set_style( )是用来设置主题的,Seaborn有五个预设好的主题: darkgrid , whitegrid , dark , white ,和 ticks 默认: darkgrid
import matplotlib.pyplot as plt import seaborn as sns sns.set_style("whitegrid") plt.plot(range(10)) plt.show() 直方图直方图的绘制:
import matplotlib.pyplot as plt import seaborn as snsimport pandas as pddf_iris = pd.read_csv(r'D:Windows 7 DocumentsDesktopiris.csv') sns.distplot(df_iris['petal_length'], kde = True) # kde 密度曲线 rug 边际毛毯 plt.show() 箱型图箱形图(Box-plot)又称为盒须图、盒式图或箱线图,是一种用作显示一组数据分散情况资料的统计图。因形状如箱子而得名。
import matplotlib.pyplot as plt import seaborn as snsimport pandas as pddf_iris = pd.read_csv(r'D:Windows 7 DocumentsDesktopiris.csv') sns.boxplot(x = df_iris['species'], y = df_iris['sepal_width']) plt.show() 联合分布两个变量的画图
import matplotlib.pyplot as plt import seaborn as snsimport pandas as pddf_iris = pd.read_csv(r'D:Windows 7 DocumentsDesktopiris.csv') sns.jointplot(df_iris['petal_width'], df_iris['sepal_width']) plt.show()不用圆点表示的话也是可以的,可以用其他方式来表示,比如六角形来表示:
import matplotlib.pyplot as plt import seaborn as snsimport pandas as pddf_iris = pd.read_csv(r'D:Windows 7 DocumentsDesktopiris.csv') sns.jointplot(df_iris['petal_width'], df_iris['sepal_width'], kind='hex') plt.show() 热力图相关系数是最早由统计学家pgyc设计的统计指标,是研究变量之间线性相关程度的量,一般用字母 r 表示。由于研究对象的不同,相关系数有多种定义方式,较为常用的是皮尔逊相关系数。
相关系数是用以反映变量之间相关关系密切程度的统计指标。相关系数是按积差方法计算,同样以两变量与各自平均值的离差为基础,通过两个离差相乘来反映两变量之间相关程度;着重研究线性的单相关系数。公式:
以上公式中:
关注数据框中各个特征之间的相关关系,呈现图形的展示,给人以直观的感受。而不是"冰冷"的数字。可以非常方便的找到各个特征之间呈现什么样的关系。比如线性,离散等关系。
import pandas as pdimport matplotlib.pyplot as plt import seaborn as sns data = pd.read_csv(r"D:Windows 7 DocumentsDesktopiris.csv") sns.set(style="ticks") # 使用默认配色 sns.pairplot(data,hue="species") # hue 选择分类列 plt.show()