Matplotlib -- Irregular data intervals

340

You need to provide the xvalues as well as the yvalues to the plotting function.

plt.plot(x,y, ...)

I assume that in this case you want

plt.plot(xvals,avgs, ...)
Share:
340

Related videos on Youtube

quil
Author by

quil

Updated on December 02, 2022

Comments

  • quil
    quil over 1 year

    How would I create a plot that has an x-axis that is to scale? At the moment, the graph spaces 2, 4, 8, 16, 32, ... equally.

    Note: The data.txt file has all the raw y-values (sample: 0.7690 0.7618 0.7762 0.7747 0.7783 0.7747 0.7152 0.6722 0.5151\n ...). I averaged all the columns to get the y values.

    import matplotlib.pyplot as plt
    
    file = open("data.txt", "r")
    accs = file.readlines()
    accs = [x.strip() for x in accs]
    
    the_list = []
    for i in range(len(accs[0].split())):
        the_list.append([])
    
    for i in range(len(accs)):
        for k in range( len(accs[i].split() ) ):
            the_list[k].append( float(accs[i].split()[k] ))
    
    avgs = []
    for j in range(len(the_list)):
        avgs.append( sum(the_list[j]) / len(the_list[j]) )
    
    x_vals = [2, 4, 8, 16, 32, 64, 128, 256, 512]
    y_vals = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]
    
    plt.plot(avgs, "-b", label="batch size")
    plt.xticks(range(len(avgs)), x_vals)
    #plt.yticks(y_vals)
    plt.ylabel('percentage')
    plt.xlabel('batch size')
    plt.legend(loc='lower right')
    
    
    plt.show()
    
    file.close()
    
    • Ƭᴇcʜιᴇ007
      Ƭᴇcʜιᴇ007 almost 12 years
      How are we supposed to know why? We're not Microsoft. ;) Are you sure this wasn't already a plug-in in the past? I ask, as I don't think any version of WMP ever did this inherently.
    • slhck
      slhck almost 12 years
      If all you want to know is how to get a similar feature, please change your question to primarily ask for that.
  • Admin
    Admin over 10 years