Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168753
Image: ubuntu2004
from pylab import * x,y = np.random.randn(2,100) fig = plt.figure() ax1 = fig.add_subplot(211) ax1.xcorr(x, y, usevlines=True, maxlags=50, normed=True, lw=2) ax1.grid(True) ax1.axhline(0, color='black', lw=2) ax2 = fig.add_subplot(212, sharex=ax1) ax2.acorr(x, usevlines=True, normed=True, maxlags=50, lw=2) ax2.grid(True) ax2.axhline(0, color='black', lw=2) savefig('1.png')
reset() import numpy as np import matplotlib.pyplot as plt plt.clf() N = 5 menMeans = (20, 35, 30, 35, 27) womenMeans = (25, 32, 34, 20, 25) menStd = (2, 3, 4, 1, 2) womenStd = (3, 5, 2, 3, 3) ind = np.arange(N) # the x locations for the groups width = 0.35 # the width of the bars: can also be len(x) sequence p1 = plt.bar(ind, menMeans, width, color='r', yerr=womenStd) p2 = plt.bar(ind, womenMeans, width, color='y', bottom=menMeans, yerr=menStd) plt.ylabel('Scores') plt.title('Scores by group and gender') plt.xticks(ind+width/2., ('G1', 'G2', 'G3', 'G4', 'G5') ) plt.yticks(np.arange(0,81,10)) plt.legend( (p1[0], p2[0]), ('Men', 'Women') ) plt.savefig('2.png')
reset() from pylab import * t = arange(0.1, 4, 0.1) s = exp(-t) e = 0.1*abs(randn(len(s))) f = 0.1*abs(randn(len(s))) g = 2*e h = 2*f figure() errorbar(t, s, e, fmt='o') # vertical symmetric figure() errorbar(t, s, None, f, fmt='o') # horizontal symmetric figure() errorbar(t, s, e, f, fmt='o') # both symmetric figure() errorbar(t, s, [e,g], [f,h], fmt='--o') # both asymmetric figure() errorbar(t, s, [e,g], f, fmt='o', ecolor='g') # both mixed figure() errorbar(t, s, e, [f,h], fmt='o') # both mixed figure() errorbar(t, s, [e,g], fmt='o') # vertical asymmetric figure() errorbar(t, s, yerr=e, fmt='o') # named figure() errorbar(t, s, xerr=f, fmt='o') # named xlabel('Distance (m)') ylabel('Height (m)') title('Mean and standard error as a function of distance') figure() ax = subplot(111) ax.set_yscale('log') errorbar(t, s+2, e, f, fmt='o') # both symmetric savefig('3.png')
reset() import numpy as np import matplotlib import matplotlib.cm as cm import matplotlib.pyplot as plt fig = plt.figure() Z = np.arange(10000.0) Z.shape = 100,100 Z[:,50:] = 1. im1 = plt.figimage(Z, xo=50, yo=0, cmap=cm.jet, origin='lower') im2 = plt.figimage(Z, xo=100, yo=100, alpha=.8, cmap=cm.jet, origin='lower') if 0: dpi = 72 plt.savefig('figimage_%d.png'%dpi, dpi=dpi, facecolor='gray') plt.savefig('figimage_%d.pdf'%dpi, dpi=dpi, facecolor='gray') plt.savefig('figimage_%d.svg'%dpi, dpi=dpi, facecolor='gray') plt.savefig('figimage_%d.eps'%dpi, dpi=dpi, facecolor='gray') plt.savefig('4.png')
reset() import numpy as np import matplotlib.pyplot as plt plt.clf() t = np.arange(0.0, 1.01, 0.01) s = np.sin(2*2*np.pi*t) plt.fill(t, s*np.exp(-5*t), 'r') plt.grid(True) plt.savefig('5.png')
reset() import matplotlib.mlab as mlab from matplotlib.pyplot import figure, show import numpy as np from matplotlib.pyplot import savefig, clf clf() x = np.arange(0.0, 2, 0.01) y1 = np.sin(2*np.pi*x) y2 = 1.2*np.sin(4*np.pi*x) fig = figure() ax1 = fig.add_subplot(311) ax2 = fig.add_subplot(312, sharex=ax1) ax3 = fig.add_subplot(313, sharex=ax1) ax1.fill_between(x, 0, y1) ax1.set_ylabel('between y1 and 0') ax2.fill_between(x, y1, 1) ax2.set_ylabel('between y1 and 1') ax3.fill_between(x, y1, y2) ax3.set_ylabel('between y1 and y2') ax3.set_xlabel('x') # now fill between y1 and y2 where a logical condition is met. Note # this is different than calling # fill_between(x[where], y1[where],y2[where] # because of edge effects over multiple contiguous regions. fig = figure() ax = fig.add_subplot(211) ax.plot(x, y1, x, y2, color='black') ax.fill_between(x, y1, y2, where=y2>=y1, facecolor='green') ax.fill_between(x, y1, y2, where=y2<=y1, facecolor='red') ax.set_title('fill between where') # Test support for masked arrays. y2 = np.ma.masked_greater(y2, 1.0) ax1 = fig.add_subplot(212, sharex=ax) ax1.plot(x, y1, x, y2, color='black') ax1.fill_between(x, y1, y2, where=y2>=y1, facecolor='green') ax1.fill_between(x, y1, y2, where=y2<=y1, facecolor='red') ax1.set_title('Now regions with y2>1 are masked') # This example illustrates a problem; because of the data # gridding, there are undesired unfilled triangles at the crossover # points. A brute-force solution would be to interpolate all # arrays to a very fine grid before plotting. # show how to use transforms to create axes spans where a certain condition is satisfied fig = figure() ax = fig.add_subplot(111) y = np.sin(4*np.pi*x) ax.plot(x, y, color='black') # use the data coordinates for the x-axis and the axes coordinates for the y-axis import matplotlib.transforms as mtransforms trans = mtransforms.blended_transform_factory(ax.transData, ax.transAxes) theta = 0.9 ax.axhline(theta, color='green', lw=2, alpha=0.5) ax.axhline(-theta, color='red', lw=2, alpha=0.5) ax.fill_between(x, 0, 1, where=y>theta, facecolor='green', alpha=0.5, transform=trans) ax.fill_between(x, 0, 1, where=y<-theta, facecolor='red', alpha=0.5, transform=trans) savefig('6.png')