<jupyter_start><jupyter_text>Author : <NAME> Topic : MatplotLib Pracetice : Python Practice Email : <EMAIL> What Is Python Matplotlib?matplotlib.pyplot is a plotting library used for 2D graphics in python programming language. It can be used in python scripts, shell, web application servers and other graphical user interface toolkits.Is Matplotlib Included in Python?Matplotlib is not a part of the Standard Libraries which is installed by default when Python, there are several toolkits which are available that extend python matplotlib functionality. Some of them are separate downloads, others can be shipped with the matplotlib source code but have external dependencies.Basemap: It is a map plotting toolkit with various map projections, coastlines and political boundaries.Cartopy: It is a mapping library featuring object-oriented map projection definitions, and arbitrary point, line, polygon and image transformation capabilities.Excel tools: Matplotlib provides utilities for exchanging data with Microsoft Excel.Mplot3d: It is used for 3-D plots.Natgrid: It is an interface to the natgrid library for irregular gridding of the spaced data.You may go through this recording of Python Matplotlib where our instructor has explained how to download Matplotlib in Python and the topics in a detailed manner with examples that will help you to understand this concept better.  Import Libraries<jupyter_code>import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Functional Method<jupyter_code>x = np.linspace(0, 5, 11)
y = x**2
x
y
plt.plot(x, y)
plt.show()<jupyter_output><empty_output><jupyter_text>Title and Labels<jupyter_code>plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Graph of $y = x^2$')

plt.plot(x, y)
plt.show()
x1 = np.linspace(-np.pi, np.pi, 256)
x2 = np.linspace(-np.pi, np.pi, 256)

y1 = np.cos(x1)
y2 = np.sin(x2)
plt.plot(x1, y1, x2, y2)<jupyter_output><empty_output><jupyter_text>Figure Size and dpi<jupyter_code>plt.figure(figsize = (10, 6), dpi = 80)

plt.plot(x1, y1)
plt.plot(x2, y2)

plt.show()<jupyter_output><empty_output><jupyter_text>Styling Figure<jupyter_code>plt.figure(figsize = (10, 6), dpi = 80)

plt.plot(x1, y1, 'r-.')
plt.plot(x2, y2, 'b--')

plt.show()
plt.figure(figsize = (10, 6), dpi = 80)

plt.plot(x1, y1, color = 'purple')
plt.plot(x2, y2, color = 'green')

plt.show()
plt.figure(figsize = (10, 6), dpi = 80)

plt.plot(x1, y1, color = 'purple', lw = 2.5)
plt.plot(x2, y2, color = 'green', lw = 2.5)

plt.show()
plt.figure(figsize = (10, 6), dpi = 80)

plt.plot(x1, y1, color = 'purple', lw = 2.5, ls = '--')
plt.plot(x2, y2, color = 'green', lw = 2.5, ls = '-.')

plt.show()<jupyter_output><empty_output><jupyter_text>xticks and yticks<jupyter_code>plt.figure(figsize = (10, 6), dpi = 80)

plt.plot(x1, y1, color = 'purple', lw = 2.5, ls = '--')
plt.plot(x2, y2, color = 'green', lw = 2.5, ls = '-.')

plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi])
plt.yticks([-1, 0, 1])

plt.show()
plt.figure(figsize = (10, 6), dpi = 80)

plt.plot(x1, y1, color = 'purple', lw = 2.5, ls = '--')
plt.plot(x2, y2, color = 'green', lw = 2.5, ls = '-.')

plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-\pi$', r'$-\pi/2$', r'$0$',r'$\pi/2$',r'$\pi$'])
plt.yticks([-1, 0, 1])

plt.show()<jupyter_output><empty_output><jupyter_text>Legend<jupyter_code>plt.figure(figsize = (10, 6), dpi = 80)

plt.plot(x1, y1, color = 'purple', lw = 2.5, ls = '--', label = '$y = cosx$')
plt.plot(x2, y2, color = 'green', lw = 2.5, ls = '-.', label = '$y = sinx$')

plt.xlabel('X')
plt.ylabel('Y')

plt.title('Graph')

plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi], [r'$-\pi$', r'$-\pi/2$', r'$0$',r'$\pi/2$',r'$\pi$'])
plt.yticks([-1, 0, 1])

plt.legend(loc = 0)

plt.show()
plt.legend??<jupyter_output><empty_output><jupyter_text>Marker<jupyter_code>plt.figure(figsize = (10,6), dpi = 80)

plt.plot([1,2,3,4,5], [1,4,9,16, 25], color = 'red', lw = 2.5, marker = '^', mec = 'blue', mew = 5)

