matplotlib 画图 -1

import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt
#%matplotlib online #如果不显示图,执行此代码
plt.rcParams["font.sans-serif"]="SimHei" #显示中文
plt.rcParams["axes.unicode_minus"] = False #让图片显示符号

#样式和颜色
#样式 "-" "--" "-." ":" "." "," o < > s + x D d 1 2 3 4 h H p | _
#颜色 b(蓝色),g(绿色),r(红色),c(青色),m(品红),y(黄色),k(黑色),w(白色)

# 画图
#c/color:颜色 
#ls :line style 线的样式
x = np.linspace(-5,5,10)
y = x**2
plt.plot(x,y,c = "red",ls = ":")

 

#figsize :画布大小,长和宽
#dpi:分辨率,像素密度 值越大,图越大
#facecolor:图背景颜色
plt.figure(figsize = (6,4),dpi = 70,facecolor = "#11aa11")
x = np.linspace(0,2*np.pi)
y = np.sin(x)
plt.plot(x,y)

1.在一个画布上画多个图

# 在一个画布上画多个图
plt.figure(figsize = (5,3))
x = np.linspace(0,8)
y = np.sin(x)
y1 = np.cos(x)
y2 = -np.sin(x)
plt.plot(x,y)
plt.plot(x,y1)
plt.plot(x,y2)
plt.show()#显示图片 放在哪个位置,立马显示之前的代码的图

2.多图布局

2.1 subplot()

#matplot 多图布局
#第一种方式subplot()
fig =plt.figure(figsize = (7,4))
x = np.linspace(-np.pi,np.pi,30)
y = np.sin(x)
#第一个图
ax1 = plt.subplot(221)#221:2行2列第一个
ax1.plot(x,y)
ax1.set_title("子图1")# 设置小图标题

#第二个图
ax2 = plt.subplot(222)#221:2行2列第一个
ax2.plot(x,y)
ax2.set_title("子图2")# 设置小图标题

#第三个图
ax3 = plt.subplot(223)#221:2行2列第一个
ax3.plot(x,y)
ax3.set_title("子图3")# 设置小图标题

#第四个图
ax4 = plt.subplot(224)#221:2行2列第一个
ax4.plot(x,y)
ax4.set_title("子图4")# 设置小图标题

#自动调节画布布局
fig.tight_layout()

2.2 subplots()

#第一种方式subplots() 注意第一个subplot 没有S
x = np.linspace(0,2*np.pi)
#3行3列
fig,ax = plt.subplots(3,3)
ax1,ax2,ax3 =ax
ax11,ax12,ax13 = ax1
ax21,ax22,ax23 = ax2
ax31,ax32,ax33 = ax3
#fig设置画布大小
fig.set_figwidth(8) #宽度
fig.set_figheight(5) #高度

#第一行
ax11.plot(x,np.sin(x))
ax21.plot(x,np.cos(x))
ax31.plot(x,np.tan(x))

#第二行
ax21.plot(x,np.tanh(x))
ax22.plot(x,np.cosh(x))
ax31.plot(x,np.sinh(x))

#第三行
ax31.plot(x,np.tan(x))
ax32.plot(x,np.tan(x)+2)
ax33.plot(x,-np.tan(x))

plt.tight_layout()# 调整布局

3 嵌套图

3.1 add_subplot()

#嵌套图 add_subplot()
fig = plt.figure(figsize = (8,5))

#子图1
axes1 = fig.add_subplot(1,1,1)# 一行一列第一个
axes1.plot([0,1],[1,3])

#子图2 嵌套图
axes2 = fig.add_subplot(2,2,1,facecolor = "pink") # 在axes1的画布上分成2行2列四份 第一个图
axes2.plot([1,3])

3.2 axes add_axes

#axes 和add_axes
fig = plt.figure(figsize = (8,5))

#图1
x = np.linspace(0,2*np.pi,30)
y = np.sin(x)
plt.plot(x,y)

#嵌套图1
#[left,bottom, width,height]
axes1 = plt.axes([0.5,0.6,0.3,0.4])
axes1.plot(x,y,color = "g")

#嵌套图2
axes2 = fig.add_axes([0.18,0.18,0.3,0.3])
axes2.plot(x,y,c = "k")

4 双轴显示 twinx()

#双轴显示
plt.figure(figsize = (6,5))

x = np.linspace(0,10,100)

#图1
axes1 = plt.gca()#得到当前轴域
axes1.plot(x,np.exp(x),c ="g")
axes1.set_xlabel("time")
axes1.set_ylabel("exp",c= "g")
axes1.tick_params(axis = "y",labelcolor = "g")
#图2
axes2 = axes1.twinx()#和图1共享x轴
axes2.plot(x,np.sin(x),c = "c")
axes2.set_ylabel("sin",c = "c")
axes2.tick_params(axis = "y",labelcolor = "c")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值