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
import matplotlib.pyplot as plt
import numpy as np
x = [0,1,2]
y = [0,1,4]
fig = plt.figure()
axes = fig.add_subplot(111)
axes.plot(x, y)
plt.show()
x_highres = np.linspace(0, 2, 20)
y_highres = x_highres ** 2
fig = plt.figure()
axes = fig.add_subplot(111)
axes.plot(x, y)
axes.plot(x_highres, y_highres)
plt.show()
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()
fig = plt.figure()
axes = fig.add_subplot(111)
axes.plot(x_highres, y_highres, color="red", linestyle='dashed', linewidth=3)
plt.show()
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``.
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()
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()
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()
noise = np.random.random((128,128))
noise
array([[ 0.31743783, 0.89968839, 0.64593546, ..., 0.92315307, 0.40529038, 0.57868866], [ 0.03509869, 0.41812159, 0.09614452, ..., 0.35230104, 0.4358769 , 0.42696774], [ 0.87243532, 0.16665336, 0.30484216, ..., 0.57570625, 0.41494208, 0.60731167], ..., [ 0.8604435 , 0.91615867, 0.44869023, ..., 0.15850609, 0.85534071, 0.87021376], [ 0.01936096, 0.56630654, 0.49670692, ..., 0.63496359, 0.15847743, 0.53732171], [ 0.67941664, 0.33243944, 0.41832329, ..., 0.7415799 , 0.56672038, 0.93639001]])
plt.imshow(noise)
plt.show()
plt.imshow(noise)
plt.colorbar()
plt.show()
plt.imshow(noise, cmap=plt.cm.gray)
plt.colorbar()
plt.show()
plt.imshow(noise, cmap=plt.cm.Paired)
plt.colorbar()
plt.show()