This cover the basics of plotting charts and controlling the style and content. We will be using matplotlib library.
Finding Help:
NumPyBase N-dimensional array package |
SciPyFundamental library for scientific computing |
MatplotlibComprehensive 2D Plotting |
|||
IPythonEnhanced Interactive Console |
SymPySymbolic mathematics |
PandasData structures & analysis |
Most of the functionality we need to plot charts is in matlotlib.pyplot. Always import it as plt
In [1]:
import matplotlib.pyplot as plt
import numpy as np
In [2]:
x = [0,1,2]
y = [0,1,4]
In [3]:
fig = plt.figure()
axes = fig.add_subplot(111)
axes.plot(x, y)
plt.show()
In [4]:
x_highres = np.linspace(0, 2, 20)
y_highres = x_highres ** 2
In [5]:
fig = plt.figure()
axes = fig.add_subplot(111)
axes.plot(x, y)
axes.plot(x_highres, y_highres)
plt.show()
In [6]:
fig = plt.figure()
axes = fig.add_subplot(111)
axes.plot(x_highres, y_highres, "r--")
#axes.plot(x_highres, y_highres, color="red", linestyle='dashed')
plt.show()
In [7]:
fig = plt.figure()
axes = fig.add_subplot(111)
axes.plot(x_highres, y_highres, color="red", linestyle='dashed', linewidth=3)
plt.show()
In [8]:
fig = plt.figure()
axes = fig.add_subplot(111)
axes.plot(x_highres, y_highres,color="red", linestyle='dashed', linewidth=3, marker='o',
markerfacecolor='blue', markersize=5)
plt.show()
================ ===============================
character description
================ ===============================
``'-'`` solid line style
``'--'`` dashed line style
``'-.'`` dash-dot line style
``':'`` dotted line style
``'.'`` point marker
``','`` pixel marker
``'o'`` circle marker
``'v'`` triangle_down marker
``'^'`` triangle_up marker
``'<'`` triangle_left marker
``'>'`` triangle_right marker
``'1'`` tri_down marker
``'2'`` tri_up marker
``'3'`` tri_left marker
``'4'`` tri_right marker
``'s'`` square marker
``'p'`` pentagon marker
``'*'`` star marker
``'h'`` hexagon1 marker
``'H'`` hexagon2 marker
``'+'`` plus marker
``'x'`` x marker
``'D'`` diamond marker
``'d'`` thin_diamond marker
``'|'`` vline marker
``'_'`` hline marker
================ ===============================
========== ========
character color
========== ========
'b' blue
'g' green
'r' red
'c' cyan
'm' magenta
'y' yellow
'k' black
'w' white
========== ========
In addition, you can specify colors in many weird and
wonderful ways, including full names (``'green'``), hex
strings (``'#008000'``), RGB or RGBA tuples (``(0,1,0,1)``) or
grayscale intensities as a string (``'0.8'``). Of these, the
string specifications can be used in place of a ``fmt`` group,
but the tuple forms can be used only as ``kwargs``.
In [9]:
fig = plt.figure()
axes = fig.add_subplot(111)
axes.plot(x_highres, y_highres,color="red", linestyle='dashed', linewidth=3, marker='o',
markerfacecolor='blue', markersize=5)
axes.set_title('$y=x^2$') ## Notice you can you LaTeX Code
## for more about LaTeX check the Tutorial about Markdown and LaTeX
axes.grid()
plt.show()
In [10]:
fig = plt.figure()
axes = fig.add_subplot(111)
axes.plot(x_highres, y_highres,color="red", linestyle='dashed', linewidth=3, marker='o',
markerfacecolor='blue', markersize=5)
axes.set_title('$y=x^2$')
axes.grid()
axes.set_xlabel('x')
axes.set_ylabel('y')
plt.show()
In [11]:
fig = plt.figure(figsize=(12,8))
axes = fig.add_subplot(111)
axes.plot(x_highres, y_highres,color="red", linestyle='dashed', linewidth=3, marker='o',
markerfacecolor='blue', markersize=5)
axes.set_title('$y=x^2$')
axes.grid()
axes.set_xlabel('x')
axes.set_ylabel('y')
plt.show()
In [12]:
noise = np.random.random((128,128))
noise
Out[12]:
In [13]:
plt.imshow(noise)
plt.show()
In [14]:
plt.imshow(noise)
plt.colorbar()
plt.show()
In [15]:
plt.imshow(noise, cmap=plt.cm.gray)
plt.colorbar()
plt.show()
In [16]:
plt.imshow(noise, cmap=plt.cm.Paired)
plt.colorbar()
plt.show()