Python Matplotlib
Python Matplotlib
Python Matplotlib
# We can use NumPy to specify the values for both axes with greater precision
x = np.arange(0, 5, 0.01)
plt.plot(x, [x1**2 for x1 in x]) # vertical co-ordinates of the points plotted: y = x^2
plt.show()
Multiline Plots
# Multiple functions can be drawn on the same plot
x = range(5)
plt.plot(x, [x1 for x1 in x])
plt.plot(x, [x1*x1 for x1 in x])
plt.plot(x, [x1*x1*x1 for x1 in x])
plt.show()
Scatter Plot
# Scatter plots display values for two sets of data, visualised as a collection of poin
ts
# Two Gaussion distribution plotted
x = np.random.rand(1000)
y = np.random.rand(1000)
plt.scatter(x, y)
plt.show()
Styling
# Matplotlib allows to choose custom colours for plots
y = np.arange(1, 3)
plt.plot(y, 'y') # Specifying line colours
plt.plot(y+5, 'm')
plt.plot(y+10, 'c')
plt.show()
Color code:
1. b = Blue
2. c = Cyan
3. g = Green
4. k = Black
5. m = Magenta
6. r = Red
7. w = White
8. y = Yellow
# Matplotlib allows different line styles for plots
y = np.arange(1, 100)
plt.plot(y, '--', y*5, '-.', y*10, ':')
plt.show()
# - Solid line
# -- Dashed line
# -. Dash-Dot line
# : Dotted Line
# Matplotlib provides customization options for markers
y = np.arange(1, 3, 0.2)
plt.plot(y, '*',
y+0.5, 'o',
y+1, 'D',
y+2, '^',
y+3, 's') # Specifying line styling
plt.show()