Adding labels to a matplotlib graph

17,295

You need annotate, e.g.:

plt.annotate('some text',xy=(days[0],impressions[0]))

To adjust the x axis text you could add:

fig=plt.figure() # below the import statements
...
fig.autofmt_xdate() # after plotting

To change the legend text use the label parameter in your plot function:

plt.plot_date(x=days, y=impressions, fmt="r-",label="response times")

To increase the legend font size do this:

plt.legend(fontsize='x-large')
Share:
17,295
sarbo
Author by

sarbo

Updated on June 08, 2022

Comments

  • sarbo
    sarbo almost 2 years

    Having the following code:

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.dates as mdates
    
    days, impressions = np.loadtxt('results_history.csv', unpack=True, delimiter=',',usecols=(0,1) ,
            converters={ 0: mdates.strpdate2num('%d-%m-%y')})
    
    plt.plot_date(x=days, y=impressions, fmt="r-")
    plt.title("Load Testing Results")
    
    
    #params = {'legend.labelsize': 500,
        #'legend.handletextpad': 1,
        #'legend.handlelength': 2,
        #'legend.loc': 'upper left',
        #'labelspacing':0.25,
        #'legend.linewidth': 50}
    #plt.rcParams.update(params)
    
    plt.legend("response times")
    
    plt.ylabel("Date")
    plt.grid(True)
    plt.show()
    

    The graph is generated but i can't figure how can i add some xy labels. The generated graph: enter image description here

    Also tried to increase the legend text size but the text is not displayed. And the labels from the X axis are overlapped. CSV file:

    01-05-14, 55494, Build 1
    10-05-14, 55000, Build 2
    15-05-14, 55500, Build 3
    20-05-14, 57482, Build 4
    25-05-14, 58741, Build 5
    

    How can i add the xytext from the csv and also change the format for legend and X axis?