# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ### 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

import numpy as np
import matplotlib.pyplot as plt

# # Functional Method

x = np.linspace(0, 5, 11)
y = x**2

x

y

plt.plot(x, y)
plt.show()

# ## Title and Labels

# +
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)

# ## Figure Size and dpi

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

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

plt.show()
# -

# ## Styling Figure

# +
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()
# -

# ## xticks and yticks

# +
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()
# -

# ## Legend

# +
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??
# -

# ## Marker

# +
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??
# -

# ## Subplot

# +
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-.')
# -

# # Object Oriented Methods

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()
# -

# ## Axes in Figure

# +
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')
# -

# ## Subplots

# +
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()
# -

# # 2D Plot

# ## Line Plot

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()
# -

# ## Scatter Plot

# +
x = np.random.randint(100, size = 50)

y = np.random.randint(100, size = 50)
# -

plt.scatter(x, y, marker = 'o')

# # 3D Plot

# ## Line Plot

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')
# -

# ## 3D Scatter Plot

# +
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')


# -

# ## 3D Surface Plot

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

# # Saving Figure

fig.savefig('test.pdf', dpi = 200)

StarcoderdataJupyterScriptsDedupFiltered