Adding caption below X-axis for a scatter plot using matplotlib

前端 未结 5 1164
灰色年华
灰色年华 2021-02-07 03:17

I am pretty new to python and to the matplotlib library. I have created a scatter plot using matplotlib and now I wish to add caption a little below the X-axis. This is my code:

5条回答
  •  耶瑟儿~
    2021-02-07 04:05

    Something like:

    from matplotlib import pyplot as plt
    import numpy as np
    
    txt="I need the caption to be present a little below X-axis"
    
    # make some synthetic data
    x = np.linspace(0, 1, 512)
    y = np.random.rand(512)*2.3 + .1
    
    fig = plt.figure()
    ax1 = fig.add_axes((0.1, 0.2, 0.8, 0.7))
    
    ax1.set_title("This is my title")
    ax1.set_xlabel('X-axis')
    ax1.set_ylabel('Y-axis')
    
    # make the edge colors match the facecolors
    ax1.scatter(x,y, c='r', edgecolors='face')
    # center text
    fig.text(.5, .05, txt, ha='center')
    
    # use OO interface    
    ax1.set_xlim([0, 1.05])
    ax1.set_ylim([0, 2.5])
    
    # resize the figure to match the aspect ratio of the Axes    
    fig.set_size_inches(7, 8, forward=True)
    
    plt.show()
    

    might work. Making this easier to do is on the radar for mpl upstream, but we are still looking for someone to do it.

提交回复
热议问题