basic_plotting
  |   Source

Basic Plotting with Matplotlib

In [6]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline # allows notebook plotting
In [7]:
x = np.arange(0, 10)
print x
[0 1 2 3 4 5 6 7 8 9]
In [8]:
y = np.sin(x)
print y
[ 0.          0.84147098  0.90929743  0.14112001 -0.7568025  -0.95892427
 -0.2794155   0.6569866   0.98935825  0.41211849]
In [10]:
plt.plot(x,y)
plt.show()
In [11]:
z = np.cos(x)
print z
[ 1.          0.54030231 -0.41614684 -0.9899925  -0.65364362  0.28366219
  0.96017029  0.75390225 -0.14550003 -0.91113026]
In [12]:
plt.plot(x,y)
plt.plot(x,z)
plt.xlabel('range of numbers between 1 and 10')
plt.ylabel('functions of the numbers')
plt.title('some weird sine and cosine graph')
plt.legend(['sine','cosine'])
plt.show()
In [13]:
print x
print y
print z
[0 1 2 3 4 5 6 7 8 9]
[ 0.          0.84147098  0.90929743  0.14112001 -0.7568025  -0.95892427
 -0.2794155   0.6569866   0.98935825  0.41211849]
[ 1.          0.54030231 -0.41614684 -0.9899925  -0.65364362  0.28366219
  0.96017029  0.75390225 -0.14550003 -0.91113026]
In [25]:
plt.subplot(2,1,1) # 2 = number of rows, 1= number of column, 1 = number of plot
plt.plot(x,y)
plt.title('Sine')

plt.subplot(2,1,2)
plt.plot(x,z)
plt.title('Cosine')

plt.subplots_adjust(hspace=0.35) # for adjusting linear spaces

plt.show()

showing Images

The Thing is that I need an image to explain this subplot. So I would kill two birds with one stone.

In [22]:
from scipy.misc import imread
In [21]:
img_horizontal_subplot = imread('plot_subplot-horizontal_1.png')
plt.imshow(img_horizontal_subplot)
Out[21]:
<matplotlib.image.AxesImage at 0x7f06668db6d0>
In [23]:
img_vertical_subplot = imread('plot_subplot-vertical_1.png')
plt.imshow(img_vertical_subplot)
Out[23]:
<matplotlib.image.AxesImage at 0x7f0666ac2590>
In [24]:
img_grid_subplot = imread('plot_subplot-grid_1.png')
plt.imshow(img_grid_subplot)
Out[24]:
<matplotlib.image.AxesImage at 0x7f0667227550>

End for now

Comments powered by Disqus