matplotlib-note

Matpoltlib简单绘图方法

####### import
import matplotlib.pyplot as plt
%matplotlib inline      jupyter notebook中直接直接将图片嵌入网页中
%matplotlib notebook    jupyter notebook中直接直接将结果嵌入网页中

##### 绘图区域及子区域
plt.figure(figsize=(8,7),dpi=98)  # 创建绘图组件的顶层容器
p1 = plt.subplot(211)   # 子图行数、列数、序号
p2 = plt.subplot(212)
p1.plot()  # p1上增加曲线

fig = plt.figure()
ax = fig.add_subplot(abc) # 添加子图

fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True) # 3个子图共用x轴

#####
plt.plot(t,v,"b--",linewidth=1,label="$sinc(x)$")      # 增加曲线
plt.plot(x, y, 'ro', x, y1, 'g--')  # 一次增加金条曲线
plt.xlabel(‘x’)           # x轴标签
plt.ylabel(‘y’)           # y轴标签
plt.title(‘…’)            # 图表标题

plt.set_yscale("log")  # 将y轴log显示

文本显示
plt.text(3,-1,r'mu=100,sigma=15')

添加注释
ax.annotate('x', xy=(1,2), textcoords='offsetpoints',xytext=(-50, 30), arrowprops=dict(arrowstyle="->"))

增加图例
plt.legend(loc='best', fancybox=True)

坐标范围
plt.xlim(0,6)  #  设置x轴刻度范围,从0~1000
plt.ylim(-2.5,2.5)   # 设置y轴刻度的范围,从0~20
plt.axis([0, 6, -2.5, 2.5])   #[xmin, xmax, ymin, ymax]

刻度线
plt.xticks(np.linspace(0,6,10,endpoint=True))  # 设置x轴刻度
plt.yticks(np.linspace(-2.5,2.5,10,endpoint=True))  # 设置y轴刻度

格线
plt.grid(True,linewidth=0.2,linestyle='--',alpha=0.5)

显示
plt.show()

#######  对坐标取对数
plt.semilogx()    横坐标
plt.semilogy()    纵坐标
plt.loglog()      横纵同时

##### 着色 将图形的不同部分用不同颜色进行着色
plt.fill_between(x_arr, y1_arr, y2_arr,
                   where=y1>y2, facecolor="green", interpolate=True, alpha=0.4)

####### 绘制直方图
plt.hist(a,b),a为长方形的左横坐标值,b为柱高

####### 绘制散点图
plt.scatter(x, y, c=x * 100, s=y * 100, marker="o", alpha=0.5)  # c 表示颜色 (=x*100, 即用不同颜色标示不同值)
                                          s 表示大小 (=y*100, 即用不同大小标示不同值)
                                          maker 是形状,可以是marker=(5,1) 5表示形状是5边型,1表示是星型(0表示多边形,2放射型,3圆形)

####### 三维绘图

from mpl_toolkits.mplot3d import Axes3D  #导入3d绘图库

三维面图
fig = plt.figure()
ax = fig.gca(projection=’3d’)   #创建一个三维的绘图工程

ax=plt.subplot(111,projection='3d') #创建一个三维的绘图工程

X = np.arange(-4, 4, 0.25)
Y = np.arange(-4, 4, 0.25)
X, Y = np.meshgrid(X, Y)     # 相当于是由XY组成的坐标面

Z = np.sin(np.sqrt(X**2 + Y**2)) # 对应的z轴值
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow') # z的数据的z.shape是(len(y), len(x))
                                                                 rstride 行数组坐标间隔,默认为10
                                                                 cstride 列数组坐标间隔,默认为10
plt.show()

三维散点图
ax.scatter(x,y,z,c='y') #绘制数据点

z方向的投影等高线图
ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.cm.hot)  

##### 极坐标绘图
theta=np.arange(0,2*np.pi,0.02) 
plt.subplot(122,polar=True)   # polar=True
plt.plot(theta,np.cos(5*theta),'--',lw=2) 
plt.plot(theta,2*np.cos(4*theta),lw=2)
plt.rgrids(np.arange(0.5,2,0.5),angle=45)  # 绘制半径为0.5 1.0 1.5的三个同心圆,同时将这些半径的值标记在45度位置的那个直径上
plt.thetagrids([0,45,90])  # 在theta为0,45,90度的位置上标记上度数
C:\Users\张豪>pip install matplotlib-3.7.2-cp310-cp310-win32.whl Processing c:\users\张豪\matplotlib-3.7.2-cp310-cp310-win32.whl Collecting contourpy>=1.0.1 (from matplotlib==3.7.2) Using cached contourpy-1.1.0.tar.gz (13.4 MB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... error error: subprocess-exited-with-error × Preparing metadata (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [12 lines of output] + meson setup C:\Users\张豪\AppData\Local\Temp\pip-install-9aonnup5\contourpy_cf94fc4320674b1ab97a527e0a04f957 C:\Users\张豪\AppData\Local\Temp\pip-install-9aonnup5\contourpy_cf94fc4320674b1ab97a527e0a04f957\.mesonpy-k9vixjo_\build -Dbuildtype=release -Db_ndebug=if-release -Db_vscrt=md --vsenv --native-file=C:\Users\张豪\AppData\Local\Temp\pip-install-9aonnup5\contourpy_cf94fc4320674b1ab97a527e0a04f957\.mesonpy-k9vixjo_\build\meson-python-native-file.ini The Meson build system Version: 1.2.0 Source dir: C:\Users\张豪\AppData\Local\Temp\pip-install-9aonnup5\contourpy_cf94fc4320674b1ab97a527e0a04f957 Build dir: C:\Users\张豪\AppData\Local\Temp\pip-install-9aonnup5\contourpy_cf94fc4320674b1ab97a527e0a04f957\.mesonpy-k9vixjo_\build Build type: native build Project name: contourpy Project version: 1.1.0 ..\..\meson.build:1:0: ERROR: Could not find C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe A full log can be found at C:\Users\张豪\AppData\Local\Temp\pip-install-9aonnup5\contourpy_cf94fc4320674b1ab97a527e0a04f957\.mesonpy-k9vixjo_\build\meson-logs\meson-log.txt [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details.
最新发布
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值