Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Some Visualizations using CoCalc

Views: 1341

2D Graphics in Sage -- Matlab style

Sage also fully supports a library called matplotlib, which has an interface closely inspired by MATLAB™.

Many Pylab examples...

%auto import numpy as np import matplotlib.pyplot as plt
plt.plot([1,2,3], [4,5,7], ) plt.show()
[<matplotlib.lines.Line2D object at 0x7fb7319c9390>]
# We put the code from the example in a function, to avoid printing intermediate results. def example(): def f(t): return np.exp(-t) * np.cos(2*np.pi*t) t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02) plt.figure(1) plt.subplot(211) plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') plt.subplot(212) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.show() # Now draw the plot. example()

Copy and paste something below. You can highlight a block, then hit tab to indent the whole thing at once...

def example(): t = np.arange(0.0, 2.0, 0.01) s = np.sin(2*np.pi*t) plt.plot(t,s) plt.title(r'$\alpha_i > \beta_i$', fontsize=20) plt.text(1, -0.6, r'$\sum_{i=0}^\infty\,\, x_i$', fontsize=20) plt.text(0.6, 0.6, r'$\mathcal{A}\mathrm{sin}(2 \omega t)$', fontsize=20) plt.xlabel('time (s)') plt.ylabel('volts (mV)') plt.show() example()

You can also just directly use Octave, which is a MATLAB clone.

Use %plot -f svg to set octave to use nice looking SVG graphics.

%octave %plot -f svg -s 800,600 plot([1,2,3], [4,5,7]) set(gca, "fontsize", 16)
%octave x = 0:0.01:20; y = (1 + sqrt(x)) .* sin (x); plot (x, y); set(gca, "fontsize", 16); xlabel ("x"); ylabel ("(1 + sqrt(x)) * sin (x)"); title ("Simple 2-D Plot");
import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111, polar=True) r = np.arange(0,1,0.001) theta = 2 * 2*np.pi * r line, = ax.plot(theta, r, color='#ee8d18', lw=3) ind = 800 thisr, thistheta = r[ind], theta[ind] ax.plot([thistheta], [thisr], 'o') ax.annotate('a polar annotation', xy=(thistheta, thisr), # theta, radius xytext=(0.05, 0.05), # fraction, fraction textcoords='figure fraction', arrowprops=dict(facecolor='black', shrink=0.05), horizontalalignment='left', verticalalignment='bottom', ) plt.show()
[<matplotlib.lines.Line2D object at 0x7fb7318d1590>] <matplotlib.text.Annotation object at 0x7fb7318d1850>