matplotlib
一、坐标、网格、标题
1 import numpy as np 2 import pandas as pd 3 from sqlalchemy import create_engine 4 import matplotlib.pyplot as plt 5 6 x = np.linspace(0,2*np.pi,100) 7 y = np.sin(x) 8 plt.plot(x,y) 9 # 背景网格设置 10 plt.grid(linestyle='--',color="green",alpha=0.5) 11 # 横纵坐标范围 12 plt.xlim(0,10) 13 plt.ylim(-1,1.2) 14 # 图形整体大小 15 plt.figure(figsize=(10,12)) 16 # 颗粒度 17 plt.yticks([-1,0,1],["min",0,"max"]) 18 plt.xticks([0,np.pi/2,np.pi/3,3*np.pi/2,2*np.pi],[0, 19 r'$\frac{\pi}{2}$',r'$\frac{3\pi}{2}$', 20 r'$\frac{\pi}{3}$', r'$\frac{2\pi}{3}$' 21 ]) 22 # 坐标标签 23 plt.ylabel("y=sin(x)",rotation=0,fontsize = 10) 24 # title 25 plt.title(fontfamily="KaiTi",label="正玄波") 26 plt.show() 27 # 字体库 28 from matplotlib.font_manager import FontManager 29 fm = FontManager() 30 print([i.name for i in fm.ttflist])
二、图例
1 x = np.linspace(0,2*np.pi) # x轴 2 y = np.sin(x) # 正弦波 3 plt.plot(x,y) 4 plt.plot(x,np.cos(x)) # 余弦波 5 #图例 6 plt.legend(['Sin','Cos'], 7 fontsize = 10, 8 loc = 'center', 9 ncol = 2,#水平放置2个 10 bbox_to_anchor =[0,1.01,1,0.2]) #x,y,w,h 相对位置
三、脊柱
1 axes = plt.gca() 2 # 消失坐标 3 axes.spines['top'].set_color("white") 4 axes.spines['right'].set_color("white") 5 # 轴偏移 6 axes.spines['left'].set_position(("data",3)) 7 axes.spines['bottom'].set_position(("data",0))
四、图片保存
1 axes = plt.gca() # 轴面 2 # plt.figure(figsize=(9,12))# 边界宽 3 axes.set_facecolor("lightblue") # 内层颜色 4 plt.savefig( 5 './test.png', 6 dpi=100,# 默认100,值越高越清晰 7 facecolor = 'red', # 外层颜色 8 edgecolor = 'green', # 边界颜色(要想边界有颜色,需要设置边界宽) 9 # bbox_inches = 'tight'# 紧凑展示(用于图片展示不全) 10 11 )
五、线型
# marker:p 菱状 o 圆形 ls(线型) :,--,
1 plt.plot(x,y, 2 color='#7AAFE7', 3 ls=":", 4 marker='o', 5 markersize=5, 6 markerfacecolor="red", 7 markeredgewidth =2,# 边界宽 8 markeredgecolor ="yellow", # 边界颜色 9 alpha=0.7 10 )
六、多个图形位置
1 x = np.linspace(0,2*np.pi) # x轴 2 y = np.sin(x) # 正弦波 3 ax = plt.subplot(2,2,1) 4 ax.plot(x,y) 5 ax = plt.subplot(2,2,2) 6 ax.plot(x,y) 7 ax = plt.subplot(2,1,2) 8 ax.plot(x,y)
七、嵌套图
left、bottom 数值范围在0~1,0代表左下角,1代表右上角
1 fig = plt.figure(figsize=(9,6)) 2 x = np.linspace(0,2*np.pi) # x轴 3 y = np.sin(x) # 正弦波 4 plt.plot(x,y) 5 ax = plt.axes([0.6,0.6,0.2,0.2]) # 轴面【left bottom width height】 6 ax.plot(x,y,color="red") 7 8 # 方法二 9 ax2 = fig.add_axes([0.2,0.2,0.2,0.2]) 10 ax2.plot(x,y)
八、多图布局分格显示
均匀
1 x = np.linspace(0,2*np.pi) # x轴 2 y = np.sin(x) # 正弦波 3 fig, ((ax11,ax12,ax13), (ax21,ax22,ax23),(ax31,ax32,ax33)) = plt.subplots(3, 3) 4 # 也可通过plt.subplot() ⼀个个添加⼦视图 5 fig.set_figwidth(9) 6 fig.set_figheight(6) 7 ax11.plot(x,np.sin(x)) 8 ax12.plot(x,np.cos(x)) 9 ax13.plot(x,np.tanh(x)) 10 ax21.plot(x,np.tan(x)) 11 ax22.plot(x,np.cosh(x)) 12 ax23.plot(x,np.sinh(x)) 13 ax31.plot(x,np.sin(x) + np.cos(x)) 14 ax32.plot(x,np.sin(x*x) + np.cos(x*x)) 15 ax33.plot(x,np.sin(x)*np.cos(x)) 16 # 紧凑显示,边框会⽐较⼩,可以注释掉该⾏查看效果 17 plt.tight_layout()
不均匀
1 import matplotlib.gridspec as gridspec 2 3 x = np.linspace(0,2*np.pi) # x轴 4 y = np.sin(x) # 正弦波 5 gs = gridspec.GridSpec(3, 3) # 3行3列 6 7 ax1 = plt.subplot(gs[0,:]) # 0行,全列 8 ax1.plot(x,np.sin(10*x)) 9 10 ax2 = plt.subplot(gs[1,:2]) # 1行,0、1列 11 ax2.plot(x,np.sin(10*x)) 12 13 ax3 = plt.subplot(gs[1:,2]) 14 ax3.plot(x,np.sin(10*x)) 15 16 ax4 = plt.subplot(gs[2,0]) 17 ax4.plot(x,np.sin(10*x)) 18 19 ax5 = plt.subplot(gs[2,1]) 20 ax5.plot(x,np.sin(10*x))
九、双轴
1 x = np.linspace(0,2*np.pi) # x轴 2 y = np.sin(x) # 正弦波 3 4 ax1 = plt.subplot(111) # 0行,全列 5 ax1.plot(x,y,color="red") 6 plt.yticks(color="red") 7 plt.ylabel("Sin",color="red") 8 # 双轴,共享X轴,返回一个新的子视图;twiny(双X轴) 9 ax2 = ax1.twinx() 10 ax2.plot(x,y,color="blue") 11 plt.yticks(color="blue") 12 plt.ylabel("Cos",color="blue")
十、文本
1 ax1 = plt.subplot(111) 2 ax1.plot(x,y,color="red") 3 plt.text(x=0,y=-0.75,s="$exp(-x)*sin(2\pi*x)$",fontSize=12,color="yellow")
十一、箭头标注
1 x = np.linspace(0,2*np.pi) # x轴 2 y = np.sin(x) # 正弦波 3 4 ax1 = plt.subplot(111) 5 ax1.plot(x,y) 6 plt.ylim([-2,2]) 7 plt.annotate( 8 "max",# 文本内容 9 xy=(1.5,1),# 指向的坐标 10 xytext=(2,1.5),# 文本的坐标 11 arrowprops = { 12 "arrowstyle": "->" 13 # "width":2,# 线宽 14 # "headwidth":6,# 箭头宽 15 # "headlength":15,# 箭头长 16 # "shrink":0 ,# 缩放,
#被注释的这4行,不能与"arrowstyle": ->同用
17 } 18 )
十二、折线图
1 # 折线图 2 y = np.random.randint(0,10,5) 3 plt.plot(y,marker="*") 4 plt.plot(y.cumsum(),marker="o") 5 # 多图分布 6 fig,ax1 = plt.subplots(2,1) # 2行1列 7 ax1[0].plot(y,marker="o") 8 ax1[1].plot(y.cumsum(),marker="*")
十三、柱状图
1 y = np.random.randint(3,10,5) 2 y2 = np.random.randint(1,5,5) 3 x = np.array(["G1","G2","G3","G4","G5"]) 4 plt.bar(x,y,color="orange",width=0.5) 5 plt.bar(x,y2, 6 bottom=y,# y在下面 7 width=0.5, 8 yerr=0.5,# 误差线 9 ecolor="red", 10 capsize=5 # 误差线的上横和下横 11 ) 12 plt.legend(["Men","Women"])
带标签的柱状图
1 y = np.random.randint(3,10,5) 2 y2 = np.random.randint(1,5,5) 3 x = np.arange(5) 4 lables = np.array(["G1","G2","G3","G4","G5"]) 5 width = 0.4 6 bar1 = plt.bar(x-width/2,y,color="orange",width=width) 7 plt.bar(x+width/2,y2,width=width) 8 9 # 设置文本 10 for i,b in enumerate(bar1): 11 h = b.get_height() 12 w = b.get_width() 13 plt.text( 14 x=b.get_x()+w/2, 15 y=h+0.2, 16 s=y[i], 17 ha="center" # 水平垂直 18 ) 19 20 plt.legend(["Men","Women"])
pandas 直接画出条形图技巧
数据:
center.plot(kind='bar') plt.xticks(rotation=0) # 将X轴的文字垂直展示
十四、直方图
1 y = np.random.randn(1000) 2 # count 统计次数;bins范围 3 count,bins,fig = plt.hist(y,bins=10,color="red")
十五、箱式图
1 y = np.random.randn(1000,3) 2 x= list("ABC") 3 plt.boxplot(y, 4 notch=True,# 修饰箱式图 5 sym="ro", # color,marker 6 labels=x 7 )
十六、散点图
1 data = np.random.randn(100,2) 2 s = np.random.randint(100,300,size = 100) 3 color = np.random.randn(100) 4 plt.scatter( 5 data[:,0], # 横坐标 6 data[:,1], # 纵坐标 7 s = s, # 尺寸大小 8 c = color, # 颜色 9 alpha = 0.5) # 透明度
十七、饼图
1 data = np.random.randint(2,100,3) 2 lables=["苹果","香蕉","鸭梨"] 3 4 plt.pie( 5 data,labels=lables, 6 textprops={"family":"KaiTi","fontsize":18}, 7 autopct="%0.2f%%", 8 explode=[0,0,0.15], 9 shadow=True 10 )
1 plt.pie( 2 data, 3 autopct='%0.2f%%', 4 radius=1, 5 pctdistance=0.85,# 文本位置, 6 labels=lables, 7 textprops={"family":"KaiTi"}, 8 wedgeprops={ 9 'linewidth':5,# 间隔宽度 10 'width':0.3, # 饼图宽度 11 'edgecolor':'white' # 间隔颜色, 12 13 } 14 ) 15 16 plt.pie( 17 data, 18 autopct='%0.2f%%', 19 radius=0.7, 20 pctdistance=0.35,# 文本位置, 21 labels=lables, 22 textprops={"family":"KaiTi"}, 23 wedgeprops={ 24 'linewidth':5,# 间隔宽度 25 'width':0.7, # 饼图宽度 26 'edgecolor':'white' # 间隔颜色, 27 28 } 29 ) 30 plt.rcParams['font.family']="KaiTi" # 全局设置 31 plt.rcParams['font.size']=18 # 全局设置 32 plt.legend(lables,title="类别")
十八、热力图
1 data = np.random.randn(7,7)*5 2 plt.imshow(data, 3 cmap=plt.cm.RdBu_r,# 颜色 4 ) 5 # 文本 6 for i in range(7): 7 for j in range(7): 8 plt.text(x=j,y=i,s=round(data[i,j],1),ha="center")
十九、面积图
1 days = [1,2,3,4,5] 2 sleeping =[7,8,6,11,7] 3 eating = [2,3,4,3,2] 4 working =[7,8,7,2,2] 5 playing = [8,5,7,8,13] 6 plt.stackplot(days,sleeping,eating,working,playing) 7 plt.xlabel('x') 8 plt.ylabel('y') 9 plt.title('Stack Plot',fontsize = 18) 10 plt.legend(['Sleeping','Eating','Working','Playing'],fontsize = 18) 11 plt.show()
十九、蜘蛛图
1 labels=np.array(["个⼈能⼒","IQ","服务意识","团队精神","解决问题能⼒","持续学习"]) 2 stats=[83, 61, 95, 67, 76, 88] 3 # 画图数据准备,⻆度、状态值 4 angles=np.linspace(0, 2*np.pi, len(labels), endpoint=False) 5 # 首尾相连 6 stats=np.concatenate((stats,[stats[0]])) 7 angles=np.concatenate((angles,[angles[0]])) 8 9 fig = plt.figure(figsize=(9,9)) 10 ax = fig.add_subplot(111, polar=True )# polar极坐标图 11 ax.plot(angles, stats, 'o-', linewidth=2) # 连线 12 ax.fill(angles, stats, alpha=0.25) # 填充颜色 13 # 设置⻆度 14 ax.set_thetagrids(angles*180/np.pi,#⻆度值 15 labels, 16 fontsize = 18) 17 ax.set_rgrids([20,40,60,80],fontsize = 18)
二十、3D图
1 from mpl_toolkits.mplot3d.axes3d import Axes3D # 3D引擎 2 month = np.arange(1,5) 3 # 每个月4周 每周都会产生数据 4 # 三个维度:月、周、销量 5 fig = plt.figure(figsize=(9,6)) 6 ax3 = Axes3D(fig) 7 for m in month: 8 ax3.bar(np.arange(4), 9 np.random.randint(1,10,size = 4), 10 zs = m ,#偏移量 11 zdir = 'x',# 在哪个方向上,一排排,排列 12 alpha = 0.7,# alpha 透明度 13 width = 0.5) 14 ax3.set_xlabel('X',fontsize = 18,color = 'red') 15 ax3.set_xticks([1,2,3,4]) 16 ax3.set_xticklabels(["一季度","二季度","三季度","四季度"],family="KaiTi") 17 ax3.set_ylabel('Y',fontsize = 18,color = 'red') 18 ax3.set_zlabel('Z',fontsize = 18,color = 'green')
实战
1 import numpy as np 2 import pandas as pd 3 from sqlalchemy import create_engine 4 import matplotlib.pyplot as plt 5 import matplotlib.gridspec as gridspec 6 from mpl_toolkits.mplot3d.axes3d import Axes3D # 3D引擎 7 8 job = pd.read_csv(r"C:\Users\Administrator\Desktop\Pandas数据分析库\lagou2020.csv") 9 data = job.city.value_counts() 10 plt.rcParams['font.family'] = "KaiTi" 11 plt.barh(y=data.index[::-1],width=data[::-1]) #倒序排 12 plt.box(False) #没有边界 13 plt.grid(axis="x",color="blue") 14 plt.title(label="各城市数据分析岗位需求量", 15 fontsize=20, 16 backgroundcolor="#c5b783", 17 color="white", 18 pad=10, 19 weight="bold" 20 )
1 plt.figure(figsize=(12,9)) 2 city_salary = job.groupby("city")["salary"].mean().sort_values() # 分组聚合运算 3 plt.bar(x = city_salary.index,height = city_salary.values, 4 color = plt.cm.RdBu_r(np.linspace(0,1,len(city_salary))))
方法二:
plt.bar(np.arange(5),skill_count,
tick_label = ['Python/R','SQL','Tableau','Excel','SPSS/SAS'],
width = 0.5,
color = plt.cm.RdBu_r(skill_count/skill_count.max()))
5 plt.title(label=' 各城市的薪资⽔平对⽐ ', 6 fontsize=32, weight='bold', color='white', backgroundcolor='#3c7f99') 7 plt.tick_params(labelsize=16) 8 plt.grid(axis = 'y',linewidth = 0.5,color = 'black') 9 plt.yticks(ticks = np.arange(0,25,step = 5,),labels = 10 ['','5k','10k','15k','20k']) 11 plt.box(False) # 去掉边框
plt.tick_params(labelsize=18,rotation=60) #x轴字体大小和旋转角度
12 plt.savefig('./各城市薪资状况.png')
1 work_salary = job.pivot_table(index="city",columns="workYear",values="salary") 2 # 透视表 3 work_salary = work_salary[["应届毕业⽣","1-3年","3-5年","5-10年"]]\ 4 .sort_values(by = '5-10年',ascending = False) # 筛选⼀部分⼯作经验 5 data = work_salary.values 6 data = np.repeat(data,4,axis = 1) # 重复4次,⽬的画图,美观,图⽚宽度拉⼤ 7 plt.figure(figsize=(12,9)) 8 plt.imshow(data,cmap='RdBu_r') 9 plt.yticks(np.arange(13),work_salary.index) 10 plt.xticks(np.array([1.5,5.5,9.5,13.5]),work_salary.columns) 11 # 绘制⽂本 12 h,w = data.shape 13 for x in range(w): 14 for y in range(h): 15 if (x%4 == 0) and (~np.isnan(data[y,x])): # 4个写一个文本,非空 16 text = plt.text(x + 1.5, y, round(data[y,x],1), 17 ha="center", va="center", color='r',fontsize = 16) 18 plt.colorbar(shrink = 0.85) 19 plt.tick_params(labelsize = 16
1 education = job["education"].value_counts(normalize=True) 2 plt.figure(figsize=(9,9)) 3 _ = plt.pie(education,labels=education.index,autopct='%0.2f%%', 4 wedgeprops=dict(linewidth=3,width = 0.5),pctdistance=0.8, 5 textprops = dict(fontsize = 20)) 6 _ = plt.title(label=' 学历要求 ', 7 fontsize=32, weight='bold', 8 color='white', backgroundcolor='#c5b783')
1 job = pd.read_csv(r"C:\Users\Administrator\Desktop\Pandas数据分析库\lagou2020.csv") 2 cond1 = job["Python"].astype("boolean") 3 cond2 = job["Sql"].astype("boolean") 4 cond3 = job["Excel"].astype("boolean") 5 cond4 = job["SPSS/SAS"].astype("boolean") 6 d1 = job[cond1]["salary"] 7 d2 = job[cond2]["salary"] 8 d3 = job[cond3]["salary"] 9 d4 = job[cond4]["salary"] 10 11 plt.boxplot( 12 [d1,d2,d3,d4], 13 vert=False,# 垂直展示 14 labels=["Python","Sql","Excel","SPSS/SAS"] 15 ) 16 plt.grid(axis="x",color='k',alpha=0.3) 17 plt.xticks(np.arange(0,161,step=20), 18 [str(i)+"K" for i in np.arange(0,161,step=20)] 19 )
字符串的排序
1 job = pd.read_csv(r"C:\Users\Administrator\Desktop\Pandas数据分析库\lagou2020.csv") 2 # 将字符串转化为category 3 job['companySize'] = job['companySize'].astype('category') 4 data = ["500-2000人","50-150人","少于15人","15-50人","150-500人","2000人以上"] 5 job["companySize"].cat.reorder_categories(data,inplace=True) 6 job.sort_values(by="companySize",ascending=False,inplace=True)