[英]Matplotlib — Irregular data intervals
How would I create a plot that has an x-axis that is to scale? 如何创建具有x轴可缩放的图? At the moment, the graph spaces 2, 4, 8, 16, 32, ... equally.
此刻,图平均间隔2、4、8、16、32...。
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 ...
). 注意:data.txt文件具有所有原始y值(示例:
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. 我平均所有列以获得y值。
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. 您需要向绘图函数提供x值和y值。
plt.plot(x,y, ...)
I assume that in this case you want 我假设在这种情况下
plt.plot(xvals,avgs, ...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.