plt.show()
plt.plot??<jupyter_output><empty_output><jupyter_text>Subplot<jupyter_code>x = np.array([1,2,3,4,5])

y1 = x**2
y2 = np.exp(x)
plt.figure(figsize = (10, 6), dpi = 80)

plt.subplot(1, 2, 1)
plt.plot(x, y1, 'r')

plt.subplot(1,2, 2)
plt.plot(x, y2, 'b-.')<jupyter_output><empty_output><jupyter_text>Object Oriented Methods<jupyter_code>x = np.array([1,2,3,4,5,6,7,8,9,10])
y = x**2
fig = plt.figure(figsize = (10,6))

axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])

axes.plot(x, y)

plt.show()<jupyter_output><empty_output><jupyter_text>Axes in Figure<jupyter_code>fig = plt.figure(figsize = (10,6))

axes1 = fig.add_axes([0.1, 0.1, 0.8, 0.8])
axes2 = fig.add_axes([0.15, 0.55, 0.4, 0.3])

axes1.plot(x,y, 'r--')
axes1.set_title('Large Plot')

axes2.plot(y,x, 'g-.')
axes2.set_title('Small Plot')<jupyter_output><empty_output><jupyter_text>Subplots<jupyter_code>fig = plt.figure(figsize = (10, 6))

axes = fig.subplots(3, 3)
type(axes)
fig = plt.figure(figsize = (10, 6))

axes = fig.subplots(2, 3)
fig = plt.figure(figsize = (10, 6))

axes = fig.subplots(2, 3)

axes[0][0].plot(x,y,'r--')
axes[0][0].set_title('0,0')

axes[0][1].plot(x,y,'b-.')
axes[0][1].set_title('0,1')

axes[0][2].plot(x,y,'go')
axes[0][2].set_title('0,2')

axes[1,0].plot(y,x,'r--')
axes[1,0].set_title('1,0')

fig.tight_layout()<jupyter_output><empty_output><jupyter_text>2D Plot  Line Plot<jupyter_code>t = np.arange(0, 2,0.01)
s = 1 + np.sin(2*np.pi*t)
fig, ax = plt.subplots()

ax.plot(t, s)
ax.set(xlabel = 'Time', ylabel = 'Voltages', title = 'Time vs Voltage')

ax.grid()
plt.show()<jupyter_output><empty_output><jupyter_text>Scatter Plot<jupyter_code>x = np.random.randint(100, size = 50)

y = np.random.randint(100, size = 50)
plt.scatter(x, y, marker = 'o')<jupyter_output><empty_output><jupyter_text>3D Plot  Line Plot<jupyter_code>from mpl_toolkits import mplot3d
fig = plt.figure(figsize= (10,6))

ax = plt.axes(projection = '3d')
fig = plt.figure(figsize= (10,6))

ax = plt.axes(projection = '3d')

zline = np.linspace(0, 15, 1000)
xline = np.sin(zline)
yline = np.cos(zline)

ax.plot3D(xline, yline, zline, 'red')<jupyter_output><empty_output><jupyter_text>3D Scatter Plot<jupyter_code>fig = plt.figure(figsize = (10, 6))

ax = plt.axes(projection = '3d')

x = np.random.randint(100, size = 200)
y = np.random.randint(100, size = 200)
z = np.random.randint(100, size = 200)

ax.scatter3D(x, y, z, c = 'red')<jupyter_output><empty_output><jupyter_text>3D Surface Plot<jupyter_code>def f(x, y):
    return np.sin(np.sqrt(x**2+y**2))
r = np.linspace(0, 6, 20)
theta = np.linspace(-0.9*np.pi, 0.8*np.pi, 40)

r,theta = np.meshgrid(r, theta)

X = r*np.sin(theta)
Y = r*np.cos(theta)
Z = f(X,Y)
fig = plt.figure(figsize = (10, 6))

ax = plt.axes(projection = '3d')

ax.plot_surface(X, Y, Z, cmap = 'winter')

plt.show()
ax.view_init(60, 0)
fig<jupyter_output><empty_output><jupyter_text>Saving Figure<jupyter_code>fig.savefig('test.pdf', dpi = 200)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19192501802451334, 'fraction_numerical': 0.05118961788031723, 'mean_word_length': 5.143489813994686, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '464066', 'license': 'MIT', 'n_tokens_mistral': 3294, 'n_tokens_neox': 3038, 'n_words': 657, 'path': 'MatplotLib/MatplotLib.ipynb', 'repo_name': 'jrderek/Data-science-master-resources', 'size': '6957'}