import numpy as np import matplotlib.pyplot as plt from math import pi import math # HACKISH WARNING: this version internally uses 0 to 2 for the sine wave x axis, but labels it 0 to 2pi # This is so the two graphs can be the same size. I couldn't work out how to otherwise within pyplot. # (Actually only wanted the height of the two graphs to be the same, and was originally aiming to have the second x axis with the same scale as the first, which would have made it 3.14x longer.) def addLines(quad, color): incr = 1.0/1000.0 start = quad/2.0 end = start + 0.5 + incr x = np.arange(start, end, incr) lines = plt.plot(x, np.sin(pi*x), '') plt.setp(lines, color=color, linewidth=2.5, solid_capstyle='butt', solid_joinstyle='miter') def addPolar(quad, color): incr = pi/1000.0 start = quad*pi/2.0 end = start + pi/2.0 + incr x = np.arange(start, end, incr) lines = plt.plot(np.cos(x), np.sin(x), '') plt.setp(lines, color=color, linewidth=2.0, solid_capstyle='butt', solid_joinstyle='miter') def addTicks(): start = 0 # in pi end = 3 # in pi tickPoints = [] tickNames = [] done = [] # in units of pi #denoms = [1, 2, 4, 8] # show more denoms = [1, 2] for denom in denoms: for numer in range(denom * start, denom * end): val = (numer+0.0)/(denom+0.0) if val not in done: #tickPoints.append(val * pi) # this is preferred tickPoints.append(val) # dodgy hack to help make the graph square. done.append(val) pich = u'\u03C0' strTop = str(numer) + pich if numer == 0: strTop = "0" elif numer == 1: strTop = pich if denom == 1: tickNames.append(strTop) else: tickNames.append(strTop + "/" + str(denom)) plt.xticks(tickPoints, tickNames) def drawSine(): addLines(-1, 'yellow') addLines(0, 'brown') addLines(1, 'r') addLines(2, 'orange') addLines(3, 'yellow') addLines(4, 'brown') addTicks() slack = .1 plt.axis('scaled') plt.xlim(-slack, 2.0 + slack) plt.ylim(-1.1, 1.1) plt.grid(True) plt.xlabel('$x$') plt.ylabel('$y$') plt.title(r'$y = \sin x$') plt.text( .25, 0, 'I', family='serif', fontsize=26, ha='center', va='center') plt.text(.75, 0, 'II', family='serif', fontsize=26, ha='center', va='center') plt.text(1.25, 0, 'III', family='serif', fontsize=26, ha='center', va='center') plt.text( 1.75, 0, 'IV', family='serif', fontsize=26, ha='center', va='center') def drawUnitCircle(): addPolar(0, 'brown') addPolar(1, 'r') addPolar(2, 'orange') addPolar(3, 'yellow') edge = 1.1 plt.axis('scaled') plt.axis([-edge, edge, -edge, edge]) plt.grid(True) plt.xlabel('$x$') plt.ylabel('$y$') plt.title(r'$x^2 + y^2 = 1$') lo = .85 #label offset plt.text( lo, lo, 'I', family='serif', fontsize=26, ha='center', va='center') plt.text(-lo, lo, 'II', family='serif', fontsize=26, ha='center', va='center') plt.text(-lo, -lo, 'III', family='serif', fontsize=26, ha='center', va='center') plt.text( lo, -lo, 'IV', family='serif', fontsize=26, ha='center', va='center') def twoInOne(): plt.subplot(121) drawUnitCircle() plt.subplot(122) drawSine() twoInOne() plt.savefig("Sine_quads_01_Pengo.svg") plt.show()