file = open('testdata.txt', 'w')
for i in range(len(x)):
# make a string for each line you want to write # ’\t’ means ’tab’ # ’\n’ means ’newline’ # ’str()’ means you are converting the quantity in brackets to a string type
txt = str(x[i]) + ’\t’ + str(y[i]) + ’ \n’ file.write(txt) file.close()
data = np.random.normal(5.0, 3.0, 1000) # make an array of 1000 random number with a mean of 5 and rms of 3.
pl.hist(data) # user define bin: bins = np.arange(-5., 16., 1.) #floating number ranges
pl.hist(data, bins, histtype=’stepfilled’)
pl.title(’Plot of y vs. x’)# give plot a title
pl.xlabel(’x axis’)# make axis labels
pl.ylabel(’y axis’)
pl.xlim(0.0, 7.0)# set axis limits
pl.ylim(0.0, 30.)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.semilogx(x,y) #x轴为对数坐标轴
ax.semilogy(x,y) #y轴为对数坐标轴
ax.loglog(x,y) #双对数坐标轴
plot1 = pl.plot(x1, y1, ’r’)# use pylab to plot x and y : Give your plots names
plot2 = pl.plot(x2, y2, ’go’)
pl.legend([plot1, plot2], (’red line’, ’green circles’), ’best’, numpoints=1)
# the 3rd parameter is positioning of the legend, which takes one of the following values:
'best’, ‘upper right’, ‘upper left’, ‘center’, ‘lower left’, ‘lower right’
Alternatively,
pl.plot(x1, y1, 'r', label='red line')
pl.plot(x2, y2, 'go', label='green circles')
pl.legend()
pl.subplot(numRows, numCols, plotNum) # the last number specify which subplot is made as the current subplot
# (subplot index sequence: top to bottom, left to right).
# when all three numbers are <10, pl.subplot(3,2,2) is equivalent to pl.subplot(322) f1 = pl.figure(1)
pl.subplot(221)
pl.subplot(222)
pl.subplot(212) # The last command remove the original two subplots in the second row and creat a new single subplot according to the new 2*1 format. # As an example, if we want to make a plot in the second (upper right) subplot: pl.subplot(222) # make this subplot current. Alternatively, if we have defined ax2=pl.subplot(222), we can use pl.sca(ax2) here. pl.plot(x,y) # fine tune the positions of subplots (when the default layout is not satisfactory): pl.subplots_adjust(left=0.08, bottom=0.95, wspace=0.25, hspace=0.45) # in units of the width/height of the plot region (also 'right', 'top').
pl.savefig(fname, dpi=None, facecolor='w', edgecolor='w', orientation='portrait', papertype=None, format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None)