Adding extra contour lines using matplotlib 2D contour plotting

前端 未结 1 802
遥遥无期
遥遥无期 2021-01-11 21:49

I am creating a two-dimensional contour plot with matplotlib. Using the documentation provided http://matplotlib.org/examples/pylab_examples/contour_demo.html, such a contou

相关标签:
1条回答
  • 2021-01-11 22:28

    To draw isolines at specified level values, set the levels parameter:

    levels = np.arange(-1.0,1.5,0.25)
    CS = plt.contour(X, Y, Z, levels=levels)
    

    import numpy as np
    import matplotlib.mlab as mlab
    import matplotlib.pyplot as plt
    
    delta = 0.025
    x = np.arange(-3.0, 3.0, delta)
    y = np.arange(-2.0, 2.0, delta)
    X, Y = np.meshgrid(x, y)
    Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
    Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
    # difference of Gaussians
    Z = 10.0 * (Z2 - Z1)
    
    plt.figure()
    levels = np.arange(-1.0,1.5,0.25)
    CS = plt.contour(X, Y, Z, levels=levels)
    plt.clabel(CS, inline=1, fontsize=10)
    plt.title('levels = {}'.format(levels.tolist()))
    plt.show()
    

    enter image description here

    The sixth figure here uses this method to draw isolines at levels = np.arange(-1.2, 1.6, 0.2).


    To zoom in, set the x limits and y limits of the desired region:

    plt.xlim(0, 3)
    plt.ylim(0, 2)
    

    and to draw, say, 24 automatically-chosen levels, use

    CS = plt.contour(X, Y, Z, 24)
    

    For example,

    import numpy as np
    import matplotlib.mlab as mlab
    import matplotlib.pyplot as plt
    
    delta = 0.025
    x = np.arange(-3.0, 3.0, delta)
    y = np.arange(-2.0, 2.0, delta)
    X, Y = np.meshgrid(x, y)
    Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
    Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
    # difference of Gaussians
    Z = 10.0 * (Z2 - Z1)
    
    plt.figure()
    N = 24
    CS = plt.contour(X, Y, Z, N)
    plt.clabel(CS, inline=1, fontsize=10)
    plt.title('{} levels'.format(N))
    plt.xlim(0, 3)
    plt.ylim(0, 2)
    plt.show()
    

    enter image description here

    The third figure here uses this method to draw 6 isolines.

    0 讨论(0)
提交回复
热议问题