import matplotlib.pyplot as plt
import numpy as np
x = np.array([4, 5, 3, 1, 6, 7])
plt.plot(x)
plt.show()
x = np.arange(25)
If only one value is given in plt.plot(x), first parameter x will be taken as index value like 0, 1, 2, 3.... and second parametetr will be taken as y.
plt.plot(x)
plt.show()
plt.plot(x, x**2 + 1)
plt.show()
[y for y in x]
y = [(y ** 2) for y in x]
y
plt.plot(x, y)
plt.show()
y = [(y**3 + 1) for y in x]
plt.plot(x, y)
plt.show()
x = np.arange(10)
x
plt.plot(x, x**2)
plt.plot(x, x**3)
plt.plot(x, 2*x)
plt.plot(x, 2**x)
plt.show()
plt.plot(x, x**2, x, x**3, x, 2*x, x, 2**x)
plt.show()
plt.plot(x, -x**2)
plt.plot(x, -x**3)
plt.plot(x, -2*x)
plt.plot(x, -2**x)
plt.show()
plt.plot(x, -x**2, x, -x**3, x, -2*x, x, -2**x)
plt.show()
x = np.array([[6, 2, 1, 5, 4], [7, 4, 1, 5, 2]])
x
plt.plot(x)
plt.show()
x = np.random.randint(5, size=(2, 4))
x
[x[0], x[1]]
plt.plot([x[0], x[1]])
plt.show()
x[0], x[1]
plt.plot(x[0], x[1])
plt.show()
x = np.random.randn(2, 10)
x
plt.plot([x[0], x[1]])
plt.show()
x = np.array([9, 1, 4, 2, 1])
x
y = np.array([5, 7, 3, 4, 1])
y
plt.plot(x, y)
plt.title("X & Y Line Graph")
plt.xlabel("X - axis")
plt.ylabel("y - axis")
plt.show()
plt.plot(x, y, color = "red")
plt.title("X & Y Line Graph")
plt.xlabel("X - axis")
plt.ylabel("y - axis")
plt.show()
plt.plot(x, y, color = "red", label = "xy")
plt.plot(y, x, color = "green", label = "yx")
plt.title("X & Y Line Graph")
plt.xlabel("X - axis")
plt.ylabel("y - axis")
plt.legend()
plt.show()
plt.plot(x, y, color = "red", label = "xy")
plt.plot(y, x, color = "green", label = "yx")
plt.title("X & Y Line Graph", color = "red", fontsize = 20)
plt.xlabel("X - axis", color = "blue", fontsize = 15)
plt.ylabel("y - axis", color = "green", fontsize = 15)
plt.legend()
plt.show()
plt.plot(x, y, color = "red", label = "xy")
plt.plot(y, x, color = "green", label = "yx")
plt.title("X & Y Line Graph", color = "red", fontsize = 20)
plt.xlabel("X - axis", color = "blue", fontsize = 15)
plt.ylabel("y - axis", color = "green", fontsize = 15)
plt.legend(loc='upper left')
plt.show()
linestyle description
'-' or 'solid' solid line '--' or 'dashed' dashed line '-.' or 'dashdot' dash-dotted line ':' or 'dotted' dotted line
plt.plot(x, x, "-", color = "blue", label = "xx")
plt.plot(x, y, "--", color = "red", label = "xy")
plt.plot(y, x, '-.', color = "green", label = "yx")
plt.title("X & Y Line Graph", color = "red", fontsize = 20)
plt.xlabel("X - axis", color = "blue", fontsize = 15)
plt.ylabel("y - axis", color = "green", fontsize = 15)
plt.legend(loc='upper left')
plt.show()
marker description
"." point "," pixel "o" circle "v" triangle_down "^" triangle_up "<" triangle_left ">" triangle_right "1" tri_down "2" tri_up "3" tri_left "4" tri_right "8" octagon "s" square "p" pentagon "P" plus (filled) "*" star "h" hexagon1 "H" hexagon2 "+" plus "x" x "X" x (filled) "D" diamond "d" thindiamond "|" vline "" hline 0(TICKLEFT) tickleft 1(TICKRIGHT)tickright 2(TICKUP) tickup 3(TICKDOWN) tickdown 4(CARETLEFT)caretleft 5(CARETRIGHT)caretright 6(CARETUP) caretup 7(CARETDOWN)caretdown 8(CARETLEFTBASE)caretleft (centered at base) 9(CARETRIGHTBASE)caretright (centered at base) 10(CARETUPBASE)caretup (centered at base) 11(CARETDOWNBASE)caretdown (centered at base)
plt.plot(x, x, "ro--", label = "xx")
plt.plot(x, y, "y<--", label = "xy")
plt.plot(y, x, 'bD-.', label = "yx")
plt.title("X & Y Line Graph", color = "red", fontsize = 20)
plt.xlabel("X - axis", color = "blue", fontsize = 15)
plt.ylabel("y - axis", color = "green", fontsize = 15)
plt.legend(loc='upper left')
plt.show()
plt.plot(x, y, label = "xy")
plt.plot(y, x, label = "yx")
plt.title("X & Y Line Graph")
plt.xlabel("X - axis")
plt.ylabel("y - axis")
plt.legend()
plt.grid(True)
plt.xlim(0, 12)
plt.ylim(0, 12)
plt.show()
If only image name is provided in savefig() function, figure will save in current directory. If we want to save in specific directory, we have to provide folder location and image name.
plt.plot(x, y, label = "xy")
plt.plot(y, x, label = "yx")
plt.title("X & Y Line Graph")
plt.xlabel("X - axis")
plt.ylabel("y - axis")
plt.legend()
plt.grid(True)
plt.xlim(0, 12)
plt.ylim(0, 12)
plt.savefig('image1.png')
plt.show()
x = np.array([9, 1, 4, 2, 1])
y = np.array([5, 7, 3, 4, 1])
y1 = np.array([3, 1, 5, 9, 3])
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('A single plot')
plt.show()
fig, (ax1, ax2) = plt.subplots(2)
fig.suptitle('Vertically stacked subplots')
ax1.plot(x, y)
ax2.plot(x, y1)
plt.show()
In plt.subplot(1, 2): first parameter represents row value which is 1, and second value represent column value which is 2.
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('Horizontally stacked subplots')
ax1.plot(x, y)
ax2.plot(x, y1)
plt.show()
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
ax1.plot(x, 'ro--')
ax1.set_title("First Graph")
ax2.plot(x, y, 'g--')
ax1.set_title("Second Graph")
ax3.plot(x, y1, color = "blue")
ax1.set_title("Third Graph")
ax4.plot(x, y**2, color = "purple")
ax1.set_title("Fourth Graph")
fig.suptitle('Horizontally stacked subplots')
plt.show()