简体   繁体   中英

Matplotlib — Irregular data intervals

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()

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, ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM