#!/usr/bin/env python # coding: utf-8 # # 1. matplotlib简介 # 在数据分析中,数据的可视化是非常重要的一部分,将数据直观的显示不仅方便于理解,更有助于后续的数据清洗等一系列操作。 # # matplotlib是一个用于图表的桌面绘图包,本文将对matplotlib的基本操作进行介绍。 # # [matplotlib官网](https://matplotlib.org/) # # [matplotlib绘图样例](https://matplotlib.org/gallery/index.html),样例是我们学习matplotlib最好的资源 # In[2]: #python中引入matplotlib的惯例 import matplotlib.pyplot as plt # In[3]: #绘制简单的直线 import numpy as np data = np.arange(10) plt.plot(data) # # 1.1 图片与子图 # matplotlib所绘制的图位于图片(Figure)对象中,在图片中添加一个或多个子图进行绘画。 # In[8]: import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_subplot(2,2,1)#最多可以放四个子图的,且当前子图位于第一个 ax2 = fig.add_subplot(2,2,2)#当前子图位于第二个 ax3 = fig.add_subplot(2,2,3) plt.plot([1.5,3.5,-2,1.6])#直接使用的话会在最后一个子图上进行绘制 # In[9]: import matplotlib.pyplot as plt fig = plt.figure() ax1 = fig.add_subplot(2,2,1) ax2 = fig.add_subplot(2,2,2) ax3 = fig.add_subplot(2,2,3) plt.plot([1.5,3.5,-2,1.6]) plt.subplots_adjust(wspace=0, hspace=0)#使用subplots_adjust调整子图之间的距离,wspace与hspace分别代表宽度和高度的百分比 # # 1.2 颜色,标记和线的类型 # matplotlib的主函数plot可以接收指定的参数来绘制不同类型的图形 # In[3]: import matplotlib.pyplot as plt import numpy as np data = np.arange(10) plt.plot(data, linestyle='--', color='r')#可以通过十六进制颜色代码的方式指定任何颜色 # In[7]: plt.plot(np.random.randn(30).cumsum(), color='#FFB6C1', linestyle='--', marker='o') #marker可以将凸显实际的数据点 # # 1.3 刻度,标签和图例 # 在matplotlib中我们也可以为图片添加必要的属性以方便理解 # In[18]: fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.plot(np.random.randn(1000).cumsum()) # In[24]: #改变x轴刻度 fig = plt.figure() ax = fig.add_subplot(1,1,1) ticks = ax.set_xticks([0,250,500,750,1000]) labels = ax.set_xticklabels(['one', 'two', 'three', 'four', 'five'], rotation=30, fontsize='small')#设置x坐标的刻度,rotation可以设置刻度的旋转角度 ax.set_title("My title")#为图表设置标题 ax.set_xlabel("x-label")#为x轴设置标记 ax.plot(np.random.randn(1000).cumsum()) # In[38]: #添加图例 fig = plt.figure() ax = fig.add_subplot(1,1,1) t1 = np.arange(0.0, 2.0, 0.01) s1 = 1 + np.sin(2 * np.pi * t1) t2 = np.arange(0.0, 2.0, 0.01) s2 = 2 + np.sin(2 * np.pi * t2) t3 = np.arange(0.0, 2.0, 0.01) s3 = 3 + np.sin(2 * np.pi * t3) ax.plot(t1,s1,'r',label='one') ax.plot(t2,s2,'g--',label='two') ax.plot(t3,s3,'b*',label='three') ax.legend(loc='best')#best会在图片的合适位置自动添加图例 # # 1.4 将图片保存到文件 # In[ ]: plt.savefig('figpath.svg')#参数为文件要保存的路径 # # 2. 使用pandas和seaborn绘图 # In[26]: import matplotlib.pyplot as plt from matplotlib.collections import PatchCollection from matplotlib.patches import Circle, Wedge, Polygon, Ellipse color = r'#39CF18' fig, ax = plt.subplots(figsize=[8,8]) plt.xticks([]) plt.yticks([]) ax.set_fc(color) plt.ylim([0,40]) plt.xlim([0,40]) es1 = Ellipse([15,24], width=21, height=18, facecolor='white', zorder=1) es2 = Ellipse([26,16], width=18, height=15, facecolor='white',linewidth=5,edgecolor=color,zorder=1) c1 = Circle([11,27], radius=1.3, facecolor=color, zorder=2) c2 = Circle([19,27], radius=1.3, facecolor=color, zorder=2) c3 = Circle([23,18], radius=1, facecolor=color, zorder=2) c4 = Circle([29,18], radius=1, facecolor=color, zorder=2) ax.add_artist(es1) ax.add_artist(es2) ax.add_artist(c1) ax.add_artist(c2) ax.add_artist(c3) ax.add_artist(c4) # patches.extend([c1,c2,c3,c4]) # In[19]: circle1 = plt.Circle((0, 0), 0.2, color='r') circle2 = plt.Circle((0.5, 0.5), 0.2, color='blue') circle3 = plt.Circle((1, 1), 0.2, color='g', clip_on=False) fig, ax = plt.subplots() # note we must use plt.subplots, not plt.subplot # (or if you have an existing figure) # fig = plt.gcf() # ax = fig.gca() ax.add_artist(circle1) ax.add_artist(circle2) ax.add_artist(circle3) # In[ ]: