How to make matplotlib graphs look professionally done like this?

前端 未结 4 1511
失恋的感觉
失恋的感觉 2021-01-30 23:56

Default matplotlib graphs look really unattractive and even unprofessional. I tried out couple of packages include seaborn as well as prettyplotlib but both of these just barely

4条回答
  •  野的像风
    2021-01-31 00:48

    matplotlib is almost infinitely flexible so you can do almost anything with it and if it doesn't exist you can write it yourself! Obviously the defaults are bland, this is because everyone has there own idea of what is "nice" so it is pointless to impose a predefined style.

    Here is a really simple example that addresses 4 of your points.

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.ticker import MultipleLocator, FormatStrFormatter
    
    x = np.linspace(-10, 10, 1000)
    y = 1+np.sinc(x)
    
    ax = plt.subplot(111)
    ax.plot(x, y, lw=2)
    ax.fill_between(x, 0, y, alpha=0.2)
    ax.grid()
    
    majorLocator   = MultipleLocator(1)
    ax.xaxis.set_major_locator(majorLocator)
    
    plt.show()
    

    enter image description here

    If your want to set defaults so all your plots look the same then you should generate a custom matplotlibrc file. A useful guide is here. To view a list of all the available options just call print plt.rcParams from an interactive terminal.

    Some of the other features such as filling will need to be done on a per plot basis. You can standardise this across your work by creating a function which adds the fill between given some input such as the axis instance and data.

提交回复
热议问题