Hide contour linestroke on pyplot.contourf to get only fills

前端 未结 4 517
北恋
北恋 2020-12-05 00:45

I have a pet project to create images of maps, where I draw the roads and other stuff over a contour plot of the terrain elevation. It is intended to plan mountain bike rout

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 01:20

    I finally found a proper solution to this long-standing problem (currently in Matplotlib 3), which does not require multiple calls to contour or rasterizing the figure.

    Note that the problem illustrated in the question appears only in saved publication-quality figures formats like PDF, not in lower-quality raster files like PNG.

    My solution was inspired by this answer, related to a similar problem with the colorbar. A similar solution turns out to solve the contour plot as well, as follows:

    import numpy as np
    import matplotlib.pyplot as plt
    
    np.random.seed(123)
    x, y = np.random.uniform(size=(100, 2)).T
    z = np.exp(-x**2 - y**2)
    levels = np.linspace(0, 1, 100)
    
    cnt = plt.tricontourf(x, y, z, levels=levels, cmap="ocean")
    
    # This is the fix for the white lines between contour levels
    for c in cnt.collections:
        c.set_edgecolor("face")
    
    plt.savefig("test.pdf")    
    

    Here below is an example of contours before the fix

    And here below is the same figure after the above fix

提交回复
热议问题