matplotlib绘制等直线


# 利用contour(),contourf()描绘等值线
# contourf()带有填充效果
def fig2():
    y, x = np.ogrid[-2:2:200j, -3:3:300j]
    z = x * np.exp(-x ** 2 - y ** 2)
    extent = [np.min(x), np.max(x), np.min(y), np.max(y)]
    plt.figure(figsize=(10, 4))
    plt.subplot(121)
    # 使用extent指定x轴和y轴的数据范围
    # 10 表示将整个函数的范围等值分为10个区间
    cs = plt.contour(z, 10, extent=extent)
    plt.clabel(cs)
    plt.subplot(122)
    plt.contourf(x.reshape(-1), y.reshape(-1), z, 20)
    plt.show()

这里写图片描述

# 隐函数的等值线绘制
def fig3():
    y, x = np.ogrid[-1.5:1.5:200j, -1.5:1.5:200j]
    f = (x ** 2 + y ** 2) ** 4 - (x ** 2 - y ** 2) ** 2
    plt.figure(figsize=(9, 4))
    plt.subplot(121)
    extend = [np.min(x), np.max(x), np.min(y), np.max(y)]
    # level参数表示最终回执的等值线的数目
    # 可以单独设置等值线的colors,linestyles,linewidths等参数
    cs = plt.contour(f, extend=extend, levels=[0, 0.1], colors=['b', 'r'], linestyles=['solid', 'dashed'],
                     linewidths=[2, 2])
    plt.subplot(122)
    # cs.collections表示等值线的列表每条等值线用LineCollection对象表示
    for c in cs.collections:
        data = c.get_paths()[0].vertices
        # c:LineCollection对象是一组曲线的集合,它可以表示蓝色实现那样有许多条线构成的等值线,它的get_paths()获得构成等值线的所有路径
        plt.plot(data[:, 0], data[:, 1], color=c.get_color()[0], linewidth=c.get_linewidth()[0])
    plt.show()

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值